@Test public void testDestroyTopicRemovesStatistics() { String randomTopicName = randomString(); HazelcastInstance instance = createHazelcastInstance(); final ITopic<String> topic = instance.getTopic(randomTopicName); topic.publish("foobar"); // we need to give the message the chance to be processed, else the topic statistics are // recreated // so in theory the destroy for the topic is broken sleepSeconds(1); topic.destroy(); final TopicService topicService = getNode(instance).nodeEngine.getService(TopicService.SERVICE_NAME); assertTrueEventually( new AssertTask() { @Override public void run() { boolean containsStats = topicService.getStatsMap().containsKey(topic.getName()); assertFalse(containsStats); } }); }
@Test public void testTopicStats() throws InterruptedException { String topicName = "testTopicStats" + generateRandomString(5); HazelcastInstance instance = createHazelcastInstance(); ITopic<String> topic = instance.getTopic(topicName); final CountDownLatch latch1 = new CountDownLatch(1000); topic.addMessageListener( new MessageListener<String>() { public void onMessage(Message msg) { latch1.countDown(); } }); final CountDownLatch latch2 = new CountDownLatch(1000); topic.addMessageListener( new MessageListener<String>() { public void onMessage(Message msg) { latch2.countDown(); } }); for (int i = 0; i < 1000; i++) { topic.publish("sancar"); } assertTrue(latch1.await(1, TimeUnit.MINUTES)); assertTrue(latch2.await(1, TimeUnit.MINUTES)); LocalTopicStatsImpl stats = (LocalTopicStatsImpl) topic.getLocalTopicStats(); assertEquals(1000, stats.getPublishOperationCount()); assertEquals(2000, stats.getReceiveOperationCount()); }
@Test public void addTwoMessageListener() throws InterruptedException { String topicName = "addTwoMessageListener" + generateRandomString(5); HazelcastInstance instance = createHazelcastInstance(); ITopic<String> topic = instance.getTopic(topicName); final CountDownLatch latch = new CountDownLatch(2); final String message = "Hazelcast Rocks!"; topic.addMessageListener( new MessageListener<String>() { public void onMessage(Message<String> msg) { if (msg.getMessageObject().equals(message)) { latch.countDown(); } } }); topic.addMessageListener( new MessageListener<String>() { public void onMessage(Message<String> msg) { if (msg.getMessageObject().equals(message)) { latch.countDown(); } } }); topic.publish(message); assertTrue(latch.await(10000, TimeUnit.MILLISECONDS)); }
@Test public void testName() { String randomTopicName = randomString(); HazelcastInstance hClient = createHazelcastInstance(); ITopic<?> topic = hClient.getTopic(randomTopicName); assertEquals(randomTopicName, topic.getName()); }
protected TopicMBean(ITopic managedObject, ManagementService service) { super(managedObject, service); objectName = service.createObjectName("ITopic", managedObject.getName()); MessageListener messageListener = new MessageListener() { public void onMessage(Message message) { totalMessageCount.incrementAndGet(); } }; registrationId = managedObject.addMessageListener(messageListener); }
public static void main(String[] args) { final String member = hazelcast().getCluster().getLocalMember().toString(); ITopic<String> topic = hazelcast().getTopic("notifications"); topic.addMessageListener( new MessageListener<String>() { public void onMessage(Message<String> message) { System.out.printf("(%s) Received %s %n", member, message.getMessageObject()); } }); }
/* * (non-Javadoc) * * @see * com.baidu.rigel.biplatform.tesseract.store.service.StoreManager#postEvent * (java.util.EventObject) */ @Override public void postEvent(EventObject event) { LOGGER.info("postEvent params: [event:{}] start", event); if (event == null) { LOGGER.info("post event {} error, event is null", event); throw new IllegalArgumentException(); } ITopic<Object> topics = this.hazelcast.getTopic(TOPICS); topics.publish(event); LOGGER.info("post event {} success", event); }
@Test public void whenDiscardNewest_whenNoSpace() { for (int k = 0; k < ringbuffer.capacity(); k++) { topic.publish("old"); } long tail = ringbuffer.tailSequence(); long head = ringbuffer.headSequence(); topic.publish("new"); // check that nothing has changed assertEquals(tail, ringbuffer.tailSequence()); assertEquals(head, ringbuffer.headSequence()); }
@Test public void whenDiscardOldest_whenNoSpace() { for (int k = 0; k < ringbuffer.capacity(); k++) { topic.publish("old"); } long tail = ringbuffer.tailSequence(); long head = ringbuffer.headSequence(); topic.publish("new"); // check that an item has been added assertEquals(tail + 1, ringbuffer.tailSequence()); assertEquals(head + 1, ringbuffer.headSequence()); }
void init() { for (int i = 0; i < threads; i++) { esOrderConsumer.execute(new PositionQueueSlurper()); } topicFeed.addMessageListener(new StockStreamListener()); mapNewOrders.addLocalEntryListener(new NewOrderListener()); startStreamer(); Executors.newSingleThreadExecutor() .execute( new Runnable() { public void run() { while (true) { try { Thread.sleep(5000); long feeds = countReceivedStockUpdates.getAndSet(0) / 5; long orders = countOrdersProcessed.getAndSet(0) / 5; long events = countNewOrderEvents.getAndSet(0) / 5; long views = countPositionViews.getAndSet(0) / 5; log( "Feeds:" + feeds + ", OrdersProcessed:" + orders + ", newOrderEvents:" + events + ", Views:" + views); } catch (Exception e) { e.printStackTrace(); } } } }); }
@Test public void testHazelcastInstances() { assertNotNull(map1); assertNotNull(map2); assertNotNull(multiMap); assertNotNull(replicatedMap); assertNotNull(queue); assertNotNull(topic); assertNotNull(set); assertNotNull(list); assertNotNull(executorService); assertNotNull(idGenerator); assertNotNull(atomicLong); assertNotNull(atomicReference); assertNotNull(countDownLatch); assertNotNull(semaphore); assertNotNull(lock); assertEquals("map1", map1.getName()); assertEquals("map2", map2.getName()); assertEquals("testMultimap", multiMap.getName()); assertEquals("replicatedMap", replicatedMap.getName()); assertEquals("testQ", queue.getName()); assertEquals("testTopic", topic.getName()); assertEquals("set", set.getName()); assertEquals("list", list.getName()); assertEquals("idGenerator", idGenerator.getName()); assertEquals("atomicLong", atomicLong.getName()); assertEquals("atomicReference", atomicReference.getName()); assertEquals("countDownLatch", countDownLatch.getName()); assertEquals("semaphore", semaphore.getName()); }
@Test public void test_whenSpace() throws Exception { topic.publish("foo"); ReliableTopicMessage msg = ringbuffer.readOne(0); assertEquals("foo", serializationService.toObject(msg.getPayload())); }
@Test public void whenBlock_whenNoSpace() { for (int k = 0; k < ringbuffer.capacity(); k++) { topic.publish("old"); } final long tail = ringbuffer.tailSequence(); final long head = ringbuffer.headSequence(); // add the item final Future f = spawn( new Runnable() { @Override public void run() { topic.publish("new"); } }); assertTrueAllTheTime( new AssertTask() { @Override public void run() throws Exception { assertFalse(f.isDone()); assertEquals(tail, ringbuffer.tailSequence()); assertEquals(head, ringbuffer.headSequence()); } }, 5); // assert that message is published eventually assertCompletesEventually(f); }
/** * Constructs the topic listener which will listen on the given topic. * * @param topic the topic to listen to */ public HzTopicListener(ITopic<byte[]> topic) { this.queue = QueueTopicProxyFactory.createQueueProxy( new ArrayBlockingQueue<byte[]>(config.getTopicMaxMessageCount())); this.msgTopic = topic; registrationId = topic.addMessageListener(this); }
@Test public void whenError_andNoSpace() { for (int k = 0; k < ringbuffer.capacity(); k++) { topic.publish("old"); } long tail = ringbuffer.tailSequence(); long head = ringbuffer.headSequence(); try { topic.publish("new"); fail(); } catch (TopicOverloadException expected) { EmptyStatement.ignore(expected); } assertEquals(tail, ringbuffer.tailSequence()); assertEquals(head, ringbuffer.headSequence()); }
@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 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(); } }
/* * (non-Javadoc) * * @see * com.hazelcast.core.MessageListener#onMessage(com.hazelcast.core.Message) */ @Override public void onMessage(Message<byte[]> hzMsg) { // We always queue the message even if we have a message listener. We'll // immediately pull it out of the queue and dispatch in a separate thread. // This is important to prevent slow message handlers from blocking topic // distribution in Hazelcast. if (!queue.offer(hzMsg.getMessageObject())) { log.warn( format( "In-memory message buffer full for topic [%s]. " + "Messages will be lost. Consider increaing the speed of " + "the consumer or the message buffer.", msgTopic.getName())); return; } if (messageListener != null) { hazelcastMQContext.onConsumerDispatchReady(id); } }
@Test public void whenBlock_whenNoSpace() { for (int k = 0; k < ringbuffer.capacity(); k++) { topic.publish("old"); } final long tail = ringbuffer.tailSequence(); final long head = ringbuffer.headSequence(); // add the item final Future f = spawn( new Runnable() { @Override public void run() { topic.publish("new"); } }); // make sure it doesn't complete within 3 seconds. We have a 2 second error margin to prevent // spurious test failures assertTrueAllTheTime( new AssertTask() { @Override public void run() throws Exception { assertFalse(f.isDone()); assertEquals(tail, ringbuffer.tailSequence()); assertEquals(head, ringbuffer.headSequence()); } }, 3); assertCompletesEventually(f); assertEquals(tail + 1, ringbuffer.tailSequence()); // since the ringbuffer got cleaned, the head is at the tail assertEquals(tail + 1, ringbuffer.headSequence()); }
/** Process all events for keys queued to the watcher */ private static void forwardToItopic(WatchEvent.Kind<Path> kind, Path dir) { boolean isDir = Files.isDirectory(dir); if (kind == ENTRY_CREATE) { if (!isDir) { Action act = new Action("add_file", Folder.getInternalPath(dir)); // Folder.getFileFromDiskToWinSafe(act.getPath()); Folder.loadFileFromFSToInternal(dir); topic.publish(act); } else { if (Folder.isEmptyFSFolder(dir)) { Folder.createEmptyFolderInInternal(dir); topic.publish(new Action("create_empty_folder", Folder.getInternalPath(dir))); } else { Folder.loadFolderFromFSToInternal(dir); topic.publish(new Action("create_folder", Folder.getInternalPath(dir))); } } } else if (kind == ENTRY_DELETE) { // todo Folder.deleteFromInternal(dir); topic.publish(new Action("delete_entry", Folder.getInternalPath(dir))); } else if (kind == ENTRY_MODIFY) { // todo if (!isDir) { Folder.loadFileFromFSToInternal(dir); topic.publish(new Action("edit_file", Folder.getInternalPath(dir))); } else { if (Folder.isEmptyFSFolder(dir)) { Folder.createEmptyFolderInInternal(dir); topic.publish(new Action("create_empty_folder", Folder.getInternalPath(dir))); } else { Folder.loadFolderFromFSToInternal(dir); topic.publish(new Action("edit_folder", Folder.getInternalPath(dir))); } } } else { // TODO System.out.println("[forwardToItopic] Unexpected Event - kind=" + kind + "dir=" + dir); } }
@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()); }
/** Testing if topic can properly listen messages and if topic has any issue after a shutdown. */ @Test public void testTopicCluster() throws InterruptedException { String topicName = "TestMessages" + generateRandomString(5); Config cfg = new Config(); TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2); HazelcastInstance[] instances = factory.newInstances(cfg); HazelcastInstance instance1 = instances[0]; HazelcastInstance instance2 = instances[1]; ITopic<String> topic1 = instance1.getTopic(topicName); final CountDownLatch latch1 = new CountDownLatch(1); final String message = "Test" + randomString(); topic1.addMessageListener( new MessageListener<String>() { public void onMessage(Message msg) { assertEquals(message, msg.getMessageObject()); latch1.countDown(); } }); ITopic<String> topic2 = instance2.getTopic(topicName); final CountDownLatch latch2 = new CountDownLatch(2); topic2.addMessageListener( new MessageListener<String>() { public void onMessage(Message msg) { assertEquals(message, msg.getMessageObject()); latch2.countDown(); } }); topic1.publish(message); assertTrue(latch1.await(5, TimeUnit.SECONDS)); instance1.shutdown(); topic2.publish(message); assertTrue(latch2.await(5, TimeUnit.SECONDS)); }
private void firePositionViewChanged(PositionView positionView) { if (positionView == null) return; countPositionViews.incrementAndGet(); ITopic topicPM = hazelcast.getTopic("pm_" + positionView.pmId); topicPM.publish(positionView); }
/** * 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); }
/** * Adds a topic listener to 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 addListener(String name, MessageListener<E> listener) { ITopic<E> topic = getTopic(name); topic.addMessageListener(listener); }
/** * Published the specified message to the Hazelcast topic with the specified name. * * @param name name of the topic * @param message message to publish * @param <E> type of the topic items */ public <E> void publish(String name, E message) { ITopic<E> topic = getTopic(name); topic.publish(message); }
public void shutdown() { msgTopic.removeMessageListener(registrationId); queue.clear(); }
void log(String msg) { if (msg != null) { logger.info(msg); topicLogs.publish(memberString + ": " + msg); } }