@Test public void testDurableSubscription() { HazelcastInstance local = createHazelcastInstance(); ITopic<String> topic = local.getReliableTopic("topic"); final DurableMessageListener<String> listener = new DurableMessageListener<String>(); String id = topic.addMessageListener(listener); topic.publish("item1"); assertTrueEventually( new AssertTask() { @Override public void run() throws Exception { assertTrue(listener.objects.contains("item1")); } }); topic.removeMessageListener(id); // todo: this part is still racy because we don't know when the listener is terminated. topic.publish("item2"); topic.publish("item3"); topic.addMessageListener(listener); assertTrueEventually( new AssertTask() { @Override public void run() throws Exception { assertEquals(asList("item1", "item2", "item3"), listener.objects); assertEquals(asList(0l, 1l, 2l), listener.sequences); } }); }
@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()); }
@Test public void removeMessageListener() throws InterruptedException { String topicName = "removeMessageListener" + generateRandomString(5); try { HazelcastInstance instance = createHazelcastInstance(); ITopic<String> topic = instance.getTopic(topicName); final CountDownLatch latch = new CountDownLatch(2); final CountDownLatch cp = new CountDownLatch(1); MessageListener<String> messageListener = new MessageListener<String>() { public void onMessage(Message<String> msg) { latch.countDown(); cp.countDown(); } }; final String message = "message_" + messageListener.hashCode() + "_"; final String id = topic.addMessageListener(messageListener); topic.publish(message + "1"); cp.await(); topic.removeMessageListener(id); topic.publish(message + "2"); assertTrueEventually( new AssertTask() { @Override public void run() { assertEquals(1, latch.getCount()); } }); } finally { shutdownNodeFactory(); } }
public void shutdown() { msgTopic.removeMessageListener(registrationId); queue.clear(); }
/** * Removes the topic listener from the topic with the specified name. * * @param name the name of the topic * @param listener the listener to add * @param <E> the type of the topic items */ public <E> void removeListener(String name, MessageListener<E> listener) { ITopic<E> topic = getTopic(name); topic.removeMessageListener(listener); }