Java- how to constantly wait for events

I am creating an application in which there are several steps. At each stage, the message is sent to my turn in Amazon Simple Work Flow (SWF) ... The application must start a new stream for each message received.

How to implement the pending part - so that the application constantly looks at the queue for new messages and takes action when the message is received?

+5
source share
4 answers

What you are asking is called the producer-consumer model. You can read about it here.

The main idea: the site is a manufacturer, and you are a customer customer.

you wait () until the listener receives a message, and then notifAll ()

    class WaitForAmazon{
    private boolean available = false;
    private int contents;

    public synchronized int consumer() {
        while (available == false) {
            try {
                wait();
            } catch (InterruptedException e) { }
        }
        available = false;
        notifyAll();
        return contents;
    }

    public synchronized void producer(int value) {
        while (available == true) {
            try {
                wait();
            } catch (InterruptedException e) { }
        }
        contents = value;
        available = true;
        notifyAll();
    }
}
+2
source

BlockingQueue LinkedBlockingQueue .

Thread take - ,

queue.take() 

, , - ,

queue.put(something). 

, , queue.put() , .

+2

: poll queue, while loop mechan.once, , .

0

:

serversocket

, .accept() , - . , , (, ..).

0

All Articles