use of org.apache.activemq.transport.TransportListener in project opencast by opencast.
the class MessageBaseFacility method reconnect.
/**
* Opens new sessions and connections to the message broker
*/
public synchronized boolean reconnect() {
disconnectMessageBroker(false);
try {
/* Create a ConnectionFactory for establishing connections to the Active MQ broker */
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
connectionFactory.setUserName(username);
connectionFactory.setPassword(password);
}
connectionFactory.setTransportListener(new TransportListener() {
@Override
public void transportResumed() {
enable(true);
logger.info("Connection to ActiveMQ is working");
}
@Override
public void transportInterupted() {
enable(false);
logger.error("Connection to ActiveMQ message broker interrupted ({}, username: {})", url, username);
}
@Override
public void onException(IOException ex) {
enable(false);
logger.error("ActiveMQ transport exception: {}", ex.getMessage());
}
@Override
public void onCommand(Object obj) {
logger.trace("ActiveMQ command: {}", obj);
}
});
logger.info("Starting connection to ActiveMQ message broker, waiting until connection is established...");
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(null);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
} catch (JMSException e) {
logger.error("Failed connecting to ActiveMQ message broker using url '{}'", url);
/* Make sure to set session, etc. to null if connecting failed */
disconnectMessageBroker(false);
return false;
}
logger.info("Connection to ActiveMQ message broker successfully started");
return true;
}
Aggregations