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));
}
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);
}
}
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);
}
}
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();
}
}
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);
}
Aggregations