use of com.hazelcast.topic.MessageListener in project hazelcast by hazelcast.
the class ClientReliableTopicTest method testRemoveListener.
@Test
public void testRemoveListener() {
ITopic topic = client.getReliableTopic(randomString());
MessageListener listener = new MessageListener() {
public void onMessage(Message message) {
}
};
UUID id = topic.addMessageListener(listener);
assertTrue(topic.removeMessageListener(id));
}
use of com.hazelcast.topic.MessageListener in project hazelcast by hazelcast.
the class TopicService method dispatchEvent.
@Override
public void dispatchEvent(Object event, Object listener) {
TopicEvent topicEvent = (TopicEvent) event;
ClusterService clusterService = nodeEngine.getClusterService();
MemberImpl member = clusterService.getMember(topicEvent.publisherAddress);
if (member == null) {
member = new MemberImpl.Builder(topicEvent.publisherAddress).version(nodeEngine.getVersion()).build();
}
Message message = new DataAwareMessage(topicEvent.name, topicEvent.data, topicEvent.publishTime, member, nodeEngine.getSerializationService());
incrementReceivedMessages(topicEvent.name);
MessageListener messageListener = (MessageListener) listener;
messageListener.onMessage(message);
}
use of com.hazelcast.topic.MessageListener in project hazelcast by hazelcast.
the class ClientRegressionWithMockNetworkTest method testServerShouldNotCloseClientWhenClientOnlyListening.
@Test
@Category(SlowTest.class)
public void testServerShouldNotCloseClientWhenClientOnlyListening() {
Config config = new Config();
int clientHeartbeatSeconds = 8;
config.setProperty(ClusterProperty.CLIENT_HEARTBEAT_TIMEOUT_SECONDS.getName(), String.valueOf(clientHeartbeatSeconds));
hazelcastFactory.newHazelcastInstance(config);
ClientConfig clientConfig = new ClientConfig();
clientConfig.setProperty(ClientProperty.HEARTBEAT_INTERVAL.getName(), "1000");
HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
HazelcastInstance client2 = hazelcastFactory.newHazelcastClient();
AtomicBoolean isClientDisconnected = new AtomicBoolean();
client.getLifecycleService().addLifecycleListener(event -> {
if (CLIENT_DISCONNECTED.equals(event.getState())) {
isClientDisconnected.set(true);
}
});
String key = "topicName";
ITopic<Object> topic = client.getTopic(key);
MessageListener<Object> listener = message -> {
};
UUID id = topic.addMessageListener(listener);
ITopic<Object> client2Topic = client2.getTopic(key);
long begin = System.currentTimeMillis();
while (System.currentTimeMillis() - begin < TimeUnit.SECONDS.toMillis(clientHeartbeatSeconds * 2)) {
client2Topic.publish("message");
}
topic.removeMessageListener(id);
assertFalse(isClientDisconnected.get());
}
use of com.hazelcast.topic.MessageListener in project hazelcast by hazelcast.
the class ReliableTopicProxy method addMessageListener.
private void addMessageListener(ListenerConfig listenerConfig) {
NodeEngine nodeEngine = getNodeEngine();
MessageListener listener = loadListener(listenerConfig);
if (listener == null) {
return;
}
if (listener instanceof HazelcastInstanceAware) {
HazelcastInstanceAware hazelcastInstanceAware = (HazelcastInstanceAware) listener;
hazelcastInstanceAware.setHazelcastInstance(nodeEngine.getHazelcastInstance());
}
addMessageListener(listener);
}
use of com.hazelcast.topic.MessageListener in project hazelcast by hazelcast.
the class ReliableTopicProxy method loadListener.
private MessageListener loadListener(ListenerConfig listenerConfig) {
try {
MessageListener listener = (MessageListener) listenerConfig.getImplementation();
if (listener != null) {
return listener;
}
if (listenerConfig.getClassName() != null) {
Object object = ClassLoaderUtil.newInstance(nodeEngine.getConfigClassLoader(), listenerConfig.getClassName());
if (!(object instanceof MessageListener)) {
throw new HazelcastException("class '" + listenerConfig.getClassName() + "' is not an instance of " + MessageListener.class.getName());
}
listener = (MessageListener) object;
}
return listener;
} catch (Exception e) {
throw ExceptionUtil.rethrow(e);
}
}
Aggregations