use of org.jaffa.modules.messaging.services.configdomain.JmsConfig in project jaffa-framework by jaffa-projects.
the class JaffaConnectionFactory method createConnection.
/**
* Creates a JMS connection, only if one doesn't already exist.
*
* @throws FrameworkException
* in case any internal error occurs.
* @throws ApplicationExceptions
* Indicates application error(s).
*/
private void createConnection() throws FrameworkException, ApplicationExceptions {
if (connection == null) {
Connection _connection = null;
try {
final ConnectionFactory connectionFactory = JaffaConnectionFactory.obtainConnectionFactory();
final JmsConfig jmsConfig = ConfigurationService.getInstance().getJmsConfig();
if (jmsConfig.getUser() == null)
_connection = connectionFactory.createConnection();
else
_connection = connectionFactory.createConnection(jmsConfig.getUser(), jmsConfig.getPassword());
// Register a listener to detect connection breakage
_connection.setExceptionListener(new ExceptionListener() {
public void onException(JMSException exception) {
if (LOGGER.isInfoEnabled())
LOGGER.info("The ExceptionListener registered with the JMS Connection '" + connection + "' has been invoked. Will recreate the JMS connection", exception);
try {
forciblyCreateConnection();
} catch (Exception e) {
LOGGER.error("Error in recreating a JMS Connection", e);
}
}
});
// This will ensure that messages reach the temporary consumers that may
// be created for deleting/resubmitting
_connection.start();
connection = _connection;
} catch (JMSException e) {
if (_connection != null) {
try {
_connection.close();
} catch (JMSException e1) {
LOGGER.error("Error closing a JMS Connection", e1);
}
}
LOGGER.error("Error in creating a JMS Connection", e);
throw new JaffaMessagingFrameworkException(JaffaMessagingFrameworkException.CONNECTION_ERROR, null, e);
}
}
}
Aggregations