I have 2 instances for my backend (2 differents servers)
I am using hazelcast 5.5 with spring boot in my application for differents cases :
- Hibernate L2 cache
- Cache applicative for my api (@Cacheable and evictIfPresent)
- Topic for publish/subscribe messaging used for websocket notifications
hazelcast.xml :
access-cache
ip1/ip2:5701
5701
ip1:5701
ip2:5701
30
60000
10000
1
0
10
true
BLOCK
and in my code for the topic publisher/listener :
@Configuration
public class HazelcastPubSubConfig {
/**
* Creates a Hazelcast instance for pub/sub communication.
*
* @return the configured {@link HazelcastInstance}
* @throws IOException if {@code hazelcast.xml} cannot be read
*/
@Bean(name = "hazelcastPubSub")
public HazelcastInstance hazelcastPubSubInstance() throws IOException {
InputStream is = new ClassPathResource("hazelcast.xml").getInputStream();
Config config = new XmlConfigBuilder(is).build();
return Hazelcast.newHazelcastInstance(config);
}
}
@Component
public class DistributedEventListener {
private final ObjectMapper mapper;
/**
* Constructs a DistributedEventListener.
*
* @param hz the Hazelcast instance for subscribing to topics
* @param multicaster the application event multicaster for local event broadcasting
* @param mapper the ObjectMapper for JSON serialization/deserialization
*/
public DistributedEventListener(@Qualifier("hazelcastPubSub") HazelcastInstance hz,
@Qualifier("applicationEventMulticaster") ApplicationEventMulticaster multicaster,
ObjectMapper mapper) {
this.mapper = mapper;
ITopic topic = hz.getReliableTopic("distributed-events");
topic.addMessageListener(msg -> {
try {
String json = msg.getMessageObject();
StreamNotification> event =
mapper.readValue(json, StreamNotification.class);
multicaster.multicastEvent(new PayloadApplicationEvent<>(this, event));
} catch (IOException e) {
throw new RuntimeException("Error deserializing event", e);
}
});
}
}
@Service
public class DistributedEventPublisher {
private final ITopic topic;
private final ObjectMapper mapper;
/**
* Constructs a HazelcastPubSubConfig.
*
* @param hz the Hazelcast instance for publishing to topics
* @param mapper the ObjectMapper for JSON serialization
*/
public DistributedEventPublisher(@Qualifier("hazelcastPubSub") HazelcastInstance hz, ObjectMapper mapper) {
this.topic = hz.getReliableTopic("distributed-events");
this.mapper = mapper;
}
/**
* Publish a StreamNotification event to the distributed topic.
*
* @param event the event to publish
* @throws RuntimeException if serialization fails
*/
public void publish(StreamNotification> event) {
try {
String json = mapper.writeValueAsString(event);
topic.publish(json);
} catch (JsonProcessingException e) {
throw new RuntimeException("Error serializing event: ", e);
}
}
}
What i want to know if with my conf, i want to have ONE hazelcast instance in my server for the cluster of two nodes (two servers) and this cluster handle the 3 points (app cache, hibernate L2, topic publish/listener)
Thank you