Skip to content

Instantly share code, notes, and snippets.

@rhyskeepence
Created November 24, 2010 10:57
Show Gist options
  • Select an option

  • Save rhyskeepence/713483 to your computer and use it in GitHub Desktop.

Select an option

Save rhyskeepence/713483 to your computer and use it in GitHub Desktop.
Print a message if connecting is taking too long
private void attachListener() {
ScheduledFuture<?> activemqWarning = scheduleWarningIfJmsConnectionTakesTooLong();
try {
// perform some operation that may hang if ActiveMq is not running
} finally {
activemqWarning.cancel(true);
}
}
private ScheduledFuture<?> scheduleWarningIfJmsConnectionTakesTooLong() {
return Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() {
public void run() {
System.err.println("WARNING: It's taking a long time to connect to JMS - check ActiveMQ is running!");
}
}, 10, TimeUnit.SECONDS);
}
@rhyskeepence
Copy link
Copy Markdown
Author

ActiveMq client code hangs if the server is not up. It would be helpful if we knew that is why it is hanging.
So, schedule a thread to print a message if we can't connect within 10 seconds.

@rajdavies
Copy link
Copy Markdown

You could just this from the URL used to connect the ActiveMQ broker - see http://activemq.apache.org/failover-transport-reference.html for information on setting failover timeouts.

@rhyskeepence
Copy link
Copy Markdown
Author

Setting a failover timeout means my app quits. I'd rather see a warning and be able to start activemq without restarting my app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment