Skip to content

Instantly share code, notes, and snippets.

@jamesmorgan
Created October 15, 2011 07:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jamesmorgan/1289210 to your computer and use it in GitHub Desktop.
Save jamesmorgan/1289210 to your computer and use it in GitHub Desktop.
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
Copy link

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