Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Soft Queue Implementations

 
Joe Joseph
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a situation that requires me to develop a 'Soft Queue'.
First let me explain my requirement.
The system under consideration has some 'text' messages flowing at a rate of about 1 msg/sec. This is a critical process & i need to log any failures to processes these msgs to a table. At the same time i do not want to affect the 1 msg/sec rate. Thus i would like to decouple the Message processing process from the Error Message logging process. Also i cannot use JMS Queues or other related Queues.
What i require is that the Message Processing method should put the failed text messages in some sort of Queue. The job of Message Processing method ends there. I need a mechanism by which the Error Message logging process will be notified of this & it will log the error in the table. Thus the Message Processing method is not held up till the Error Msg is logged.
This is some sort of Consumer-Producer problem & Observerable pattern.
Please let me know how i can go about this?

Regards,
Joe
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can put your error message in a
java.util.concurrent.ConcurrentLinkedQueue
The queue is FIFO, thread safe and fast.

You can notify the message writter thread in a similar fashion to the example given in the interface
java.util.concurrent.locks.Condition.

At start up, create the Queue. Create the message writer thread passing a reference to the Queue.

When you have an error message, put it in the Queue and signal() the message writer thread.

The thread await() until there is work. It then retrieves each message until there are no more. The thread goes back to await().
 
Joe Joseph
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Forgot to mention that i have to use jdk 1.4.2.
I cannot use later verions.
java.util.concurrent.ConcurrentLinkedQueue is in 1.5
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Find the blocking queue in the Commons Thread Pool. It's very simple and ought to drop right into what you need. The log writer would block on a get message call rather than waiting for a notification. If you don't like that bit we can try to change it around to what you need.
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can't use 1.5, then do it yourself.

Set up a Vector (its synchronized) in place of a 1.5 Queue. Pass a reference to the message writer thread.

In place of Condition await()/signal() use Object wait()/notify(). There are plenty of examples of this floating around the web.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!