@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 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);
          }
        });
  }
Exemple #4
0
 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();
                 }
               }
             }
           });
 }
    /**
     * 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);
    }
Exemple #6
0
 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);
 }
  @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));
  }
  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());
          }
        });
  }
Exemple #10
0
  @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();
    }
  }
Exemple #11
0
 /**
  * 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);
 }