Hi,

I needed to route a message from one ActiveMQ broker to one or more remote
ActiveMQ brokers based on some runtime information.

Lets say I've broker "central" and remote brokers "remote1", "remote2", ...
"remoteN".
The number of remote brokers can change over time and I wouldn't want to
configure any beans or any kind of configuration in the activemq.xml of the
"central" broker.

The following what I tried, but it seems to be sending the message always to
the first "remote" server that I used. Couple of points:

0. I putup a POJO  bean with RecipientList EIP.
1. topic:Out.RemoteServers is the topic on the "central" broker
2. topic:In.LocalServer is the topic on any "remote" broker.
3. All applications that post messages to "central" broker add a message
header with name "IPAddesses". The header value contain a list of IP
addresses corresponding to the "remote" brokers.
4. I'm using camel version 1.6.1.0

The code looks as follows (trimmed for clarity)

--------
        @Consume(uri = "topic:Out.RemoteServers")
        @RecipientList()
        public Collection<Endpoint> getRoutes(@Header(name = "IPAddresses") 
String
ips, String body) {
                List<Endpoint> dests = new ArrayList<Endpoint>();
                try {
                        // do some validation on IP addresses header and barf.

                        // The header is delimited by commas.
                        //
                        StringTokenizer tokenizer = new StringTokenizer(ips, 
",");
                
                        CamelContext ctx = new DefaultCamelContext();
                        while (tokenizer.hasMoreElements()) {
                                String ip = tokenizer.nextToken();
                                ActiveMQComponent amq = new 
ActiveMQComponent(ctx);
                                
                                logger.info("Setting the broker URL to IP: " + 
ip);
                                
                                amq.setBrokerURL("tcp://" + ip + ":61616");

                                Endpoint ep = 
amq.createEndpoint("activemq:topic:In.LocalServer");
                                dests.add(ep);
                        }
                } catch (Exception e) {
                            // Let our monitoring take care of this..
                        logger.error("Unexpected exception while routing.", e);
                }
                return dests;
        }

----

Appreciate any help.

cheers
- mdasari

-- 
View this message in context: 
http://old.nabble.com/How-to-route-a-message-to-remote-ActiveMQ-broker%28s%29-dynamically--tp28273566p28273566.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to