Publisher send some ObjectMessage(any pojo) in to the queue. let there are 5 message in queue.
When Consumer is wake up it reads the queue and sort all the queue objects (on the basis of some property of pojo class i.e entry_date, priority_level) and pick the top one and process and leave the rest in to the queue. And do the above thing again and again until the queue is empty.

So my question is that is it possible to implements?if yes the give some way.

Publisher send some ObjectMessage(any pojo) in to the queue. let there are 5 message in queue.
When Consumer is wake up it reads the queue and sort all the queue objects (on the basis of some property of pojo class i.e entry_date, priority_level) and pick the top one and process and leave the rest in to the queue. And do the above thing again and again until the queue is empty.

So my question is that is it possible to implements?if yes the give some way.

You can't in general consume arbitrary messages from the middle of a queue. It's a queue and is designed for messages to be delivered in order.

However the JMS "message selector" feature may be worth investigating. This allows you to define a filtered view of the queue on the basis of various message header properties. Please consult any JMS textbook or your product documentation for more information.

nigeldeakin wrote:
You can't in general consume arbitrary messages from the middle of a queue.

The OP does specifically state taking from the top of the queue, not "in the middle" as you suggest. I think the basic question is how to sort messages on the queue and only process one message at a time. Consuming messages synchronously seems a solution to the latter requirement, like this basic tutorial demonstrates:

http://java.sun.com/developer/technicalArticles/Ecommerce/jms/

But that does not cover sorting. I'm not too familiar with JMS at that level, but I believe that is something that would be a vendor specific feature and in general not a good requirement to have when messaging is involved. The power of the system is in the ability to deal with messages in any order, synchronously or asynchronously.

nigeldeakin wrote:
You can't in general consume arbitrary messages from the middle of a queue.

The OP does specifically state taking from the top of the queue, not "in the middle" as you suggest. I think the basic question is how to sort messages on the queue and only process one message at a time. Consuming messages synchronously seems a solution to the latter requirement, like this basic tutorial demonstrates:

But that does not cover sorting. I'm not too familiar with JMS at that level, but I believe that is something that would be a vendor specific feature and in general not a good requirement to have when messaging is involved. The power of the system is in the ability to deal with messages in any order, synchronously or asynchronously.

If the OP wants to change the order of messages on a queue prior to consuming them then this isn't a feature provided by JMS.

Not by the specification, no. But there are multiple implementations out there with vendor specific features. Perhaps something can be found there, but that doesn't change the fact that it is probably not a good idea to need something like that. It smells like an architectural mistake.

I find it pretty hard to believe that any vendor would supply an operation that sorted a queue in situ. What happens if another message arrives during the sort? or after the sort but before the client takes off the first message? It just cannot work correctly, because it can't be atomic.

Well, it would just require inserting new objects into the right place into the queue, which is hardly "sorting". But yeah, it would have to lock out both reads by consumers and writes by producers while it was doing that insertion.

However I find it hard to believe that any vendor would provide this feature strictly for ObjectMessages and allow comparison based on properties of the enclosed objects. Sounds too unlikely for even the IBMs of the world to contemplate.

No, the OP is talking about sorting existing messages in situ, not ordered insertion. It was suggested that a vendor might provide such a thing. I don't believe it is even feasible let alone desirable.

Maybe the OP should just reconsider (or consider) his notion of queue priority.

EJP wrote:
No, the OP is talking about sorting existing messages in situ, not ordered insertion. It was suggested that a vendor might provide such a thing. I don't believe it is even feasible let alone desirable.

In partial defense, I base my hasty conclusions on a Google search pointing at JBoss messaging implementations that can do at least some form of sorting, but I didn't investigate through to actually be able to claim that what I found relates in any way to this thread. My main point was to investigate implementation specific features when you need to do something that the API doesn't provide.