Search in sources :

Example 31 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project geode by apache.

the class AlertAppenderJUnitTest method testListenerHandling.

/**
   * Verify that adding/removing/replacing listeners works correctly.
   */
@Test
public void testListenerHandling() throws Exception {
    DistributedMember member1 = createTestDistributedMember("Member1");
    DistributedMember member2 = createTestDistributedMember("Member2");
    DistributedMember member3 = createTestDistributedMember("Member3");
    DistributedMember member4 = createTestDistributedMember("Member4");
    DistributedMember member5 = createTestDistributedMember("Member5");
    DistributedMember member6 = createTestDistributedMember("Member6");
    LogService.setBaseLogLevel(Level.WARN);
    AlertAppender.getInstance().addAlertListener(member1, Alert.SEVERE);
    AlertAppender.getInstance().addAlertListener(member2, Alert.WARNING);
    AlertAppender.getInstance().addAlertListener(member3, Alert.ERROR);
    AlertAppender.getInstance().addAlertListener(member4, Alert.ERROR);
    AlertAppender.getInstance().addAlertListener(member5, Alert.WARNING);
    AlertAppender.getInstance().addAlertListener(member6, Alert.SEVERE);
    Field listenersField = AlertAppender.getInstance().getClass().getDeclaredField("listeners");
    listenersField.setAccessible(true);
    @SuppressWarnings("unchecked") final CopyOnWriteArrayList<AlertAppender.Listener> listeners = (CopyOnWriteArrayList<AlertAppender.Listener>) listenersField.get(AlertAppender.getInstance());
    // Verify add
    assertSame(member5, listeners.get(0).getMember());
    assertSame(member2, listeners.get(1).getMember());
    assertSame(member4, listeners.get(2).getMember());
    assertSame(member3, listeners.get(3).getMember());
    assertSame(member6, listeners.get(4).getMember());
    assertSame(member1, listeners.get(5).getMember());
    assertSame(6, listeners.size());
    // Verify replace with same level
    AlertAppender.getInstance().addAlertListener(member5, Alert.WARNING);
    assertSame(member5, listeners.get(0).getMember());
    assertSame(member2, listeners.get(1).getMember());
    assertSame(member4, listeners.get(2).getMember());
    assertSame(member3, listeners.get(3).getMember());
    assertSame(member6, listeners.get(4).getMember());
    assertSame(member1, listeners.get(5).getMember());
    assertSame(6, listeners.size());
    // Verify replace with difference level
    AlertAppender.getInstance().addAlertListener(member5, Alert.SEVERE);
    assertSame(member2, listeners.get(0).getMember());
    assertSame(member4, listeners.get(1).getMember());
    assertSame(member3, listeners.get(2).getMember());
    assertSame(member5, listeners.get(3).getMember());
    assertSame(member6, listeners.get(4).getMember());
    assertSame(member1, listeners.get(5).getMember());
    assertSame(6, listeners.size());
    // Verify remove
    assertTrue(AlertAppender.getInstance().removeAlertListener(member3));
    assertSame(member2, listeners.get(0).getMember());
    assertSame(member4, listeners.get(1).getMember());
    assertSame(member5, listeners.get(2).getMember());
    assertSame(member6, listeners.get(3).getMember());
    assertSame(member1, listeners.get(4).getMember());
    assertSame(5, listeners.size());
    assertTrue(AlertAppender.getInstance().removeAlertListener(member1));
    assertTrue(AlertAppender.getInstance().removeAlertListener(member2));
    assertFalse(AlertAppender.getInstance().removeAlertListener(member3));
    assertTrue(AlertAppender.getInstance().removeAlertListener(member4));
    assertTrue(AlertAppender.getInstance().removeAlertListener(member5));
    assertTrue(AlertAppender.getInstance().removeAlertListener(member6));
}
Also used : Field(java.lang.reflect.Field) DistributedMember(org.apache.geode.distributed.DistributedMember) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 32 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project Shuttle by timusus.

the class VideoCastManager method onQueueUpdated.

/*
    * This is called by onQueueStatusUpdated() of RemoteMediaPlayer
    */
private void onQueueUpdated(List<MediaQueueItem> queueItems, MediaQueueItem item, int repeatMode, boolean shuffle) {
    LOGD(TAG, "onQueueUpdated() reached");
    LOGD(TAG, String.format("Queue Items size: %d, Item: %s, Repeat Mode: %d, Shuffle: %s", queueItems == null ? 0 : queueItems.size(), item, repeatMode, shuffle));
    if (queueItems != null) {
        mMediaQueue = new MediaQueue(new CopyOnWriteArrayList<>(queueItems), item, shuffle, repeatMode);
    } else {
        mMediaQueue = new MediaQueue(new CopyOnWriteArrayList<MediaQueueItem>(), null, false, MediaStatus.REPEAT_MODE_REPEAT_OFF);
    }
    for (VideoCastConsumer consumer : mVideoConsumers) {
        consumer.onMediaQueueUpdated(queueItems, item, repeatMode, shuffle);
    }
}
Also used : VideoCastConsumer(com.google.android.libraries.cast.companionlibrary.cast.callbacks.VideoCastConsumer) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 33 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project hazelcast by hazelcast.

the class XAResourceProxy method start.

@Override
public void start(Xid xid, int flags) throws XAException {
    long threadId = currentThreadId();
    TransactionContext threadContext = threadContextMap.get(currentThreadId());
    switch(flags) {
        case TMNOFLAGS:
            List<TransactionContext> contexts = new CopyOnWriteArrayList<TransactionContext>();
            List<TransactionContext> currentContexts = xidContextMap.putIfAbsent(xid, contexts);
            if (currentContexts != null) {
                throw new XAException("There is already TransactionContexts for the given xid: " + xid);
            }
            TransactionContext context = createTransactionContext(xid);
            contexts.add(context);
            threadContextMap.put(threadId, context);
            break;
        case TMRESUME:
        case TMJOIN:
            List<TransactionContext> contextList = xidContextMap.get(xid);
            if (contextList == null) {
                throw new XAException("There is no TransactionContexts for the given xid: " + xid);
            }
            if (threadContext == null) {
                threadContext = createTransactionContext(xid);
                threadContextMap.put(threadId, threadContext);
                contextList.add(threadContext);
            }
            break;
        default:
            throw new XAException("Unknown flag!" + flags);
    }
}
Also used : XAException(javax.transaction.xa.XAException) TransactionContext(com.hazelcast.transaction.TransactionContext) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 34 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project hazelcast by hazelcast.

the class ClientDiscoverySpiTest method testNodeStartup.

@Test
public void testNodeStartup() {
    Config config = new Config();
    config.setProperty("hazelcast.discovery.enabled", "true");
    config.getNetworkConfig().setPort(50001);
    InterfacesConfig interfaces = config.getNetworkConfig().getInterfaces();
    interfaces.clear();
    interfaces.setEnabled(true);
    interfaces.addInterface("127.0.0.1");
    List<DiscoveryNode> discoveryNodes = new CopyOnWriteArrayList<DiscoveryNode>();
    DiscoveryStrategyFactory factory = new CollectingDiscoveryStrategyFactory(discoveryNodes);
    JoinConfig join = config.getNetworkConfig().getJoin();
    join.getTcpIpConfig().setEnabled(false);
    join.getMulticastConfig().setEnabled(false);
    DiscoveryConfig discoveryConfig = join.getDiscoveryConfig();
    discoveryConfig.getDiscoveryStrategyConfigs().clear();
    DiscoveryStrategyConfig strategyConfig = new DiscoveryStrategyConfig(factory, Collections.<String, Comparable>emptyMap());
    discoveryConfig.addDiscoveryStrategyConfig(strategyConfig);
    final HazelcastInstance hazelcastInstance1 = Hazelcast.newHazelcastInstance(config);
    final HazelcastInstance hazelcastInstance2 = Hazelcast.newHazelcastInstance(config);
    final HazelcastInstance hazelcastInstance3 = Hazelcast.newHazelcastInstance(config);
    try {
        ClientConfig clientConfig = new ClientConfig();
        clientConfig.setProperty("hazelcast.discovery.enabled", "true");
        discoveryConfig = clientConfig.getNetworkConfig().getDiscoveryConfig();
        discoveryConfig.getDiscoveryStrategyConfigs().clear();
        strategyConfig = new DiscoveryStrategyConfig(factory, Collections.<String, Comparable>emptyMap());
        discoveryConfig.addDiscoveryStrategyConfig(strategyConfig);
        final HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
        assertNotNull(hazelcastInstance1);
        assertNotNull(hazelcastInstance2);
        assertNotNull(hazelcastInstance3);
        assertNotNull(client);
        assertTrueEventually(new AssertTask() {

            @Override
            public void run() throws Exception {
                assertEquals(3, hazelcastInstance1.getCluster().getMembers().size());
                assertEquals(3, hazelcastInstance2.getCluster().getMembers().size());
                assertEquals(3, hazelcastInstance3.getCluster().getMembers().size());
                assertEquals(3, client.getCluster().getMembers().size());
            }
        });
    } finally {
        HazelcastClient.shutdownAll();
        Hazelcast.shutdownAll();
    }
}
Also used : InterfacesConfig(com.hazelcast.config.InterfacesConfig) DiscoveryNode(com.hazelcast.spi.discovery.DiscoveryNode) SimpleDiscoveryNode(com.hazelcast.spi.discovery.SimpleDiscoveryNode) DiscoveryStrategyConfig(com.hazelcast.config.DiscoveryStrategyConfig) AwsConfig(com.hazelcast.config.AwsConfig) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig) DiscoveryStrategyConfig(com.hazelcast.config.DiscoveryStrategyConfig) JoinConfig(com.hazelcast.config.JoinConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) InterfacesConfig(com.hazelcast.config.InterfacesConfig) DiscoveryStrategyFactory(com.hazelcast.spi.discovery.DiscoveryStrategyFactory) HazelcastInstance(com.hazelcast.core.HazelcastInstance) JoinConfig(com.hazelcast.config.JoinConfig) AssertTask(com.hazelcast.test.AssertTask) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 35 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project hazelcast by hazelcast.

the class ClientDiscoverySpiTest method testDiscoveryServiceLifecycleMethodsCalledWhenClientAndServerStartAndShutdown.

@Test
public void testDiscoveryServiceLifecycleMethodsCalledWhenClientAndServerStartAndShutdown() {
    //Given
    Config config = new Config();
    config.setProperty("hazelcast.discovery.enabled", "true");
    config.getNetworkConfig().setPort(50001);
    InterfacesConfig interfaces = config.getNetworkConfig().getInterfaces();
    interfaces.clear();
    interfaces.setEnabled(true);
    interfaces.addInterface("127.0.0.1");
    //Both server and client are using the same LifecycleDiscoveryStrategyFactory so latch count is set to 2.
    CountDownLatch startLatch = new CountDownLatch(2);
    CountDownLatch stopLatch = new CountDownLatch(2);
    List<DiscoveryNode> discoveryNodes = new CopyOnWriteArrayList<DiscoveryNode>();
    DiscoveryStrategyFactory factory = new LifecycleDiscoveryStrategyFactory(startLatch, stopLatch, discoveryNodes);
    JoinConfig join = config.getNetworkConfig().getJoin();
    join.getTcpIpConfig().setEnabled(false);
    join.getMulticastConfig().setEnabled(false);
    DiscoveryConfig discoveryConfig = join.getDiscoveryConfig();
    discoveryConfig.getDiscoveryStrategyConfigs().clear();
    DiscoveryStrategyConfig strategyConfig = new DiscoveryStrategyConfig(factory, Collections.<String, Comparable>emptyMap());
    discoveryConfig.addDiscoveryStrategyConfig(strategyConfig);
    final HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config);
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.setProperty("hazelcast.discovery.enabled", "true");
    discoveryConfig = clientConfig.getNetworkConfig().getDiscoveryConfig();
    discoveryConfig.getDiscoveryStrategyConfigs().clear();
    strategyConfig = new DiscoveryStrategyConfig(factory, Collections.<String, Comparable>emptyMap());
    discoveryConfig.addDiscoveryStrategyConfig(strategyConfig);
    final HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
    assertNotNull(hazelcastInstance);
    assertNotNull(client);
    //When
    HazelcastClient.shutdownAll();
    Hazelcast.shutdownAll();
    //Then
    assertOpenEventually(startLatch);
    assertOpenEventually(stopLatch);
}
Also used : InterfacesConfig(com.hazelcast.config.InterfacesConfig) DiscoveryNode(com.hazelcast.spi.discovery.DiscoveryNode) SimpleDiscoveryNode(com.hazelcast.spi.discovery.SimpleDiscoveryNode) DiscoveryStrategyConfig(com.hazelcast.config.DiscoveryStrategyConfig) AwsConfig(com.hazelcast.config.AwsConfig) ClientNetworkConfig(com.hazelcast.client.config.ClientNetworkConfig) DiscoveryStrategyConfig(com.hazelcast.config.DiscoveryStrategyConfig) JoinConfig(com.hazelcast.config.JoinConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) InterfacesConfig(com.hazelcast.config.InterfacesConfig) CountDownLatch(java.util.concurrent.CountDownLatch) DiscoveryStrategyFactory(com.hazelcast.spi.discovery.DiscoveryStrategyFactory) HazelcastInstance(com.hazelcast.core.HazelcastInstance) JoinConfig(com.hazelcast.config.JoinConfig) DiscoveryConfig(com.hazelcast.config.DiscoveryConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)304 CountDownLatch (java.util.concurrent.CountDownLatch)84 ArrayList (java.util.ArrayList)83 List (java.util.List)76 Test (org.junit.Test)71 IOException (java.io.IOException)53 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)32 HashMap (java.util.HashMap)24 Map (java.util.Map)24 ExecutionException (java.util.concurrent.ExecutionException)23 LinkedList (java.util.LinkedList)21 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 Set (java.util.Set)18 TimeUnit (java.util.concurrent.TimeUnit)18 ClientRequest (io.undertow.client.ClientRequest)17 Test (org.junit.jupiter.api.Test)17 HashSet (java.util.HashSet)16 ExecutorService (java.util.concurrent.ExecutorService)16 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)16 ClientConnection (io.undertow.client.ClientConnection)15