Example usage for java.util.concurrent.atomic AtomicReference wait

List of usage examples for java.util.concurrent.atomic AtomicReference wait

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicReference wait.

Prototype

public final native void wait(long timeoutMillis) throws InterruptedException;

Source Link

Document

Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

Usage

From source file:io.fabric8.agent.download.DownloadManagerTest.java

private StreamProvider download(DownloadManager manager, String url) throws Exception {
    final AtomicReference<StreamProvider> ref = new AtomicReference<>();
    Downloader downloader = manager.createDownloader();
    downloader.download(url, new DownloadCallback() {
        @Override/*w w  w  .  j  a  va  2 s  .com*/
        public void downloaded(StreamProvider provider) throws Exception {
            synchronized (ref) {
                ref.set(provider);
                ref.notifyAll();
            }
        }
    });
    synchronized (ref) {
        ref.wait(30000);
        return ref.get();
    }
}

From source file:com.sybase365.mobiliser.custom.project.handlers.authentication.GtalkAuthenticationHandler.java

@Override
public void authenticate(final SubTransaction transaction, final String authToken, final boolean payer)
        throws AuthenticationException {

    lazyInit();// w  ww.j  av  a 2 s  .  com

    final Customer customer;
    final String otherName;
    if (payer) {
        customer = transaction.getTransaction().getPayer();
        otherName = transaction.getTransaction().getPayee().getDisplayName();
    } else {
        customer = transaction.getTransaction().getPayee();
        otherName = transaction.getTransaction().getPayer().getDisplayName();
    }

    final String email = this.notificationLogic.getCustomersEmail(customer, 0).get(0);

    final AtomicReference<String> userResponse = new AtomicReference<String>();

    final ChatManager chatmanager = this.connection.getChatManager();

    final MessageListener messageListener = new MessageListener() {

        @Override
        public void processMessage(Chat chat, Message message) {
            synchronized (userResponse) {
                LOG.info("got user response: " + message.getBody());
                userResponse.set(message.getBody());
                userResponse.notify();
            }
        }

    };

    final Chat newChat = chatmanager.createChat(email, messageListener);

    try {
        try {
            newChat.sendMessage("Please confirm payment to " + otherName + " by entering yes.");
        } catch (final XMPPException e) {
            LOG.info("can not send message to user " + email + ": " + e.getMessage(), e);

            throw new AuthenticationFailedPermanentlyException(StatusCodes.ERROR_IVR_NO_PICKUP);
        }

        synchronized (userResponse) {
            try {
                userResponse.wait(10000);
            } catch (final InterruptedException e) {
                LOG.info("Interrupted while waiting", e);
                Thread.currentThread().interrupt();
            }
        }

        if (userResponse.get() == null) {
            LOG.info("did not receive a response in time.");

            throw new AuthenticationFailedPermanentlyException("did not receive a response in time.",
                    StatusCodes.ERROR_IVR_NO_USER_INPUT, new HashMap<String, String>());
        }

        if ("yes".equalsIgnoreCase(userResponse.get())) {

            LOG.debug("received confirmation.");

            try {
                newChat.sendMessage("Thank You.");
            } catch (final XMPPException e1) {
                LOG.debug("Thank you Response was not sent. Ignored", e1);
            }

            return;
        }

        LOG.debug("received unknown response: " + userResponse.get());

        try {
            newChat.sendMessage("Transaction will be cancelled.");
        } catch (final XMPPException e2) {
            LOG.debug("Response was not sent. Ignored", e2);
        }

        throw new AuthenticationFailedPermanentlyException(StatusCodes.ERROR_IVR_HANGUP);

    } finally {
        newChat.removeMessageListener(messageListener);
    }

}