How to find out if JMS Connection is there?

前端 未结 4 1647
生来不讨喜
生来不讨喜 2020-12-15 18:47

In JMS it is easy to find out if a connection is lost, a exception happens. But how do I find out if the connection is there again?

Scenario: I use JMS to communicat

4条回答
  •  攒了一身酷
    2020-12-15 19:17

    The best way to monitor for connection exception is setting an exception listener, for example:

    ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("jmsContextName");
            connection = connectionFactory.createConnection();
            connection.setExceptionListener(new ExceptionListener() {
                @Override
                public void onException(JMSException exception) {
                    logger.error("ExceptionListener triggered: " + exception.getMessage(), exception);
                    try {
                        Thread.sleep(5000); // Wait 5 seconds (JMS server restarted?)
                        restartJSMConnection();
                    } catch (InterruptedException e) {
                        logger.error("Error pausing thread" + e.getMessage());
                    }
                }
            });
            connection.start();
    

提交回复
热议问题