use of java.util.concurrent.CopyOnWriteArrayList in project archaius by Netflix.
the class ConcurrentMapConfiguration method addPropertyDirect.
protected void addPropertyDirect(String key, Object value) {
ReentrantLock lock = locks[Math.abs(key.hashCode()) % NUM_LOCKS];
lock.lock();
try {
Object previousValue = map.putIfAbsent(key, value);
if (previousValue == null) {
return;
}
if (previousValue instanceof List) {
// the value is added to the existing list
((List) previousValue).add(value);
} else {
// the previous value is replaced by a list containing the previous value and the new value
List<Object> list = new CopyOnWriteArrayList<Object>();
list.add(previousValue);
list.add(value);
map.put(key, list);
}
} finally {
lock.unlock();
}
}
use of java.util.concurrent.CopyOnWriteArrayList in project archaius by Netflix.
the class DynamicURLConfigurationTestWithFileURL method testFileURLWithPropertiesUpdatedDynamically.
@Test
public void testFileURLWithPropertiesUpdatedDynamically() throws IOException, InterruptedException {
File file = File.createTempFile("DynamicURLConfigurationTestWithFileURL", "testFileURLWithPropertiesUpdatedDynamically");
populateFile(file, "test.host=12312,123213", "test.host1=13212");
AbstractConfiguration.setDefaultListDelimiter(',');
DynamicURLConfiguration config = new DynamicURLConfiguration(0, 500, false, file.toURI().toString());
Thread.sleep(1000);
Assert.assertEquals(13212, config.getInt("test.host1"));
Thread.sleep(1000);
populateFile(file, "test.host=12312,123213", "test.host1=13212");
populateFile(file, "test.host=12312,123213", "test.host1=13212");
CopyOnWriteArrayList writeList = new CopyOnWriteArrayList();
writeList.add("12312");
writeList.add("123213");
config.setProperty("sample.domain", "google,yahoo");
Assert.assertEquals(writeList, config.getProperty("test.host"));
}
use of java.util.concurrent.CopyOnWriteArrayList in project pinpoint by naver.
the class ActiveMQClientITBase method testQueuePush.
@Test
public void testQueuePush() throws Exception {
// Given
final String testQueueName = "TestPushQueue";
final ActiveMQQueue testQueue = new ActiveMQQueue(testQueueName);
final String testMessage = "Hello World for Queue!";
final CountDownLatch consumerLatch = new CountDownLatch(1);
final Collection<Throwable> consumerThrowables = new CopyOnWriteArrayList<Throwable>();
// create producer
ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl());
MessageProducer producer = producerSession.createProducer(testQueue);
final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage);
// create consumer
ActiveMQSession consumerSession = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
MessageConsumer consumer = consumerSession.createConsumer(testQueue);
consumer.setMessageListener(new AssertTextMessageListener(consumerLatch, consumerThrowables, expectedTextMessage));
// When
producer.send(expectedTextMessage);
consumerLatch.await(1L, TimeUnit.SECONDS);
// Then
assertNoConsumerError(consumerThrowables);
// Wait till all traces are recorded (consumer traces are recorded from another thread)
awaitAndVerifyTraceCount(2, 5000L);
// trace count : 1
verifyProducerSendEvent(testQueue);
// trace count : 1
verifyConsumerPushEvent(testQueue);
}
use of java.util.concurrent.CopyOnWriteArrayList in project pinpoint by naver.
the class ActiveMQClientITBase method testTopicPush.
@Test
public void testTopicPush() throws Exception {
// Given
final String testTopicName = "TestPushTopic";
final ActiveMQTopic testTopic = new ActiveMQTopic(testTopicName);
final String testMessage = "Hello World for Topic!";
final int numMessageConsumers = 2;
final CountDownLatch consumerConsumeLatch = new CountDownLatch(numMessageConsumers);
final Collection<Throwable> consumerThrowables = new CopyOnWriteArrayList<Throwable>();
// create producer
ActiveMQSession producerSession = ActiveMQClientITHelper.createSession(getProducerBrokerName(), getProducerBrokerUrl());
MessageProducer producer = new MessageProducerBuilder(producerSession, testTopic).waitTillStarted().build();
final TextMessage expectedTextMessage = producerSession.createTextMessage(testMessage);
// create 2 consumers
ActiveMQSession consumer1Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
new MessageConsumerBuilder(consumer1Session, testTopic).withMessageListener(new AssertTextMessageListener(consumerConsumeLatch, consumerThrowables, expectedTextMessage)).waitTillStarted().build();
ActiveMQSession consumer2Session = ActiveMQClientITHelper.createSession(getConsumerBrokerName(), getConsumerBrokerUrl());
new MessageConsumerBuilder(consumer2Session, testTopic).withMessageListener(new AssertTextMessageListener(consumerConsumeLatch, consumerThrowables, expectedTextMessage)).waitTillStarted().build();
// When
producer.send(expectedTextMessage);
consumerConsumeLatch.await(1L, TimeUnit.SECONDS);
// Then
// Wait till all traces are recorded (consumer traces are recorded from another thread)
awaitAndVerifyTraceCount(3, 1000L);
// trace count : 1
verifyProducerSendEvent(testTopic);
// trace count : 1
verifyConsumerPushEvent(testTopic);
// trace count : 1
verifyConsumerPushEvent(testTopic);
}
use of java.util.concurrent.CopyOnWriteArrayList in project druid by druid-io.
the class ZkCoordinator method addSegments.
private void addSegments(Collection<DataSegment> segments, final DataSegmentChangeCallback callback) {
ExecutorService loadingExecutor = null;
try (final BackgroundSegmentAnnouncer backgroundSegmentAnnouncer = new BackgroundSegmentAnnouncer(announcer, exec, config.getAnnounceIntervalMillis())) {
backgroundSegmentAnnouncer.startAnnouncing();
loadingExecutor = Execs.multiThreaded(config.getNumBootstrapThreads(), "ZkCoordinator-loading-%s");
final int numSegments = segments.size();
final CountDownLatch latch = new CountDownLatch(numSegments);
final AtomicInteger counter = new AtomicInteger(0);
final CopyOnWriteArrayList<DataSegment> failedSegments = new CopyOnWriteArrayList<>();
for (final DataSegment segment : segments) {
loadingExecutor.submit(new Runnable() {
@Override
public void run() {
try {
log.info("Loading segment[%d/%d][%s]", counter.getAndIncrement(), numSegments, segment.getIdentifier());
loadSegment(segment, callback);
if (!announcer.isAnnounced(segment)) {
try {
backgroundSegmentAnnouncer.announceSegment(segment);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new SegmentLoadingException(e, "Loading Interrupted");
}
}
} catch (SegmentLoadingException e) {
log.error(e, "[%s] failed to load", segment.getIdentifier());
failedSegments.add(segment);
} finally {
latch.countDown();
}
}
});
}
try {
latch.await();
if (failedSegments.size() > 0) {
log.makeAlert("%,d errors seen while loading segments", failedSegments.size()).addData("failedSegments", failedSegments);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.makeAlert(e, "LoadingInterrupted");
}
backgroundSegmentAnnouncer.finishAnnouncing();
} catch (SegmentLoadingException e) {
log.makeAlert(e, "Failed to load segments -- likely problem with announcing.").addData("numSegments", segments.size()).emit();
} finally {
callback.execute();
if (loadingExecutor != null) {
loadingExecutor.shutdownNow();
}
}
}
Aggregations