Example usage for java.util.concurrent.atomic AtomicBoolean compareAndSet

List of usage examples for java.util.concurrent.atomic AtomicBoolean compareAndSet

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicBoolean compareAndSet.

Prototype

public final boolean compareAndSet(boolean expectedValue, boolean newValue) 

Source Link

Document

Atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle#compareAndSet .

Usage

From source file:org.springframework.kafka.listener.TransactionalContainerTests.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test/*from   ww  w  .j a  v a2s  .  c o  m*/
public void testConsumeAndProduceTransactionExternalTM() throws Exception {
    Consumer consumer = mock(Consumer.class);
    final TopicPartition topicPartition = new TopicPartition("foo", 0);
    willAnswer(i -> {
        ((ConsumerRebalanceListener) i.getArgument(1))
                .onPartitionsAssigned(Collections.singletonList(topicPartition));
        return null;
    }).given(consumer).subscribe(any(Collection.class), any(ConsumerRebalanceListener.class));
    final ConsumerRecords records = new ConsumerRecords(Collections.singletonMap(topicPartition,
            Collections.singletonList(new ConsumerRecord<>("foo", 0, 0, "key", "value"))));
    final AtomicBoolean done = new AtomicBoolean();
    willAnswer(i -> {
        if (done.compareAndSet(false, true)) {
            return records;
        } else {
            Thread.sleep(500);
            return null;
        }
    }).given(consumer).poll(any(Duration.class));
    ConsumerFactory cf = mock(ConsumerFactory.class);
    willReturn(consumer).given(cf).createConsumer("group", "", null);
    Producer producer = mock(Producer.class);

    final CountDownLatch closeLatch = new CountDownLatch(1);

    willAnswer(i -> {
        closeLatch.countDown();
        return null;
    }).given(producer).close();

    final ProducerFactory pf = mock(ProducerFactory.class);
    given(pf.transactionCapable()).willReturn(true);
    given(pf.createProducer()).willReturn(producer);
    ContainerProperties props = new ContainerProperties("foo");
    props.setGroupId("group");
    props.setTransactionManager(new SomeOtherTransactionManager());
    final KafkaTemplate template = new KafkaTemplate(pf);
    props.setMessageListener((MessageListener<String, String>) m -> {
        template.send("bar", "baz");
        template.sendOffsetsToTransaction(Collections.singletonMap(new TopicPartition(m.topic(), m.partition()),
                new OffsetAndMetadata(m.offset() + 1)));
    });
    KafkaMessageListenerContainer container = new KafkaMessageListenerContainer<>(cf, props);
    container.setBeanName("commit");
    container.start();

    assertThat(closeLatch.await(10, TimeUnit.SECONDS)).isTrue();

    InOrder inOrder = inOrder(producer);
    inOrder.verify(producer).beginTransaction();
    ArgumentCaptor<ProducerRecord> captor = ArgumentCaptor.forClass(ProducerRecord.class);
    inOrder.verify(producer).send(captor.capture(), any(Callback.class));
    assertThat(captor.getValue()).isEqualTo(new ProducerRecord("bar", "baz"));
    inOrder.verify(producer).sendOffsetsToTransaction(
            Collections.singletonMap(topicPartition, new OffsetAndMetadata(1)), "group");
    inOrder.verify(producer).commitTransaction();
    inOrder.verify(producer).close();
    container.stop();
    verify(pf).createProducer();
}

From source file:org.springframework.kafka.listener.TransactionalContainerTests.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test/* w ww.j av  a  2s .  c o m*/
public void testConsumeAndProduceTransactionRollback() throws Exception {
    Consumer consumer = mock(Consumer.class);
    final TopicPartition topicPartition0 = new TopicPartition("foo", 0);
    final TopicPartition topicPartition1 = new TopicPartition("foo", 1);
    Map<TopicPartition, List<ConsumerRecord<String, String>>> recordMap = new HashMap<>();
    recordMap.put(topicPartition0,
            Collections.singletonList(new ConsumerRecord<>("foo", 0, 0, "key", "value")));
    recordMap.put(topicPartition1,
            Collections.singletonList(new ConsumerRecord<>("foo", 1, 0, "key", "value")));
    ConsumerRecords records = new ConsumerRecords(recordMap);
    final AtomicBoolean done = new AtomicBoolean();
    willAnswer(i -> {
        if (done.compareAndSet(false, true)) {
            return records;
        } else {
            Thread.sleep(500);
            return null;
        }
    }).given(consumer).poll(any(Duration.class));
    final CountDownLatch seekLatch = new CountDownLatch(2);
    willAnswer(i -> {
        seekLatch.countDown();
        return null;
    }).given(consumer).seek(any(), anyLong());
    ConsumerFactory cf = mock(ConsumerFactory.class);
    willReturn(consumer).given(cf).createConsumer("group", "", null);
    Producer producer = mock(Producer.class);
    final CountDownLatch closeLatch = new CountDownLatch(1);
    willAnswer(i -> {
        closeLatch.countDown();
        return null;
    }).given(producer).close();
    ProducerFactory pf = mock(ProducerFactory.class);
    given(pf.transactionCapable()).willReturn(true);
    given(pf.createProducer()).willReturn(producer);
    KafkaTransactionManager tm = new KafkaTransactionManager(pf);
    ContainerProperties props = new ContainerProperties(new TopicPartitionInitialOffset("foo", 0),
            new TopicPartitionInitialOffset("foo", 1));
    props.setGroupId("group");
    props.setTransactionManager(tm);
    final KafkaTemplate template = new KafkaTemplate(pf);
    props.setMessageListener((MessageListener) m -> {
        template.send("bar", "baz");
        throw new RuntimeException("fail");
    });
    KafkaMessageListenerContainer container = new KafkaMessageListenerContainer<>(cf, props);
    container.setBeanName("rollback");
    container.start();
    assertThat(closeLatch.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(seekLatch.await(10, TimeUnit.SECONDS)).isTrue();
    InOrder inOrder = inOrder(producer);
    inOrder.verify(producer).beginTransaction();
    ArgumentCaptor<ProducerRecord> captor = ArgumentCaptor.forClass(ProducerRecord.class);
    verify(producer).send(captor.capture(), any(Callback.class));
    assertThat(captor.getValue()).isEqualTo(new ProducerRecord("bar", "baz"));
    inOrder.verify(producer, never()).sendOffsetsToTransaction(anyMap(), anyString());
    inOrder.verify(producer, never()).commitTransaction();
    inOrder.verify(producer).abortTransaction();
    inOrder.verify(producer).close();
    verify(consumer).seek(topicPartition0, 0);
    verify(consumer).seek(topicPartition1, 0);
    verify(consumer, never()).commitSync(anyMap());
    container.stop();
    verify(pf, times(1)).createProducer();
}

From source file:org.springframework.kafka.listener.TransactionalContainerTests.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test//from   w ww  . j a  v  a 2 s .c om
public void testConsumeAndProduceTransactionRollbackBatch() throws Exception {
    Consumer consumer = mock(Consumer.class);
    final TopicPartition topicPartition0 = new TopicPartition("foo", 0);
    final TopicPartition topicPartition1 = new TopicPartition("foo", 1);
    Map<TopicPartition, List<ConsumerRecord<String, String>>> recordMap = new HashMap<>();
    recordMap.put(topicPartition0,
            Collections.singletonList(new ConsumerRecord<>("foo", 0, 0, "key", "value")));
    recordMap.put(topicPartition1,
            Collections.singletonList(new ConsumerRecord<>("foo", 1, 0, "key", "value")));
    ConsumerRecords records = new ConsumerRecords(recordMap);
    final AtomicBoolean done = new AtomicBoolean();
    willAnswer(i -> {
        if (done.compareAndSet(false, true)) {
            return records;
        } else {
            Thread.sleep(500);
            return null;
        }
    }).given(consumer).poll(any(Duration.class));
    final CountDownLatch seekLatch = new CountDownLatch(2);
    willAnswer(i -> {
        seekLatch.countDown();
        return null;
    }).given(consumer).seek(any(), anyLong());
    ConsumerFactory cf = mock(ConsumerFactory.class);
    willReturn(consumer).given(cf).createConsumer("group", "", null);
    Producer producer = mock(Producer.class);
    final CountDownLatch closeLatch = new CountDownLatch(1);
    willAnswer(i -> {
        closeLatch.countDown();
        return null;
    }).given(producer).close();
    ProducerFactory pf = mock(ProducerFactory.class);
    given(pf.transactionCapable()).willReturn(true);
    given(pf.createProducer()).willReturn(producer);
    KafkaTransactionManager tm = new KafkaTransactionManager(pf);
    ContainerProperties props = new ContainerProperties(new TopicPartitionInitialOffset("foo", 0),
            new TopicPartitionInitialOffset("foo", 1));
    props.setGroupId("group");
    props.setTransactionManager(tm);
    final KafkaTemplate template = new KafkaTemplate(pf);
    props.setMessageListener((BatchMessageListener) recordlist -> {
        template.send("bar", "baz");
        throw new RuntimeException("fail");
    });
    KafkaMessageListenerContainer container = new KafkaMessageListenerContainer<>(cf, props);
    container.setBeanName("rollback");
    container.start();
    assertThat(closeLatch.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(seekLatch.await(10, TimeUnit.SECONDS)).isTrue();
    InOrder inOrder = inOrder(producer);
    inOrder.verify(producer).beginTransaction();
    ArgumentCaptor<ProducerRecord> captor = ArgumentCaptor.forClass(ProducerRecord.class);
    verify(producer).send(captor.capture(), any(Callback.class));
    assertThat(captor.getValue()).isEqualTo(new ProducerRecord("bar", "baz"));
    inOrder.verify(producer, never()).sendOffsetsToTransaction(anyMap(), anyString());
    inOrder.verify(producer, never()).commitTransaction();
    inOrder.verify(producer).abortTransaction();
    inOrder.verify(producer).close();
    verify(consumer).seek(topicPartition0, 0);
    verify(consumer).seek(topicPartition1, 0);
    verify(consumer, never()).commitSync(anyMap());
    container.stop();
    verify(pf, times(1)).createProducer();
}

From source file:org.springframework.kafka.listener.TransactionalContainerTests.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private void testConsumeAndProduceTransactionGuts(boolean chained, boolean handleError) throws Exception {
    Consumer consumer = mock(Consumer.class);
    final TopicPartition topicPartition = new TopicPartition("foo", 0);
    willAnswer(i -> {//from w w  w .jav  a  2 s.  c om
        ((ConsumerRebalanceListener) i.getArgument(1))
                .onPartitionsAssigned(Collections.singletonList(topicPartition));
        return null;
    }).given(consumer).subscribe(any(Collection.class), any(ConsumerRebalanceListener.class));
    ConsumerRecords records = new ConsumerRecords(Collections.singletonMap(topicPartition,
            Collections.singletonList(new ConsumerRecord<>("foo", 0, 0, "key", "value"))));
    final AtomicBoolean done = new AtomicBoolean();
    willAnswer(i -> {
        if (done.compareAndSet(false, true)) {
            return records;
        } else {
            Thread.sleep(500);
            return null;
        }
    }).given(consumer).poll(any(Duration.class));
    ConsumerFactory cf = mock(ConsumerFactory.class);
    willReturn(consumer).given(cf).createConsumer("group", "", null);
    Producer producer = mock(Producer.class);
    final CountDownLatch closeLatch = new CountDownLatch(2);
    willAnswer(i -> {
        closeLatch.countDown();
        return null;
    }).given(producer).close();
    ProducerFactory pf = mock(ProducerFactory.class);
    given(pf.transactionCapable()).willReturn(true);
    final List<String> transactionalIds = new ArrayList<>();
    willAnswer(i -> {
        transactionalIds.add(TransactionSupport.getTransactionIdSuffix());
        return producer;
    }).given(pf).createProducer();
    KafkaTransactionManager tm = new KafkaTransactionManager(pf);
    PlatformTransactionManager ptm = tm;
    if (chained) {
        ptm = new ChainedKafkaTransactionManager(new SomeOtherTransactionManager(), tm);
    }
    ContainerProperties props = new ContainerProperties("foo");
    props.setGroupId("group");
    props.setTransactionManager(ptm);
    final KafkaTemplate template = new KafkaTemplate(pf);
    props.setMessageListener((MessageListener) m -> {
        template.send("bar", "baz");
        if (handleError) {
            throw new RuntimeException("fail");
        }
    });
    KafkaMessageListenerContainer container = new KafkaMessageListenerContainer<>(cf, props);
    container.setBeanName("commit");
    if (handleError) {
        container.setErrorHandler((e, data) -> {
        });
    }
    container.start();
    assertThat(closeLatch.await(10, TimeUnit.SECONDS)).isTrue();
    InOrder inOrder = inOrder(producer);
    inOrder.verify(producer).beginTransaction();
    inOrder.verify(producer).sendOffsetsToTransaction(
            Collections.singletonMap(topicPartition, new OffsetAndMetadata(0)), "group");
    inOrder.verify(producer).commitTransaction();
    inOrder.verify(producer).close();
    inOrder.verify(producer).beginTransaction();
    ArgumentCaptor<ProducerRecord> captor = ArgumentCaptor.forClass(ProducerRecord.class);
    inOrder.verify(producer).send(captor.capture(), any(Callback.class));
    assertThat(captor.getValue()).isEqualTo(new ProducerRecord("bar", "baz"));
    inOrder.verify(producer).sendOffsetsToTransaction(
            Collections.singletonMap(topicPartition, new OffsetAndMetadata(1)), "group");
    inOrder.verify(producer).commitTransaction();
    inOrder.verify(producer).close();
    container.stop();
    verify(pf, times(2)).createProducer();
    verifyNoMoreInteractions(producer);
    assertThat(transactionalIds.get(0)).isEqualTo("group.foo.0");
    assertThat(transactionalIds.get(0)).isEqualTo("group.foo.0");
}

From source file:co.cask.cdap.internal.app.runtime.distributed.AbstractDistributedProgramRunner.java

/**
 * Adds a listener to the given TwillController to delete local temp files when the program has started/terminated.
 * The local temp files could be removed once the program is started, since Twill would keep the files in
 * HDFS and no long needs the local temp files once program is started.
 *
 * @return The same TwillController instance.
 */// ww w . j a  v a  2  s. c om
private TwillController addCleanupListener(TwillController controller, final File hConfFile,
        final File cConfFile, final Program program, final File programDir) {

    final AtomicBoolean deleted = new AtomicBoolean(false);
    controller.addListener(new ServiceListenerAdapter() {
        @Override
        public void running() {
            cleanup();
        }

        @Override
        public void terminated(Service.State from) {
            cleanup();
        }

        @Override
        public void failed(Service.State from, Throwable failure) {
            cleanup();
        }

        private void cleanup() {
            if (deleted.compareAndSet(false, true)) {
                LOG.debug("Cleanup tmp files for {}: {} {} {}", program.getName(), hConfFile, cConfFile,
                        program.getJarLocation().toURI());
                hConfFile.delete();
                cConfFile.delete();
                try {
                    program.getJarLocation().delete();
                } catch (IOException e) {
                    LOG.warn("Failed to delete program jar {}", program.getJarLocation().toURI(), e);
                }
                try {
                    FileUtils.deleteDirectory(programDir);
                } catch (IOException e) {
                    LOG.warn("Failed to delete program directory {}", programDir, e);
                }
            }
        }
    }, Threads.SAME_THREAD_EXECUTOR);
    return controller;
}

From source file:com.netflix.curator.framework.recipes.leader.TestLeaderLatch.java

@Test
public void testWaiting() throws Exception {
    final int PARTICIPANT_QTY = 10;

    ExecutorService executorService = Executors.newFixedThreadPool(PARTICIPANT_QTY);
    ExecutorCompletionService<Void> service = new ExecutorCompletionService<Void>(executorService);

    final Timing timing = new Timing();
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
            timing.session(), timing.connection(), new RetryOneTime(1));
    try {//from w w w  .j  a  v a2  s. c o m
        client.start();

        final AtomicBoolean thereIsALeader = new AtomicBoolean(false);
        for (int i = 0; i < PARTICIPANT_QTY; ++i) {
            service.submit(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    LeaderLatch latch = new LeaderLatch(client, PATH_NAME);
                    try {
                        latch.start();
                        Assert.assertTrue(latch.await(timing.forWaiting().seconds(), TimeUnit.SECONDS));
                        Assert.assertTrue(thereIsALeader.compareAndSet(false, true));
                        Thread.sleep((int) (10 * Math.random()));
                    } finally {
                        thereIsALeader.set(false);
                        latch.close();
                    }
                    return null;
                }
            });
        }

        for (int i = 0; i < PARTICIPANT_QTY; ++i) {
            service.take().get();
        }
    } finally {
        executorService.shutdown();
        IOUtils.closeQuietly(client);
    }
}

From source file:org.elasticsearch.client.sniff.SnifferTests.java

/**
 * Test that {@link Sniffer#close()} shuts down the underlying {@link Scheduler}, and that such calls are idempotent.
 * Also verifies that the next scheduled round gets cancelled.
 *//*w ww .  j  av  a2  s  .c o m*/
public void testClose() {
    final Future<?> future = mock(Future.class);
    long sniffInterval = randomLongBetween(1, Long.MAX_VALUE);
    long sniffAfterFailureDelay = randomLongBetween(1, Long.MAX_VALUE);
    RestClient restClient = mock(RestClient.class);
    final AtomicInteger shutdown = new AtomicInteger(0);
    final AtomicBoolean initialized = new AtomicBoolean(false);
    Scheduler scheduler = new Scheduler() {
        @Override
        public Future<?> schedule(Sniffer.Task task, long delayMillis) {
            if (initialized.compareAndSet(false, true)) {
                //run from the same thread so the sniffer gets for sure initialized and the scheduled task gets cancelled on close
                task.run();
            }
            return future;
        }

        @Override
        public void shutdown() {
            shutdown.incrementAndGet();
        }
    };

    Sniffer sniffer = new Sniffer(restClient, new MockHostsSniffer(), scheduler, sniffInterval,
            sniffAfterFailureDelay);
    assertEquals(0, shutdown.get());
    int iters = randomIntBetween(3, 10);
    for (int i = 1; i <= iters; i++) {
        sniffer.close();
        verify(future, times(i)).cancel(false);
        assertEquals(i, shutdown.get());
    }
}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerIntegration2Tests.java

@Test
public void testRestartConsumerOnBasicQosIoException() throws Exception {
    this.template.convertAndSend(queue.getName(), "foo");

    ConnectionFactory connectionFactory = new SingleConnectionFactory("localhost", BrokerTestUtils.getPort());

    final AtomicBoolean networkGlitch = new AtomicBoolean();

    class MockChannel extends PublisherCallbackChannelImpl {

        MockChannel(Channel delegate) {
            super(delegate);
        }// ww  w  .  j av  a  2  s . c  om

        @Override
        public void basicQos(int prefetchCount) throws IOException {
            if (networkGlitch.compareAndSet(false, true)) {
                throw new IOException("Intentional connection reset");
            }
            super.basicQos(prefetchCount);
        }

    }

    Connection connection = spy(connectionFactory.createConnection());
    when(connection.createChannel(anyBoolean()))
            .then(invocation -> new MockChannel((Channel) invocation.callRealMethod()));

    DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
    dfa.setPropertyValue("connection", connection);

    CountDownLatch latch = new CountDownLatch(1);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setMessageListener(new MessageListenerAdapter(new PojoListener(latch)));
    container.setQueueNames(queue.getName());
    container.setRecoveryInterval(500);
    container.afterPropertiesSet();
    container.start();

    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertTrue(networkGlitch.get());

    container.stop();
    ((DisposableBean) connectionFactory).destroy();
}

From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessMutexBase.java

@Test
public void testWaitingProcessKilledServer() throws Exception {
    final Timing timing = new Timing();
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
            new RetryOneTime(1));
    try {//from  w ww.j  a  v  a2 s. c  o m
        client.start();

        final CountDownLatch latch = new CountDownLatch(1);
        ConnectionStateListener listener = new ConnectionStateListener() {
            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.LOST) {
                    latch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);

        final AtomicBoolean isFirst = new AtomicBoolean(true);
        ExecutorCompletionService<Object> service = new ExecutorCompletionService<Object>(
                Executors.newFixedThreadPool(2));
        for (int i = 0; i < 2; ++i) {
            service.submit(new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    InterProcessLock lock = makeLock(client);
                    lock.acquire();
                    try {
                        if (isFirst.compareAndSet(true, false)) {
                            timing.sleepABit();

                            server.stop();
                            Assert.assertTrue(timing.awaitLatch(latch));
                            server = new TestingServer(server.getPort(), server.getTempDirectory());
                        }
                    } finally {
                        try {
                            lock.release();
                        } catch (Exception e) {
                            // ignore
                        }
                    }
                    return null;
                }
            });
        }

        for (int i = 0; i < 2; ++i) {
            service.take().get(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS);
        }
    } finally {
        IOUtils.closeQuietly(client);
    }
}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerIntegration2Tests.java

@Test
public void testRestartConsumerOnConnectionLossDuringQueueDeclare() throws Exception {
    this.template.convertAndSend(queue.getName(), "foo");

    ConnectionFactory connectionFactory = new CachingConnectionFactory("localhost", BrokerTestUtils.getPort());

    final AtomicBoolean networkGlitch = new AtomicBoolean();

    class MockChannel extends PublisherCallbackChannelImpl {

        MockChannel(Channel delegate) {
            super(delegate);
        }//  w  w w. jav  a  2  s  . co  m

        @Override
        public DeclareOk queueDeclarePassive(String queue) throws IOException {
            if (networkGlitch.compareAndSet(false, true)) {
                getConnection().close();
                throw new IOException("Intentional connection reset");
            }
            return super.queueDeclarePassive(queue);
        }

    }

    Connection connection = spy(connectionFactory.createConnection());
    when(connection.createChannel(anyBoolean()))
            .then(invocation -> new MockChannel((Channel) invocation.callRealMethod()));

    DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
    dfa.setPropertyValue("connection", connection);

    CountDownLatch latch = new CountDownLatch(1);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setMessageListener(new MessageListenerAdapter(new PojoListener(latch)));
    container.setQueueNames(queue.getName());
    container.setRecoveryInterval(500);
    container.afterPropertiesSet();
    container.start();

    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertTrue(networkGlitch.get());

    container.stop();
    ((DisposableBean) connectionFactory).destroy();
}