Search in sources :

Example 26 with LifecycleListener

use of com.hazelcast.core.LifecycleListener in project hazelcast by hazelcast.

the class ClientSplitBrainTest method testClientListeners_InSplitBrain.

@Test
public void testClientListeners_InSplitBrain() throws Throwable {
    Config config = new Config();
    config.setProperty(GroupProperty.MERGE_FIRST_RUN_DELAY_SECONDS.getName(), "5");
    config.setProperty(GroupProperty.MERGE_NEXT_RUN_DELAY_SECONDS.getName(), "5");
    HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
    HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
    final ClientConfig clientConfig = new ClientConfig();
    final HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
    final String mapName = randomMapName();
    final IMap mapNode1 = h1.getMap(mapName);
    final IMap mapNode2 = h2.getMap(mapName);
    final IMap mapClient = client.getMap(mapName);
    final AtomicBoolean[] listenerGotEventFlags = new AtomicBoolean[3];
    for (int i = 0; i < 3; i++) {
        listenerGotEventFlags[i] = new AtomicBoolean();
    }
    final CountDownLatch mergedLatch = new CountDownLatch(1);
    final LifecycleListener mergeListener = createMergeListener(mergedLatch);
    h1.getLifecycleService().addLifecycleListener(mergeListener);
    h2.getLifecycleService().addLifecycleListener(mergeListener);
    final EntryAdapter entryListener1 = createEntryListener(listenerGotEventFlags[0]);
    mapNode1.addEntryListener(entryListener1, true);
    final EntryAdapter entryListener2 = createEntryListener(listenerGotEventFlags[1]);
    mapNode2.addEntryListener(entryListener2, true);
    final EntryAdapter entryListener3 = createEntryListener(listenerGotEventFlags[2]);
    mapClient.addEntryListener(entryListener3, true);
    closeConnectionBetween(h2, h1);
    assertTrue(mergedLatch.await(30, TimeUnit.SECONDS));
    assertEquals(2, h1.getCluster().getMembers().size());
    assertEquals(2, h2.getCluster().getMembers().size());
    AtomicBoolean testFinished = new AtomicBoolean(false);
    final Thread clientThread = startClientPutThread(mapClient, testFinished);
    try {
        checkEventsEventually(listenerGotEventFlags);
    } catch (Throwable t) {
        throw t;
    } finally {
        testFinished.set(true);
        clientThread.interrupt();
        clientThread.join();
    }
}
Also used : Config(com.hazelcast.config.Config) ClientConfig(com.hazelcast.client.config.ClientConfig) EntryAdapter(com.hazelcast.core.EntryAdapter) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) IMap(com.hazelcast.core.IMap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientConfig(com.hazelcast.client.config.ClientConfig) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 27 with LifecycleListener

use of com.hazelcast.core.LifecycleListener in project hazelcast by hazelcast.

the class NodeQueryCacheContext method flushPublishersOnNodeShutdown.

/**
     * This is a best effort approach; there is no guarantee that events in publishers internal buffers will be fired,
     * {@link com.hazelcast.spi.EventService} can drop them.
     */
private void flushPublishersOnNodeShutdown() {
    Node node = ((NodeEngineImpl) this.nodeEngine).getNode();
    LifecycleServiceImpl lifecycleService = node.hazelcastInstance.getLifecycleService();
    lifecycleService.addLifecycleListener(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            if (SHUTTING_DOWN == event.getState()) {
                publisherContext.flush();
            }
        }
    });
}
Also used : NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) Node(com.hazelcast.instance.Node) LifecycleEvent(com.hazelcast.core.LifecycleEvent) LifecycleListener(com.hazelcast.core.LifecycleListener) LifecycleServiceImpl(com.hazelcast.instance.LifecycleServiceImpl)

Aggregations

LifecycleListener (com.hazelcast.core.LifecycleListener)27 LifecycleEvent (com.hazelcast.core.LifecycleEvent)25 HazelcastInstance (com.hazelcast.core.HazelcastInstance)23 CountDownLatch (java.util.concurrent.CountDownLatch)23 Test (org.junit.Test)21 ClientConfig (com.hazelcast.client.config.ClientConfig)14 ParallelTest (com.hazelcast.test.annotation.ParallelTest)13 QuickTest (com.hazelcast.test.annotation.QuickTest)13 NightlyTest (com.hazelcast.test.annotation.NightlyTest)10 Config (com.hazelcast.config.Config)9 ListenerConfig (com.hazelcast.config.ListenerConfig)7 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)5 AssertTask (com.hazelcast.test.AssertTask)5 JoinConfig (com.hazelcast.config.JoinConfig)3 NetworkConfig (com.hazelcast.config.NetworkConfig)3 DistributedObject (com.hazelcast.core.DistributedObject)3 ExecutionException (java.util.concurrent.ExecutionException)3 LifecycleService (com.hazelcast.core.LifecycleService)2 HazelcastInstanceFactory.newHazelcastInstance (com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance)2 Node (com.hazelcast.instance.Node)2