Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copy Messages from one ActiveMQ instance to another with Apache Camel and Groovy
import org.apache.activemq.camel.component.ActiveMQComponent
import org.apache.camel.CamelContext
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.impl.DefaultCamelContext
class CopyFromOneServerToAnother extends RouteBuilder{
public static void main(String[] args) {
final CamelContext camelContext = new DefaultCamelContext();
// IP address for the queue I will be consuming messages from
camelContext.addComponent("jms-01", ActiveMQComponent.activeMQComponent("tcp://172.31.100.01:61616"));
// IP address for the queue I will be placing my consumed messages from
camelContext.addComponent("jms-02", ActiveMQComponent.activeMQComponent("tcp://172.31.100.02:61616"));
try {
// Add the routes defined below to the camel context
camelContext.addRoutes(new CopyFromOneServerToAnother());
camelContext.start();
Thread.sleep(10000000);
}
catch (final Exception e) {
e.printStackTrace();
}
finally {
try {
camelContext.stop();
}
catch (final Exception e) {
e.printStackTrace();
}
}
}
@Override
public void configure() throws Exception {
/// The name of the AMQ instance and queue where message will be taken from
from("jms-01:some-broken-queue")
// Simply log when each message has been processed
// Additional processors can be used here to alter messages, fix issue with them before being placed on the new queue
.log("Processed")
// Final resting point for each message.
.to("jms-02:some-fixed-queue")
.end();
}
}
@xpapad

This comment has been minimized.

Copy link

@xpapad xpapad commented Dec 5, 2011

I'm facing a similar issue right now, just my two cents :) The first problem with your approach is that messages can be lost if there is a problem during delivery, therefore the route should be transactional. Another issue is that the default shutdown strategy suspends an endpoint when you close a route. This will prevent other routes from consuming messages from your source queue. If this is an issue in your case, you should lower the timeout of the shutdown strategy in your Camel context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.