Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect broken/recovered JMS connection in Apache Camel?

We are building an integration project using Apache Camel (Camel 2.10.3, Java DSL based).

We have a route that extracts data from a database (lets call it IN_DB), does some logic and inserts into another database (OUT_DB) once a day, and another route that subscribes to a JMS topic for XML data, does some logic and inserts it into the same database (OUT_DB) throughout the day.

The requirement is that when the JMS topic connection goes down for whatever reason, we keep trying to reconnect indefinitely, and once reconnection is successful we need to go back to the database (IN_DB) and do another load to fill in the gap where the topic was down.

My question is how can we do this logic ('I was connected then I got disconnected and now I am connected again') in Camel? What happens to the route that starts with a topic consumer when the topic goes down, will the route just stop? Or will it issue an error message to some error queue? Do I have to write my own handler to monitor the topic connection, or will Camel automatically reconnect when the topic comes back up and set some message header, or set some context variable to indicate that the 'I was connected then I got disconnected and now I am connected again' scenario has happened? I am happy building the route logic around calling the database load I just can't figure out the best way to 'detect' in Camel that this scenario has happened.

Any suggestions much appreciated.

like image 815
Matt Avatar asked Sep 05 '13 10:09

Matt


1 Answers

In terms of the reconnecting to your queue, it depends on what JMS broker you are using. If you are using ActiveMQ then you can configure how it reconnects through the URI so it can try and reconnect to another broker, reconnect to the same broker after a timeout etc. The docs for it are here.

To detect when the connection has failed, the easiest from the point of the view of the program is to just use persistent queues instead of topics. However, assuming that isn't feasible then I think you've got two options.

  1. Define a JMS exception listener. This should let you know when the underlying connection has disappeared.

To detect when its back up again I think you're stuck with posting a message to a particular topic and watching for messages from this topic in another route. When you read a message on this topic, you know the broker has come back up so you can reload your data from the DB.

or 2. You could post regular hearbeat messages onto your JMS topic. If you stop receiving them you know the broker has gone down. Once you start to get them again, you know it is back up and you need to reload data from the DB.

like image 154
matt helliwell Avatar answered Oct 22 '22 02:10

matt helliwell