Search in sources :

Example 6 with JmsConfig

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);
        }
    }
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) ExceptionListener(javax.jms.ExceptionListener) JMSException(javax.jms.JMSException) JmsConfig(org.jaffa.modules.messaging.services.configdomain.JmsConfig) FrameworkException(org.jaffa.exceptions.FrameworkException) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException)

Aggregations

JmsConfig (org.jaffa.modules.messaging.services.configdomain.JmsConfig)6 NamingException (javax.naming.NamingException)3 ConnectionFactory (javax.jms.ConnectionFactory)2 InitialContext (javax.naming.InitialContext)2 ContextKey (org.jaffa.loader.ContextKey)2 JndiConfig (org.jaffa.modules.messaging.services.configdomain.JndiConfig)2 Properties (java.util.Properties)1 Connection (javax.jms.Connection)1 ExceptionListener (javax.jms.ExceptionListener)1 JMSException (javax.jms.JMSException)1 FrameworkException (org.jaffa.exceptions.FrameworkException)1 JndiContext (org.jaffa.modules.messaging.services.configdomain.JndiContext)1 Param (org.jaffa.modules.messaging.services.configdomain.Param)1