use of com.hazelcast.core.Message in project hazelcast by hazelcast.
the class TopicTest method addTwoListenerAndRemoveOne.
@Test
public void addTwoListenerAndRemoveOne() throws InterruptedException {
String topicName = "addTwoListenerAndRemoveOne" + generateRandomString(5);
HazelcastInstance instance = createHazelcastInstance();
ITopic<String> topic = instance.getTopic(topicName);
final CountDownLatch latch = new CountDownLatch(3);
final CountDownLatch cp = new CountDownLatch(2);
final AtomicInteger atomicInteger = new AtomicInteger();
final String message = "Hazelcast Rocks!";
MessageListener<String> messageListener1 = new MessageListener<String>() {
public void onMessage(Message<String> msg) {
atomicInteger.incrementAndGet();
latch.countDown();
cp.countDown();
}
};
MessageListener<String> messageListener2 = new MessageListener<String>() {
public void onMessage(Message<String> msg) {
atomicInteger.incrementAndGet();
latch.countDown();
cp.countDown();
}
};
String messageListenerId = topic.addMessageListener(messageListener1);
topic.addMessageListener(messageListener2);
topic.publish(message);
assertOpenEventually(cp);
topic.removeMessageListener(messageListenerId);
topic.publish(message);
assertOpenEventually(latch);
assertEquals(3, atomicInteger.get());
}
use of com.hazelcast.core.Message in project hazelcast by hazelcast.
the class TopicTest method testConfigListenerRegistration.
@Test
public void testConfigListenerRegistration() throws InterruptedException {
String topicName = "default";
Config config = new Config();
final CountDownLatch latch = new CountDownLatch(1);
config.getTopicConfig(topicName).addMessageListenerConfig(new ListenerConfig().setImplementation(new MessageListener() {
public void onMessage(Message message) {
latch.countDown();
}
}));
HazelcastInstance instance = createHazelcastInstance(config);
instance.getTopic(topicName).publish(1);
assertTrue(latch.await(10, TimeUnit.SECONDS));
}
use of com.hazelcast.core.Message in project hazelcast by hazelcast.
the class ReliableTopicAbstractTest method testMessageFieldSetCorrectly.
@Test
public void testMessageFieldSetCorrectly() {
final ReliableMessageListenerMock listener = new ReliableMessageListenerMock();
topic.addMessageListener(listener);
final long beforePublishTime = Clock.currentTimeMillis();
topic.publish("foo");
final long afterPublishTime = Clock.currentTimeMillis();
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
assertEquals(1, listener.messages.size());
Message<String> message = listener.messages.get(0);
assertEquals("foo", message.getMessageObject());
Member localMember = local.getCluster().getLocalMember();
assertEquals(localMember, message.getPublishingMember());
long actualPublishTime = message.getPublishTime();
assertTrue(actualPublishTime >= beforePublishTime);
assertTrue(actualPublishTime <= afterPublishTime);
}
});
}
Aggregations