Home arrow static arrow Java Programming [Archive] - creating a work queue
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - creating a work queue
This topic has 6 replies on 1 page.

Posts:86
Registered: 11/16/01
creating a work queue  
Jul 27, 2004 7:19 AM



 
Hello everyone,

I need some quick pointers on an architecture for a work queue.

I have a connection class, which has a socket binded to a server. I RECEIVE work packages from the server, and have to SEND back the results over the same socket. There is quite a bit of processing involved, and I receive about 2 - 3 million such packages per day.

In the connection class I have a NIO socket that does the receiving, and I would like to pass all the packages to a non-coupled handler. The handler must then take these packages and create a thread to process each request. When the result comes back to the handler, it must pass it back to the connection class, and using the same socket that I am receiving on, must send the response back.

Now I need queues between the connection class and the handler class for receiving and sending. Sometimes I receive packages too quickly to process and must queue them. I would also like to queue the responses that go back to the connection class.

How can I create a "standalone" queue that each class can just go look at and retrieve\put work, but abstract enough that each class does not have to instantiate it. Ideally, I don't want the handler to know about the connection class and vice versa.

Any pointers to a simple patter \ architecture would be welcome.

Thanks!!!

Brendan

 

Posts:349
Registered: 1/8/04
Re: creating a work queue  
Jul 27, 2004 7:50 AM (reply 1 of 6)



 
You may want to take a look at the command pattern. Google has many, many links.

The command pattern is all about queueing and processing commands. It's not at all unlike what you are describing. It has nothing to do with networking, though (not directly anyway), so you can handle that issue separately.
 

Posts:31,095
Registered: 4/30/99
Re: creating a work queue  
Jul 27, 2004 8:00 AM (reply 2 of 6)



 
For "architecture" you could use a LinkedList. The producer (the connection class) adds tasks to the end of the list, and the consumer (the handler class) removes them from the beginning of the list. Suitable synchronization is required of course.
 

Posts:49
Registered: 5/6/04
Re: creating a work queue  
Jul 27, 2004 8:20 AM (reply 3 of 6)



 
This might work for you:
http://www.developer.com/java/data/article.php/10932_3296821_1

Also, in 1.5, er 5.0, a Queue becomes part of the Collections interface:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Queue.html
 

Posts:86
Registered: 11/16/01
Re: creating a work queue  
Jul 28, 2004 1:31 AM (reply 4 of 6)



 
Thanks,

It's not the actual queing that is giving me problems, I have that sorted out.

The real issue I have is creating a class that is totally decoupled from the other class. How would such a class look?

Thanks
 

Posts:3,081
Registered: 2/15/99
Re: creating a work queue  
Jul 28, 2004 3:53 AM (reply 5 of 6)



 
I'm not sure I see the problem...
public class WorkQueue{    // If you prefer, make everything non-static and stash a static WorkQueue somewhere    private static LinkedList queue = new LinkedList();     // Work receiver thread calls this    public static void addWork(WorkItemOrSomething work)    {        synchronized (queue) {            queue.add(work);            queue.notify();        }    }     // Worker thread calls this; waits if no work available    public static WorkItemOrSomething getWork()    {        synchronized (queue) {            while (queue.isEmpty()) {                try {                    queue.wait();                } catch (InterruptedException e) { /* ignore */ }            }            return (WorkItemOrSomething) queue.removeFirst();        }    }}

For a real industrial strength solution, you shouldn't use an in-memory data structure, but rather a database or JMS. An in-memory implementation loses work and results if there is a crash, power outage, ...
 

Posts:3,081
Registered: 2/15/99
Re: creating a work queue  
Jul 28, 2004 4:01 AM (reply 6 of 6)



 
The handler must then take these packages
and create a thread to process each request.

One thing to consider: imagine you get a sudden burst of 1000 work requests. Do you want to:

- Start 1000 threads, one for each request (ouch!)

- Start 10 threads, and at any time have only the 10 threads doing work (easy: start the 10 threads at program startup, and have all call WorkQueue.getWork() above. The threads will automatically "compete" for the work items, and notify() will randomly pick one of the threads as the lucky winner.)

- If work is CPU-bound, perhaps just have the "handler thread" do it directly, with no extra threads.
 
This topic has 6 replies on 1 page.