Search in sources :

Example 1 with MessageListener

use of com.hazelcast.topic.MessageListener 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 MessageListener

use of com.hazelcast.topic.MessageListener 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 3 with MessageListener

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

the class ClientRegressionWithMockNetworkTest method testServerShouldNotCloseClientWhenClientOnlyListening.

@Test
@Category(SlowTest.class)
public void testServerShouldNotCloseClientWhenClientOnlyListening() {
    Config config = new Config();
    int clientHeartbeatSeconds = 8;
    config.setProperty(ClusterProperty.CLIENT_HEARTBEAT_TIMEOUT_SECONDS.getName(), String.valueOf(clientHeartbeatSeconds));
    hazelcastFactory.newHazelcastInstance(config);
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.setProperty(ClientProperty.HEARTBEAT_INTERVAL.getName(), "1000");
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
    HazelcastInstance client2 = hazelcastFactory.newHazelcastClient();
    AtomicBoolean isClientDisconnected = new AtomicBoolean();
    client.getLifecycleService().addLifecycleListener(event -> {
        if (CLIENT_DISCONNECTED.equals(event.getState())) {
            isClientDisconnected.set(true);
        }
    });
    String key = "topicName";
    ITopic<Object> topic = client.getTopic(key);
    MessageListener<Object> listener = message -> {
    };
    UUID id = topic.addMessageListener(listener);
    ITopic<Object> client2Topic = client2.getTopic(key);
    long begin = System.currentTimeMillis();
    while (System.currentTimeMillis() - begin < TimeUnit.SECONDS.toMillis(clientHeartbeatSeconds * 2)) {
        client2Topic.publish("message");
    }
    topic.removeMessageListener(id);
    assertFalse(isClientDisconnected.get());
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) LifecycleListener(com.hazelcast.core.LifecycleListener) QuickTest(com.hazelcast.test.annotation.QuickTest) PortableWriter(com.hazelcast.nio.serialization.PortableWriter) SerializationConfig(com.hazelcast.config.SerializationConfig) MapListener(com.hazelcast.map.listener.MapListener) Future(java.util.concurrent.Future) After(org.junit.After) DataSerializable(com.hazelcast.nio.serialization.DataSerializable) Assert.fail(org.junit.Assert.fail) ObjectDataInput(com.hazelcast.nio.ObjectDataInput) ClientSecurityConfig(com.hazelcast.client.config.ClientSecurityConfig) EntryEvent(com.hazelcast.core.EntryEvent) Logger(com.hazelcast.logging.Logger) LifecycleEvent(com.hazelcast.core.LifecycleEvent) HazelcastTestSupport(com.hazelcast.test.HazelcastTestSupport) Collection(java.util.Collection) CLIENT_CONNECTED(com.hazelcast.core.LifecycleEvent.LifecycleState.CLIENT_CONNECTED) ENTRY_COUNT(com.hazelcast.config.MaxSizePolicy.ENTRY_COUNT) UUID(java.util.UUID) Category(org.junit.experimental.categories.Category) ClusterProperty(com.hazelcast.spi.properties.ClusterProperty) CountDownLatch(java.util.concurrent.CountDownLatch) NearCacheConfig(com.hazelcast.config.NearCacheConfig) MessageListener(com.hazelcast.topic.MessageListener) Assert.assertFalse(org.junit.Assert.assertFalse) HazelcastParallelClassRunner(com.hazelcast.test.HazelcastParallelClassRunner) ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) Mockito.mock(org.mockito.Mockito.mock) RunWith(org.junit.runner.RunWith) ClientExecutionServiceImpl(com.hazelcast.client.impl.spi.impl.ClientExecutionServiceImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Portable(com.hazelcast.nio.serialization.Portable) Callable(java.util.concurrent.Callable) AtomicReference(java.util.concurrent.atomic.AtomicReference) ListenerConfig(com.hazelcast.config.ListenerConfig) ClientProperty(com.hazelcast.client.properties.ClientProperty) ConnectionRetryConfig(com.hazelcast.client.config.ConnectionRetryConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) TestHazelcastFactory(com.hazelcast.client.test.TestHazelcastFactory) ITopic(com.hazelcast.topic.ITopic) UsernamePasswordCredentials(com.hazelcast.security.UsernamePasswordCredentials) LinkedList(java.util.LinkedList) ExpectedException(org.junit.rules.ExpectedException) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapInterceptor(com.hazelcast.map.MapInterceptor) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) SlowTest(com.hazelcast.test.annotation.SlowTest) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) DistributedObject(com.hazelcast.core.DistributedObject) Rule(org.junit.Rule) LifecycleState(com.hazelcast.core.LifecycleEvent.LifecycleState) Assert.assertNull(org.junit.Assert.assertNull) IExecutorService(com.hazelcast.core.IExecutorService) PortableReader(com.hazelcast.nio.serialization.PortableReader) CLIENT_DISCONNECTED(com.hazelcast.core.LifecycleEvent.LifecycleState.CLIENT_DISCONNECTED) EntryAdapter(com.hazelcast.core.EntryAdapter) Assert.assertEquals(org.junit.Assert.assertEquals) IMap(com.hazelcast.map.IMap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) SerializationConfig(com.hazelcast.config.SerializationConfig) ClientSecurityConfig(com.hazelcast.client.config.ClientSecurityConfig) NearCacheConfig(com.hazelcast.config.NearCacheConfig) ListenerConfig(com.hazelcast.config.ListenerConfig) ConnectionRetryConfig(com.hazelcast.client.config.ConnectionRetryConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) DistributedObject(com.hazelcast.core.DistributedObject) ClientConfig(com.hazelcast.client.config.ClientConfig) UUID(java.util.UUID) Category(org.junit.experimental.categories.Category) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 4 with MessageListener

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

the class ReliableTopicProxy method addMessageListener.

private void addMessageListener(ListenerConfig listenerConfig) {
    NodeEngine nodeEngine = getNodeEngine();
    MessageListener listener = loadListener(listenerConfig);
    if (listener == null) {
        return;
    }
    if (listener instanceof HazelcastInstanceAware) {
        HazelcastInstanceAware hazelcastInstanceAware = (HazelcastInstanceAware) listener;
        hazelcastInstanceAware.setHazelcastInstance(nodeEngine.getHazelcastInstance());
    }
    addMessageListener(listener);
}
Also used : NodeEngine(com.hazelcast.spi.impl.NodeEngine) ReliableMessageListener(com.hazelcast.topic.ReliableMessageListener) MessageListener(com.hazelcast.topic.MessageListener) HazelcastInstanceAware(com.hazelcast.core.HazelcastInstanceAware)

Example 5 with MessageListener

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

the class ReliableTopicProxy method loadListener.

private MessageListener loadListener(ListenerConfig listenerConfig) {
    try {
        MessageListener listener = (MessageListener) listenerConfig.getImplementation();
        if (listener != null) {
            return listener;
        }
        if (listenerConfig.getClassName() != null) {
            Object object = ClassLoaderUtil.newInstance(nodeEngine.getConfigClassLoader(), listenerConfig.getClassName());
            if (!(object instanceof MessageListener)) {
                throw new HazelcastException("class '" + listenerConfig.getClassName() + "' is not an instance of " + MessageListener.class.getName());
            }
            listener = (MessageListener) object;
        }
        return listener;
    } catch (Exception e) {
        throw ExceptionUtil.rethrow(e);
    }
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) ReliableMessageListener(com.hazelcast.topic.ReliableMessageListener) MessageListener(com.hazelcast.topic.MessageListener) AbstractDistributedObject(com.hazelcast.spi.impl.AbstractDistributedObject) TopicOverloadException(com.hazelcast.topic.TopicOverloadException) HazelcastException(com.hazelcast.core.HazelcastException)

Aggregations

MessageListener (com.hazelcast.topic.MessageListener)9 ITopic (com.hazelcast.topic.ITopic)5 Message (com.hazelcast.topic.Message)5 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)4 QuickTest (com.hazelcast.test.annotation.QuickTest)4 Test (org.junit.Test)4 UUID (java.util.UUID)3 HazelcastInstanceAware (com.hazelcast.core.HazelcastInstanceAware)2 NodeEngine (com.hazelcast.spi.impl.NodeEngine)2 ReliableMessageListener (com.hazelcast.topic.ReliableMessageListener)2 ClientConfig (com.hazelcast.client.config.ClientConfig)1 ClientSecurityConfig (com.hazelcast.client.config.ClientSecurityConfig)1 ConnectionRetryConfig (com.hazelcast.client.config.ConnectionRetryConfig)1 ClientExecutionServiceImpl (com.hazelcast.client.impl.spi.impl.ClientExecutionServiceImpl)1 ClientProperty (com.hazelcast.client.properties.ClientProperty)1 TestHazelcastFactory (com.hazelcast.client.test.TestHazelcastFactory)1 MemberImpl (com.hazelcast.cluster.impl.MemberImpl)1 Config (com.hazelcast.config.Config)1 EvictionConfig (com.hazelcast.config.EvictionConfig)1 ListenerConfig (com.hazelcast.config.ListenerConfig)1