Processing of JMS Messages with Different Priority using Apache Camel

1.3k views Asked by At

I'm solving the problem of processing JMS messages with different priority. I use Apache Camel framework as an EIP implementor.I have two queues which I must consume from. The first contains messages with higher priority, the second one messages with lower priority.

Now I'd like to create a special component 'mixer' containing loop doing following steps:

  1. look in the priority queue
    • if it contains a message, it will send it to the output queue and go to the start of a loop (if it contains a lot of messages, it will process the first 10 messages and then go to the step 2)
    • if it does not contain any messages, go to the step 2
  2. look in the second queue
    • if it contains a message, send it to the output queue (but ONLY one message) and repeat the loop
    • if it does not contain any messages, repeat the loop

As you can see, I would like to keep a special ratio if there will be a lot of messages (10 from priority queue / 1 from the second one). If there is no message in priority queue, we can immediately process the messages in the second queue. I would like to have something like EIP Resequencer that will consume from multiple input queues.

I looked for a Camel's route where I would consume from two queues where I would have a my component "Mixer" described above. I would like to have something like that:

<route>
  <from id="A" />
  <from id="B" />
  <resequence id="mixer" />
  <to id="C" />
</route>

But I didn't find a way how to do it. One route can have only one input. If it has more inputs, Camel will internally duplicate the route (we will have two independent routes) and the behavior will be following:

<route>
  <from id="A" />
  <resequence id="mixer" />
  <to id="C" />
</route>

<route>
  <from id="B" />
  <resequence id="mixer" />
  <to id="C" />
</route>

as descibed here. And that is not what I want :-(. Do you have any ideas how to solve my problem? Thanks in advance!

1

There are 1 answers

0
vikingsteve On BEST ANSWER

Perhaps you could look at the Polling Consumer EIP.

I am showing some example code from the camel website, which may be the basis for your solution:

public void someBusinessLogic() {
    // loop to empty queue
    while (true) {
        // receive the message from the queue, wait at most 3 sec
        String msg = consumer.receiveBody("activemq:queue.inbox", 3000, String.class);
        if (msg == null) {
            // no more messages in queue
            break;
        }

        // do something with body
        msg = "Hello " + msg;

        // send it to the next queue
        producer.sendBodyAndHeader("activemq:queue.foo", msg, "number", count++);
    }
}

You can try to modify this code to have a counter which tries to consume 10 messages from your hi-pri queue and 1 message from your lo-pri queue.

Have fun and good luck. Interested to know if this succeeds or not :)