how do i prioritize code to look at 3 different queue

时间:2016-02-03 04:23:45

标签: multithreading jms spring-integration spring-jms

We have a requirement where we will have messages coming in 3 different queues.

I need to write code such that messages from Queue A are given higher priority over Queue B followed by Queue C.

However I cannot keep any of the Queue waiting for too long so there should be some dedicated receivers for each thread.

Can you please suggest any existing framework that can do this for me?

A possible solution is a higher number of dedicated receivers for queue A that also look at B and C if there are no messages in A.

A slightly lesser number of dedicated receivers for Queue B that also look at A and C if there are no messages in B.

A very few dedicated receivers for Queue C that also look at A and B if there are no messages in C.

Is it possible to implement this solution at JMS consumer\receiver level or Do I need to write custom code for it?

3 个答案:

答案 0 :(得分:0)

JMS无法控制消息处理的优先级。我建议在任务中转换每个消息(当它到达时立即),并将任务提交给优先执行的Executor。见Java Executors: how can I set task priority?

答案 1 :(得分:0)

如果您控制队列(例如,编写代码可以有您提供的队列引用),那么您将放置一个PriorityBlockingQueue,其中包含一个对A,B,C进行排序的比较器。

如果你无法避免3个队列(因为你只得到队列引用来读取),那么你不幸的是必须轮询每个队列,而不是take()。但是你不能全速旋转并且必须等待,所以我认为只要你的最小响应时间允许服务B和C队列(如果A,那么它会很大)我应该对A队列采取(超时)。永远有优先权)。如果B和C队列当然是空的,你只能调用A.take()(但如果你不知道队列实现,就不要依赖.size();只要相信最后一次轮询()你刚试过的结果。)

当然你可以旋转3个线程来简单地取()并放入你控制的单个优先级队列。但这有点矫枉过正。

答案 2 :(得分:0)

在JMS消息上使用JMSPriority属性,将消息转储到同一队列中,让提供程序完成优先级的工作。