Search in sources :

Example 1 with EventHandler

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

the class ClientTestSupport method getAllEventHandlers.

protected Map<Long, EventHandler> getAllEventHandlers(HazelcastInstance client) {
    ClientConnectionManager connectionManager = getHazelcastClientInstanceImpl(client).getConnectionManager();
    Collection<Connection> activeConnections = connectionManager.getActiveConnections();
    HashMap<Long, EventHandler> map = new HashMap<>();
    for (Connection activeConnection : activeConnections) {
        map.putAll(((ClientConnection) activeConnection).getEventHandlers());
    }
    return map;
}
Also used : HashMap(java.util.HashMap) ClientConnection(com.hazelcast.client.impl.connection.ClientConnection) Connection(com.hazelcast.internal.nio.Connection) EventHandler(com.hazelcast.client.impl.spi.EventHandler) ClientConnectionManager(com.hazelcast.client.impl.connection.ClientConnectionManager)

Example 2 with EventHandler

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

the class NearCachedClientCacheProxy method registerInvalidationListener.

private void registerInvalidationListener() {
    if (!invalidateOnChange) {
        return;
    }
    EventHandler eventHandler = new NearCacheInvalidationEventHandler();
    invalidationListenerId = addNearCacheInvalidationListener(eventHandler);
}
Also used : EventHandler(com.hazelcast.client.impl.spi.EventHandler)

Example 3 with EventHandler

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

the class ClientListenerServiceImpl method handleEventMessageOnCallingThread.

public void handleEventMessageOnCallingThread(ClientMessage clientMessage) {
    long correlationId = clientMessage.getCorrelationId();
    ClientConnection connection = (ClientConnection) clientMessage.getConnection();
    EventHandler eventHandler = connection.getEventHandler(correlationId);
    if (eventHandler == null) {
        if (logger.isFineEnabled()) {
            logger.fine("No eventHandler for callId: " + correlationId + ", event: " + clientMessage);
        }
        return;
    }
    eventHandler.handle(clientMessage);
}
Also used : EventHandler(com.hazelcast.client.impl.spi.EventHandler) ClientConnection(com.hazelcast.client.impl.connection.ClientConnection)

Example 4 with EventHandler

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

the class ClientHeartbeatTest method testAddingListenerToNewConnectionFailedBecauseOfHeartbeat.

@Test
public void testAddingListenerToNewConnectionFailedBecauseOfHeartbeat() throws Exception {
    hazelcastFactory.newHazelcastInstance();
    final HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
    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().addConnectionListener(new ConnectionListener() {

        @Override
        public void connectionAdded(Connection connection) {
        }

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

        AtomicInteger count = new AtomicInteger(0);

        @Override
        public void handle(Object event) {
        }

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

        @Override
        public void onListenerRegister(Connection connection) {
            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.impl.spi.ClientListenerService) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Connection(com.hazelcast.internal.nio.Connection) HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) EventHandler(com.hazelcast.client.impl.spi.EventHandler) ConnectionListener(com.hazelcast.internal.nio.ConnectionListener) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with EventHandler

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

the class ClientCacheClearTest method testClientInvalidationListenerCallCount.

@Test
public void testClientInvalidationListenerCallCount() {
    ICache<String, String> cache = createCache();
    Map<String, String> entries = createAndFillEntries();
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        cache.put(entry.getKey(), entry.getValue());
    }
    // Verify that put works
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        String key = entry.getKey();
        String expectedValue = entries.get(key);
        String actualValue = cache.get(key);
        assertEquals(expectedValue, actualValue);
    }
    final AtomicInteger counter = new AtomicInteger(0);
    CacheConfig config = cache.getConfiguration(CacheConfig.class);
    registerInvalidationListener(new EventHandler() {

        @Override
        public void handle(Object event) {
            counter.getAndIncrement();
        }
    }, config.getNameWithPrefix());
    cache.clear();
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertEquals(1, counter.get());
        }
    });
    // Make sure that the callback is not called for a while
    assertTrueAllTheTime(new AssertTask() {

        @Override
        public void run() throws Exception {
            assertTrue(counter.get() <= 1);
        }
    }, 3);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EventHandler(com.hazelcast.client.impl.spi.EventHandler) AssertTask(com.hazelcast.test.AssertTask) Map(java.util.Map) CacheConfig(com.hazelcast.config.CacheConfig) NearCacheConfig(com.hazelcast.config.NearCacheConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) CacheClearTest(com.hazelcast.cache.CacheClearTest)

Aggregations

EventHandler (com.hazelcast.client.impl.spi.EventHandler)7 ClientConnection (com.hazelcast.client.impl.connection.ClientConnection)2 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)2 Connection (com.hazelcast.internal.nio.Connection)2 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Test (org.junit.Test)2 CacheClearTest (com.hazelcast.cache.CacheClearTest)1 HazelcastClientNotActiveException (com.hazelcast.client.HazelcastClientNotActiveException)1 HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)1 ClientConnectionManager (com.hazelcast.client.impl.connection.ClientConnectionManager)1 ClientListenerService (com.hazelcast.client.impl.spi.ClientListenerService)1 ClientInvocation (com.hazelcast.client.impl.spi.impl.ClientInvocation)1 ClientInvocationFuture (com.hazelcast.client.impl.spi.impl.ClientInvocationFuture)1 ListenerMessageCodec (com.hazelcast.client.impl.spi.impl.ListenerMessageCodec)1 CacheConfig (com.hazelcast.config.CacheConfig)1 NearCacheConfig (com.hazelcast.config.NearCacheConfig)1 HazelcastException (com.hazelcast.core.HazelcastException)1 HazelcastInstance (com.hazelcast.core.HazelcastInstance)1