You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by DanielR <ro...@gmail.com> on 2009/03/19 19:23:00 UTC

Problem trying to route with Interceptors

I have a main queue named QUEUE.COLOR. 
1 Producer send a lot of messages to QUEUE.COLOR
Messages only have "red ..." or "blue..." in their body

In trying to route the messages with "red ..." to QUEUE.RED and the messages
with "blue..." to QUEUE.BLUE ONLY USING AN INTERCEPTOR

My problem: messages are randomly dispatched to QUEUE.RED or QUEUE.BLUE

I have inspected some messages in QUEUE.RED and have their Destination
property set to "queue://QUEUE.BLUE"   that's weird...


Any clues? corrections? hints? codes?


Best Regards, DR.



This is my code:

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.broker.Broker;
import org.apache.activemq.broker.BrokerFilter;
import org.apache.activemq.broker.BrokerPlugin;
import org.apache.activemq.broker.ProducerBrokerExchange;
import org.apache.activemq.command.Message;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.broker.ConnectionContext;
import org.apache.activemq.broker.region.MessageReference;
import org.apache.activemq.command.TransactionId;
import org.apache.activemq.command.ActiveMQDestination;

import javax.jms.TextMessage; 
import javax.jms.Session;
import javax.jms.Connection;
import javax.jms.Queue;


public class MsgInterceptorPluginRouter2 extends BrokerFilter implements
BrokerPlugin {	

	public MsgInterceptorPluginRouter2() {
		super(null);
	}

	public MsgInterceptorPluginRouter2(Broker next) {
		super(next);
	}

	public Broker installPlugin(Broker broker) throws Exception {
		return new MsgInterceptorPluginRouter2(broker);
	}

	public void send(ProducerBrokerExchange producerExchange, Message
messageSend) throws Exception{
		String content = ((TextMessage) messageSend).getText().substring(0, 30) +
"...";
		
		String substr = content.substring(0,4);
	
		if (substr.matches("red\\s*")) {
			messageSend.setDestination(this.getDestinations()[2]);
		}
		else if (substr.matches("blue\\s*")) {
			messageSend.setDestination(this.getDestinations()[7]);
		}

		super.send(producerExchange, messageSend);
	}
	
}


-- 
View this message in context: http://www.nabble.com/Problem-trying-to-route-with-Interceptors-tp22606463p22606463.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Problem trying to route with Interceptors

Posted by Andreas Gies <ag...@progress.com>.
Also attaching the Java class.

Andreas




-------------------------------------------------------
Progress Software GmbH
Sitz der Gesellschaft: Agrippinawerft 26, 50678 Koeln;
Niederlassung: Fuerstenrieder Str. 279, 81377 Muenchen
Amtsgericht Koeln, HRB 15620; 
Geschaeftsfuehrung: David Ireland
-------------------------------------------------------

Re: Problem trying to route with Interceptors

Posted by Andreas Gies <ag...@progress.com>.
Hi again ,

Well, I am not a Camel expert and don't know all the components from  
the top of my head.
What I can say is that what you are trying to achieve would be  
possible with a very simple
camel processor.

Perhaps it is even simpler to extract the color from the message body  
in an even simpler
Processor, populate a message property and then use an out of the box  
camel content
based router.

The architectural difference is that you keep the messaging layer  
clean from routing definitions.

I have attached one my examples. You can see from there that it is  
possible to write a processor
that looks into the message body. My code is slightly more complex as  
I am adding Endpoints
on the fly in case my environment is dynamically growing. In your case  
the code would be
simpler as you have a limited number of destinations.

I am also attaching the corresponding spring definitions.


Best regards
Andreas





-------------------------------------------------------
Progress Software GmbH
Sitz der Gesellschaft: Agrippinawerft 26, 50678 Koeln;
Niederlassung: Fuerstenrieder Str. 279, 81377 Muenchen
Amtsgericht Koeln, HRB 15620; 
Geschaeftsfuehrung: David Ireland
-------------------------------------------------------

Re: Problem trying to route with Interceptors

Posted by DanielR <ro...@gmail.com>.
Hi.

I need to use interceptors because I need to inspect the body of the
message. My filters are based on some characters of the body.  With camel,
you can only  check if some string is contained in the body (may be i'm
wrong?)



Andreas Gies-3 wrote:
> 
> Hi there,
> 
> Apart from the question why you want to do such a thin in an  
> interceptor instead of
> using virtual destinations or a camel route....
> 
> Could you share your activemq.xml to let us see how you hooked in the  
> plugin in the broker ?
> 
> Perhaps you could include some logging statements in your code and  
> understand what is executed.
> You could also attach a remote debugger to Activemq and debug through  
> your code.
> 
> 
> Best regards
> Andreas
> 
> On Mar 23, 2009, at 5:35 PM, DanielR wrote:
> 
>>
>> any ideas?
>>
>>
>>
>> DanielR wrote:
>>>
>>> I have a main queue named QUEUE.COLOR.
>>> 1 Producer send a lot of messages to QUEUE.COLOR
>>> Messages only have "red ..." or "blue..." in their body
>>>
>>> In trying to route the messages with "red ..." to QUEUE.RED and the
>>> messages with "blue..." to QUEUE.BLUE ONLY USING AN INTERCEPTOR
>>>
>>> My problem: messages are randomly dispatched to QUEUE.RED or  
>>> QUEUE.BLUE
>>>
>>> I have inspected some messages in QUEUE.RED and have their  
>>> Destination
>>> property set to "queue://QUEUE.BLUE"   that's weird...
>>>
>>>
>>> Any clues? corrections? hints? codes?
>>>
>>>
>>> Best Regards, DR.
>>>
>>>
>>>
>>> This is my code:
>>>
>>> import org.apache.activemq.ActiveMQConnectionFactory;
>>> import org.apache.activemq.ActiveMQSession;
>>> import org.apache.activemq.ActiveMQConnection;
>>> import org.apache.activemq.broker.Broker;
>>> import org.apache.activemq.broker.BrokerFilter;
>>> import org.apache.activemq.broker.BrokerPlugin;
>>> import org.apache.activemq.broker.ProducerBrokerExchange;
>>> import org.apache.activemq.command.Message;
>>> import org.apache.activemq.command.ActiveMQQueue;
>>> import org.apache.activemq.broker.ConnectionContext;
>>> import org.apache.activemq.broker.region.MessageReference;
>>> import org.apache.activemq.command.TransactionId;
>>> import org.apache.activemq.command.ActiveMQDestination;
>>>
>>> import javax.jms.TextMessage;
>>> import javax.jms.Session;
>>> import javax.jms.Connection;
>>> import javax.jms.Queue;
>>>
>>>
>>> public class MsgInterceptorPluginRouter2 extends BrokerFilter  
>>> implements
>>> BrokerPlugin {	
>>>
>>> 	public MsgInterceptorPluginRouter2() {
>>> 		super(null);
>>> 	}
>>>
>>> 	public MsgInterceptorPluginRouter2(Broker next) {
>>> 		super(next);
>>> 	}
>>>
>>> 	public Broker installPlugin(Broker broker) throws Exception {
>>> 		return new MsgInterceptorPluginRouter2(broker);
>>> 	}
>>>
>>> 	public void send(ProducerBrokerExchange producerExchange, Message
>>> messageSend) throws Exception{
>>> 		String content = ((TextMessage)  
>>> messageSend).getText().substring(0, 30)
>>> + "...";
>>> 		
>>> 		String substr = content.substring(0,4);
>>> 	
>>> 		if (substr.matches("red\\s*")) {
>>> 			messageSend.setDestination(this.getDestinations()[2]);
>>> 		}
>>> 		else if (substr.matches("blue\\s*")) {
>>> 			messageSend.setDestination(this.getDestinations()[7]);
>>> 		}
>>>
>>> 		super.send(producerExchange, messageSend);
>>> 	}
>>> 	
>>> }
>>>
>>>
>>>
>>
>> -- 
>> View this message in context:
>> http://www.nabble.com/Problem-trying-to-route-with-Interceptors-tp22606463p22663866.html
>> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>>
> 
> ---
> Mit freundlichen Grüssen - Kind Regards
> Andreas Gies
> Principal Consultant
> Open Source Center of Competence
> 
> Progress Software GmbH
> Agrippinawerft 26
> 50678 Köln
> 
> E-Mail      	agies@progress.com
> Direct Line 	+49 (0)9953 980349
> Mobile      	+49 (0)170 5759611
> Skype        	+44 (0)20 3239 2922
> Skype       	+353 (0)1 443 4971
> Skype       	+1 (0)781 262 0168
> 
> http://www.progress.com
> http://fusesource.com
> http://open-source-adventures.blogspot.com
> 
> 
> 
> -------------------------------------------------------
> Progress Software GmbH
> Sitz der Gesellschaft: Agrippinawerft 26, 50678 Koeln;
> Niederlassung: Fuerstenrieder Str. 279, 81377 Muenchen
> Amtsgericht Koeln, HRB 15620;
> Geschaeftsfuehrung: David Ireland
> -------------------------------------------------------
> 
> 

-- 
View this message in context: http://www.nabble.com/Problem-trying-to-route-with-Interceptors-tp22606463p22665980.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Re: Problem trying to route with Interceptors

Posted by Andreas Gies <ag...@progress.com>.
Hi there,

Apart from the question why you want to do such a thin in an  
interceptor instead of
using virtual destinations or a camel route....

Could you share your activemq.xml to let us see how you hooked in the  
plugin in the broker ?

Perhaps you could include some logging statements in your code and  
understand what is executed.
You could also attach a remote debugger to Activemq and debug through  
your code.


Best regards
Andreas

On Mar 23, 2009, at 5:35 PM, DanielR wrote:

>
> any ideas?
>
>
>
> DanielR wrote:
>>
>> I have a main queue named QUEUE.COLOR.
>> 1 Producer send a lot of messages to QUEUE.COLOR
>> Messages only have "red ..." or "blue..." in their body
>>
>> In trying to route the messages with "red ..." to QUEUE.RED and the
>> messages with "blue..." to QUEUE.BLUE ONLY USING AN INTERCEPTOR
>>
>> My problem: messages are randomly dispatched to QUEUE.RED or  
>> QUEUE.BLUE
>>
>> I have inspected some messages in QUEUE.RED and have their  
>> Destination
>> property set to "queue://QUEUE.BLUE"   that's weird...
>>
>>
>> Any clues? corrections? hints? codes?
>>
>>
>> Best Regards, DR.
>>
>>
>>
>> This is my code:
>>
>> import org.apache.activemq.ActiveMQConnectionFactory;
>> import org.apache.activemq.ActiveMQSession;
>> import org.apache.activemq.ActiveMQConnection;
>> import org.apache.activemq.broker.Broker;
>> import org.apache.activemq.broker.BrokerFilter;
>> import org.apache.activemq.broker.BrokerPlugin;
>> import org.apache.activemq.broker.ProducerBrokerExchange;
>> import org.apache.activemq.command.Message;
>> import org.apache.activemq.command.ActiveMQQueue;
>> import org.apache.activemq.broker.ConnectionContext;
>> import org.apache.activemq.broker.region.MessageReference;
>> import org.apache.activemq.command.TransactionId;
>> import org.apache.activemq.command.ActiveMQDestination;
>>
>> import javax.jms.TextMessage;
>> import javax.jms.Session;
>> import javax.jms.Connection;
>> import javax.jms.Queue;
>>
>>
>> public class MsgInterceptorPluginRouter2 extends BrokerFilter  
>> implements
>> BrokerPlugin {	
>>
>> 	public MsgInterceptorPluginRouter2() {
>> 		super(null);
>> 	}
>>
>> 	public MsgInterceptorPluginRouter2(Broker next) {
>> 		super(next);
>> 	}
>>
>> 	public Broker installPlugin(Broker broker) throws Exception {
>> 		return new MsgInterceptorPluginRouter2(broker);
>> 	}
>>
>> 	public void send(ProducerBrokerExchange producerExchange, Message
>> messageSend) throws Exception{
>> 		String content = ((TextMessage)  
>> messageSend).getText().substring(0, 30)
>> + "...";
>> 		
>> 		String substr = content.substring(0,4);
>> 	
>> 		if (substr.matches("red\\s*")) {
>> 			messageSend.setDestination(this.getDestinations()[2]);
>> 		}
>> 		else if (substr.matches("blue\\s*")) {
>> 			messageSend.setDestination(this.getDestinations()[7]);
>> 		}
>>
>> 		super.send(producerExchange, messageSend);
>> 	}
>> 	
>> }
>>
>>
>>
>
> -- 
> View this message in context: http://www.nabble.com/Problem-trying-to-route-with-Interceptors-tp22606463p22663866.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>

---
Mit freundlichen Grüssen - Kind Regards
Andreas Gies
Principal Consultant
Open Source Center of Competence

Progress Software GmbH
Agrippinawerft 26
50678 Köln

E-Mail      	agies@progress.com
Direct Line 	+49 (0)9953 980349
Mobile      	+49 (0)170 5759611
Skype        	+44 (0)20 3239 2922
Skype       	+353 (0)1 443 4971
Skype       	+1 (0)781 262 0168

http://www.progress.com
http://fusesource.com
http://open-source-adventures.blogspot.com



-------------------------------------------------------
Progress Software GmbH
Sitz der Gesellschaft: Agrippinawerft 26, 50678 Koeln;
Niederlassung: Fuerstenrieder Str. 279, 81377 Muenchen
Amtsgericht Koeln, HRB 15620; 
Geschaeftsfuehrung: David Ireland
-------------------------------------------------------

Re: Problem trying to route with Interceptors

Posted by DanielR <ro...@gmail.com>.
any ideas?



DanielR wrote:
> 
> I have a main queue named QUEUE.COLOR. 
> 1 Producer send a lot of messages to QUEUE.COLOR
> Messages only have "red ..." or "blue..." in their body
> 
> In trying to route the messages with "red ..." to QUEUE.RED and the
> messages with "blue..." to QUEUE.BLUE ONLY USING AN INTERCEPTOR
> 
> My problem: messages are randomly dispatched to QUEUE.RED or QUEUE.BLUE
> 
> I have inspected some messages in QUEUE.RED and have their Destination
> property set to "queue://QUEUE.BLUE"   that's weird...
> 
> 
> Any clues? corrections? hints? codes?
> 
> 
> Best Regards, DR.
> 
> 
> 
> This is my code:
> 
> import org.apache.activemq.ActiveMQConnectionFactory;
> import org.apache.activemq.ActiveMQSession;
> import org.apache.activemq.ActiveMQConnection;
> import org.apache.activemq.broker.Broker;
> import org.apache.activemq.broker.BrokerFilter;
> import org.apache.activemq.broker.BrokerPlugin;
> import org.apache.activemq.broker.ProducerBrokerExchange;
> import org.apache.activemq.command.Message;
> import org.apache.activemq.command.ActiveMQQueue;
> import org.apache.activemq.broker.ConnectionContext;
> import org.apache.activemq.broker.region.MessageReference;
> import org.apache.activemq.command.TransactionId;
> import org.apache.activemq.command.ActiveMQDestination;
> 
> import javax.jms.TextMessage; 
> import javax.jms.Session;
> import javax.jms.Connection;
> import javax.jms.Queue;
> 
> 
> public class MsgInterceptorPluginRouter2 extends BrokerFilter implements
> BrokerPlugin {	
> 
> 	public MsgInterceptorPluginRouter2() {
> 		super(null);
> 	}
> 
> 	public MsgInterceptorPluginRouter2(Broker next) {
> 		super(next);
> 	}
> 
> 	public Broker installPlugin(Broker broker) throws Exception {
> 		return new MsgInterceptorPluginRouter2(broker);
> 	}
> 
> 	public void send(ProducerBrokerExchange producerExchange, Message
> messageSend) throws Exception{
> 		String content = ((TextMessage) messageSend).getText().substring(0, 30)
> + "...";
> 		
> 		String substr = content.substring(0,4);
> 	
> 		if (substr.matches("red\\s*")) {
> 			messageSend.setDestination(this.getDestinations()[2]);
> 		}
> 		else if (substr.matches("blue\\s*")) {
> 			messageSend.setDestination(this.getDestinations()[7]);
> 		}
> 
> 		super.send(producerExchange, messageSend);
> 	}
> 	
> }
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Problem-trying-to-route-with-Interceptors-tp22606463p22663866.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.