I needed a solution for the same question, that's why I read more about it and made several tests.
Sending a test message (as Laures suggested) can be a problem in some environments.
The "normal" way would be to set a TransportListener (as Anand suggested), but really implement the provided interface and react on the reported events.
For other ActiveMQ newbies (as I was until last month) I post a sample startup implementation. It just writes logging for each event. In a real environment one can think about a reconnect trial in transportInterupted()
until transportResumed()
or similar and many things more ...
import java.io.IOException;
import org.apache.activemq.transport.TransportListener;
import org.apache.log4j.Logger;
class ConnectionStateMonitor
implements TransportListener
{
private static final Logger log = Logger.getLogger(ConnectionStateMonitor.class);
@Override
public void onCommand(Object command)
{
log.debug("Command detected: '" + command + "'");
}
@Override
public void onException(IOException exception)
{
log.error("Exception detected: '" + exception + "'");
}
@Override
public void transportInterupted()
{
log.error("Transport interuption detected.");
}
@Override
public void transportResumed()
{
log.info("Transport resumption detected.");
}
}
The TransportListener can be set e.g.:
ActiveMQConnection connection = (ActiveMQConnection) _factory.createConnection();
...
connection.addTransportListener(new ConnectionStateMonitor());
Have fun!