Search in sources :

Example 1 with EventHandler

use of com.hazelcast.client.spi.EventHandler in project hazelcast by hazelcast.

the class ClientCacheProxy method registerCacheEntryListener.

@Override
public void registerCacheEntryListener(CacheEntryListenerConfiguration cacheEntryListenerConfiguration, boolean addToConfig) {
    ensureOpen();
    if (cacheEntryListenerConfiguration == null) {
        throw new NullPointerException("CacheEntryListenerConfiguration can't be null");
    }
    CacheEventListenerAdaptor<K, V> adaptor = new CacheEventListenerAdaptor<K, V>(this, cacheEntryListenerConfiguration, clientContext.getSerializationService(), clientContext.getHazelcastInstance());
    EventHandler handler = createHandler(adaptor);
    String regId = clientContext.getListenerService().registerListener(createCacheEntryListenerCodec(), handler);
    if (regId != null) {
        if (addToConfig) {
            cacheConfig.addCacheEntryListenerConfiguration(cacheEntryListenerConfiguration);
        }
        addListenerLocally(regId, cacheEntryListenerConfiguration);
        if (addToConfig) {
            updateCacheListenerConfigOnOtherNodes(cacheEntryListenerConfiguration, true);
        }
    }
}
Also used : CacheEventListenerAdaptor(com.hazelcast.cache.impl.CacheEventListenerAdaptor) EventHandler(com.hazelcast.client.spi.EventHandler)

Example 2 with EventHandler

use of com.hazelcast.client.spi.EventHandler in project hazelcast by hazelcast.

the class ClientReplicatedMapProxy method addNearCacheInvalidateListener.

private void addNearCacheInvalidateListener() {
    try {
        EventHandler handler = new ReplicatedMapAddNearCacheEventHandler();
        invalidationListenerId = registerListener(createNearCacheInvalidationListenerCodec(), handler);
    } catch (Exception e) {
        ILogger logger = getContext().getLoggingService().getLogger(ClientReplicatedMapProxy.class);
        logger.severe("-----------------\nNear Cache is not initialized!\n-----------------", e);
    }
}
Also used : EventHandler(com.hazelcast.client.spi.EventHandler) ILogger(com.hazelcast.logging.ILogger)

Example 3 with EventHandler

use of com.hazelcast.client.spi.EventHandler in project hazelcast by hazelcast.

the class ClientInvocationServiceSupport method registerInvocation.

private void registerInvocation(ClientInvocation clientInvocation) {
    short protocolVersion = client.getProtocolVersion();
    ClientMessage clientMessage = clientInvocation.getClientMessage();
    clientMessage.setVersion(protocolVersion);
    long correlationId = clientMessage.getCorrelationId();
    callIdMap.put(correlationId, clientInvocation);
    EventHandler handler = clientInvocation.getEventHandler();
    if (handler != null) {
        clientListenerService.addEventHandler(correlationId, handler);
    }
}
Also used : EventHandler(com.hazelcast.client.spi.EventHandler) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Example 4 with EventHandler

use of com.hazelcast.client.spi.EventHandler in project hazelcast by hazelcast.

the class ClientHeartbeatTest method testAddingListenerToNewConnectionFailedBecauseOfHeartbeat.

@Test
public void testAddingListenerToNewConnectionFailedBecauseOfHeartbeat() throws Exception {
    hazelcastFactory.newHazelcastInstance();
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.setProperty(ClientProperty.HEARTBEAT_TIMEOUT.getName(), "4000");
    clientConfig.setProperty(ClientProperty.HEARTBEAT_INTERVAL.getName(), "1000");
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
    HazelcastClientInstanceImpl clientInstanceImpl = getHazelcastClientInstanceImpl(client);
    final ClientListenerService clientListenerService = clientInstanceImpl.getListenerService();
    final CountDownLatch blockIncoming = new CountDownLatch(1);
    final CountDownLatch heartbeatStopped = new CountDownLatch(1);
    final CountDownLatch onListenerRegister = new CountDownLatch(2);
    clientInstanceImpl.getConnectionManager().addConnectionHeartbeatListener(new ConnectionHeartbeatListener() {

        @Override
        public void heartbeatResumed(Connection connection) {
        }

        @Override
        public void heartbeatStopped(Connection connection) {
            heartbeatStopped.countDown();
        }
    });
    clientListenerService.registerListener(createPartitionLostListenerCodec(), new EventHandler() {

        AtomicInteger count = new AtomicInteger(0);

        @Override
        public void handle(Object event) {
        }

        @Override
        public void beforeListenerRegister() {
            if (count.incrementAndGet() == 2) {
                try {
                    blockIncoming.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onListenerRegister() {
            onListenerRegister.countDown();
        }
    });
    HazelcastInstance hazelcastInstance2 = hazelcastFactory.newHazelcastInstance();
    assertSizeEventually(2, clientInstanceImpl.getConnectionManager().getActiveConnections());
    blockMessagesFromInstance(hazelcastInstance2, client);
    assertOpenEventually(heartbeatStopped);
    blockIncoming.countDown();
    unblockMessagesFromInstance(hazelcastInstance2, client);
    assertOpenEventually(onListenerRegister);
}
Also used : ClientListenerService(com.hazelcast.client.spi.ClientListenerService) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Connection(com.hazelcast.nio.Connection) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) EventHandler(com.hazelcast.client.spi.EventHandler) ConnectionHeartbeatListener(com.hazelcast.client.spi.impl.ConnectionHeartbeatListener) ClientConfig(com.hazelcast.client.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with EventHandler

use of com.hazelcast.client.spi.EventHandler in project hazelcast by hazelcast.

the class ClientNonSmartListenerService method invoke.

private ClientEventRegistration invoke(ClientRegistrationKey registrationKey) throws Exception {
    EventHandler handler = registrationKey.getHandler();
    handler.beforeListenerRegister();
    ClientMessage request = registrationKey.getCodec().encodeAddRequest(false);
    ClientInvocation invocation = new ClientInvocation(client, request);
    invocation.setEventHandler(handler);
    ClientInvocationFuture future = invocation.invoke();
    String registrationId = registrationKey.getCodec().decodeAddResponse(future.get());
    handler.onListenerRegister();
    Connection connection = future.getInvocation().getSendConnection();
    return new ClientEventRegistration(registrationId, request.getCorrelationId(), connection, registrationKey.getCodec());
}
Also used : Connection(com.hazelcast.nio.Connection) EventHandler(com.hazelcast.client.spi.EventHandler) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture)

Aggregations

EventHandler (com.hazelcast.client.spi.EventHandler)11 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)3 ListenerMessageCodec (com.hazelcast.client.spi.impl.ListenerMessageCodec)3 Connection (com.hazelcast.nio.Connection)3 ParallelTest (com.hazelcast.test.annotation.ParallelTest)3 QuickTest (com.hazelcast.test.annotation.QuickTest)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Test (org.junit.Test)3 CacheClearTest (com.hazelcast.cache.CacheClearTest)2 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)2 ClientListenerService (com.hazelcast.client.spi.ClientListenerService)2 ClientInvocation (com.hazelcast.client.spi.impl.ClientInvocation)2 ClientInvocationFuture (com.hazelcast.client.spi.impl.ClientInvocationFuture)2 CacheConfig (com.hazelcast.config.CacheConfig)2 NearCacheConfig (com.hazelcast.config.NearCacheConfig)2 AssertTask (com.hazelcast.test.AssertTask)2 Map (java.util.Map)2 CacheEventListenerAdaptor (com.hazelcast.cache.impl.CacheEventListenerAdaptor)1 ClientConfig (com.hazelcast.client.config.ClientConfig)1 ClientConnection (com.hazelcast.client.connection.nio.ClientConnection)1