Search in sources :

Example 1 with Message

use of com.hazelcast.topic.Message 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));
}
Also used : ITopic(com.hazelcast.topic.ITopic) Message(com.hazelcast.topic.Message) MessageListener(com.hazelcast.topic.MessageListener) UUID(java.util.UUID) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) DurableSubscriptionTest(com.hazelcast.topic.impl.reliable.DurableSubscriptionTest) Test(org.junit.Test)

Example 2 with Message

use of com.hazelcast.topic.Message in project hazelcast by hazelcast.

the class ClientReliableTopicOnClusterRestartTest method shouldContinue_OnClusterRestart_whenDataLoss_LossTolerant_afterInvocationTimeout.

@Test
public void shouldContinue_OnClusterRestart_whenDataLoss_LossTolerant_afterInvocationTimeout() throws InterruptedException {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.getConnectionStrategyConfig().getConnectionRetryConfig().setClusterConnectTimeoutMillis(Long.MAX_VALUE);
    int invocationTimeoutSeconds = 2;
    clientConfig.setProperty(ClientProperty.INVOCATION_TIMEOUT_SECONDS.getName(), String.valueOf(invocationTimeoutSeconds));
    final HazelcastInstance member = hazelcastFactory.newHazelcastInstance(smallInstanceConfig());
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
    AtomicLong messageCount = new AtomicLong();
    CountDownLatch messageArrived = new CountDownLatch(1);
    String topicName = "topic";
    member.getReliableTopic(topicName).publish("message");
    member.getReliableTopic(topicName).publish("message");
    ClientReliableTopicProxy<?> topic = (ClientReliableTopicProxy<?>) client.getReliableTopic(topicName);
    UUID registrationId = topic.addMessageListener(createListener(true, m -> {
        messageCount.incrementAndGet();
        messageArrived.countDown();
    }));
    member.shutdown();
    final HazelcastInstance restartedMember = hazelcastFactory.newHazelcastInstance(smallInstanceConfig());
    // wait some time for subscription
    Thread.sleep(TimeUnit.SECONDS.toMillis(invocationTimeoutSeconds));
    assertTrueEventually(() -> {
        String item = "newItem " + UUID.randomUUID();
        restartedMember.getReliableTopic(topicName).publish(item);
        assertOpenEventually(messageArrived, 5);
    });
    assertFalse(topic.isListenerCancelled(registrationId));
    assertTrue(messageCount.get() >= 1);
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Message(com.hazelcast.topic.Message) QuickTest(com.hazelcast.test.annotation.QuickTest) RunWith(org.junit.runner.RunWith) ClientProperty(com.hazelcast.client.properties.ClientProperty) HazelcastTestSupport.smallInstanceConfig(com.hazelcast.test.HazelcastTestSupport.smallInstanceConfig) ClientReliableTopicProxy(com.hazelcast.client.impl.proxy.ClientReliableTopicProxy) DurableMessageListener(com.hazelcast.topic.impl.reliable.DurableSubscriptionTest.DurableMessageListener) After(org.junit.After) HazelcastTestSupport.assertOpenEventually(com.hazelcast.test.HazelcastTestSupport.assertOpenEventually) ClientConfig(com.hazelcast.client.config.ClientConfig) TestHazelcastFactory(com.hazelcast.client.test.TestHazelcastFactory) ITopic(com.hazelcast.topic.ITopic) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastTestSupport.assertTrueEventually(com.hazelcast.test.HazelcastTestSupport.assertTrueEventually) Test(org.junit.Test) UUID(java.util.UUID) Category(org.junit.experimental.categories.Category) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) TestCase.assertTrue(junit.framework.TestCase.assertTrue) Assert.assertFalse(org.junit.Assert.assertFalse) HazelcastParallelClassRunner(com.hazelcast.test.HazelcastParallelClassRunner) TestCase.assertEquals(junit.framework.TestCase.assertEquals) AtomicLong(java.util.concurrent.atomic.AtomicLong) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientReliableTopicProxy(com.hazelcast.client.impl.proxy.ClientReliableTopicProxy) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with Message

use of com.hazelcast.topic.Message 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);
}
Also used : ClusterService(com.hazelcast.internal.cluster.ClusterService) Message(com.hazelcast.topic.Message) MemberImpl(com.hazelcast.cluster.impl.MemberImpl) MessageListener(com.hazelcast.topic.MessageListener)

Example 4 with Message

use of com.hazelcast.topic.Message in project hazelcast by hazelcast.

the class MessageRunner method toMessage.

private Message<E> toMessage(ReliableTopicMessage m) {
    Member member = getMember(m);
    E payload = serializationService.toObject(m.getPayload());
    return new Message<E>(topicName, payload, m.getPublishTime(), member);
}
Also used : Message(com.hazelcast.topic.Message) Member(com.hazelcast.cluster.Member)

Example 5 with Message

use of com.hazelcast.topic.Message in project hazelcast by hazelcast.

the class ClientReliableTopicOnClusterRestartTest method shouldContinue_OnClusterRestart_afterInvocationTimeout.

@Test
public void shouldContinue_OnClusterRestart_afterInvocationTimeout() throws InterruptedException {
    HazelcastInstance member = hazelcastFactory.newHazelcastInstance(smallInstanceConfig());
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.getConnectionStrategyConfig().getConnectionRetryConfig().setClusterConnectTimeoutMillis(Long.MAX_VALUE);
    int invocationTimeoutSeconds = 2;
    clientConfig.setProperty(ClientProperty.INVOCATION_TIMEOUT_SECONDS.getName(), String.valueOf(invocationTimeoutSeconds));
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
    final CountDownLatch messageArrived = new CountDownLatch(1);
    String topicName = "topic";
    ITopic<String> topic = client.getReliableTopic(topicName);
    UUID registrationId = topic.addMessageListener(createListener(true, m -> messageArrived.countDown()));
    member.shutdown();
    // wait for the topic operation to timeout
    Thread.sleep(TimeUnit.SECONDS.toMillis(invocationTimeoutSeconds));
    member = hazelcastFactory.newHazelcastInstance(smallInstanceConfig());
    member.getReliableTopic(topicName).publish("message");
    assertOpenEventually(messageArrived);
    ClientReliableTopicProxy<?> proxy = (ClientReliableTopicProxy<?>) topic;
    assertFalse(proxy.isListenerCancelled(registrationId));
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Message(com.hazelcast.topic.Message) QuickTest(com.hazelcast.test.annotation.QuickTest) RunWith(org.junit.runner.RunWith) ClientProperty(com.hazelcast.client.properties.ClientProperty) HazelcastTestSupport.smallInstanceConfig(com.hazelcast.test.HazelcastTestSupport.smallInstanceConfig) ClientReliableTopicProxy(com.hazelcast.client.impl.proxy.ClientReliableTopicProxy) DurableMessageListener(com.hazelcast.topic.impl.reliable.DurableSubscriptionTest.DurableMessageListener) After(org.junit.After) HazelcastTestSupport.assertOpenEventually(com.hazelcast.test.HazelcastTestSupport.assertOpenEventually) ClientConfig(com.hazelcast.client.config.ClientConfig) TestHazelcastFactory(com.hazelcast.client.test.TestHazelcastFactory) ITopic(com.hazelcast.topic.ITopic) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastTestSupport.assertTrueEventually(com.hazelcast.test.HazelcastTestSupport.assertTrueEventually) Test(org.junit.Test) UUID(java.util.UUID) Category(org.junit.experimental.categories.Category) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) TestCase.assertTrue(junit.framework.TestCase.assertTrue) Assert.assertFalse(org.junit.Assert.assertFalse) HazelcastParallelClassRunner(com.hazelcast.test.HazelcastParallelClassRunner) TestCase.assertEquals(junit.framework.TestCase.assertEquals) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientReliableTopicProxy(com.hazelcast.client.impl.proxy.ClientReliableTopicProxy) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) UUID(java.util.UUID) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

Message (com.hazelcast.topic.Message)10 ITopic (com.hazelcast.topic.ITopic)8 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)7 QuickTest (com.hazelcast.test.annotation.QuickTest)7 Test (org.junit.Test)7 MessageListener (com.hazelcast.topic.MessageListener)5 UUID (java.util.UUID)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 ClientConfig (com.hazelcast.client.config.ClientConfig)3 ClientReliableTopicProxy (com.hazelcast.client.impl.proxy.ClientReliableTopicProxy)3 ClientProperty (com.hazelcast.client.properties.ClientProperty)3 TestHazelcastFactory (com.hazelcast.client.test.TestHazelcastFactory)3 Config (com.hazelcast.config.Config)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 HazelcastParallelClassRunner (com.hazelcast.test.HazelcastParallelClassRunner)3 HazelcastTestSupport.assertOpenEventually (com.hazelcast.test.HazelcastTestSupport.assertOpenEventually)3 HazelcastTestSupport.assertTrueEventually (com.hazelcast.test.HazelcastTestSupport.assertTrueEventually)3 HazelcastTestSupport.smallInstanceConfig (com.hazelcast.test.HazelcastTestSupport.smallInstanceConfig)3 DurableSubscriptionTest (com.hazelcast.topic.impl.reliable.DurableSubscriptionTest)3 DurableMessageListener (com.hazelcast.topic.impl.reliable.DurableSubscriptionTest.DurableMessageListener)3