I have developed the following program/architecture:
A) A Java servlet receives POST requests, gets the Parameters from the POST requests and stores them in a public static LinkedList:
public static LinkedList incomingQueue = new LinkedList<myObjects>();
That is, for every POST request I do this:
incomingQueue.push(myObject);
Now, I want to periodically access the Queue and perform processing on the Objects:
while(true){
doProcessing(incomingQueue);
wait(someTime);
}
Obviously, I don’t have a main class to do this. How do I create such a class that has access to the incomingQueue without being triggered by the servlet? What is the correct architecture to do this?
Thank you for your time.
First of all the queue should be placed in servlet context attributes (see:
ServletContext.setAttribute(). Also access to this queue must be synchronized, considerArrayBlockingQueue.In plain servlets you can use
ServletContextListenerby starting a thread incontextInitialized()and interrupting it incontextDestroyed.If you are using spring you can use
@Scheduledannotation, in ejb:TimerServiceor@Schedule.Finally there is a
Timerclass in standard Java. Last but not least, have a look at jms, it might be a better choice in your situation.