use of javax.jms.ExceptionListener in project oozie by apache.
the class DefaultConnectionContext method createConnection.
@Override
public void createConnection(Properties props) throws NamingException, JMSException {
Context jndiContext = new InitialContext(props);
connectionFactoryName = (String) jndiContext.getEnvironment().get("connectionFactoryNames");
if (connectionFactoryName == null || connectionFactoryName.trim().length() == 0) {
connectionFactoryName = "ConnectionFactory";
}
ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup(connectionFactoryName);
LOG.info("Connecting with the following properties \n" + jndiContext.getEnvironment().toString());
try {
connection = connectionFactory.createConnection();
connection.start();
connection.setExceptionListener(new ExceptionListener() {
@Override
public void onException(JMSException je) {
LOG.error("Error in JMS connection", je);
}
});
} catch (JMSException e1) {
LOG.error(e1.getMessage(), e1);
if (connection != null) {
try {
connection.close();
} catch (Exception e2) {
LOG.error(e2.getMessage(), e2);
} finally {
connection = null;
}
}
throw e1;
}
}
use of javax.jms.ExceptionListener 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();
if (connectionFactory == null) {
throw new JaffaMessagingFrameworkException(JaffaMessagingFrameworkException.CONNECTION_ERROR, null);
}
final JmsConfig jmsConfig = ConfigurationService.getInstance().getJmsConfig();
if (jmsConfig == null || 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);
}
}
}
use of javax.jms.ExceptionListener in project hive by apache.
the class NotificationListener method createConnection.
/**
* Create the JMS connection
* @return newly created JMS connection
*/
protected Connection createConnection() {
LOG.info("Will create new JMS connection");
Context jndiCntxt;
Connection jmsConnection = null;
try {
jndiCntxt = new InitialContext();
ConnectionFactory connFac = (ConnectionFactory) jndiCntxt.lookup("ConnectionFactory");
jmsConnection = connFac.createConnection();
jmsConnection.start();
jmsConnection.setExceptionListener(new ExceptionListener() {
@Override
public void onException(JMSException jmse) {
LOG.error("JMS Exception listener received exception. Ignored the error", jmse);
}
});
} catch (NamingException e) {
LOG.error("JNDI error while setting up Message Bus connection. " + "Please make sure file named 'jndi.properties' is in " + "classpath and contains appropriate key-value pairs.", e);
} catch (JMSException e) {
LOG.error("Failed to initialize connection to message bus", e);
} catch (Throwable t) {
LOG.error("Unable to connect to JMS provider", t);
}
return jmsConnection;
}
use of javax.jms.ExceptionListener in project qpid-broker-j by apache.
the class ClientJmsDelegate method setConnectionLostListener.
public void setConnectionLostListener(final ConnectionLostListener connectionLostListener) {
try {
_controllerConnection.setExceptionListener(new ExceptionListener() {
@Override
public void onException(final JMSException exception) {
LOGGER.warn("Caught ", exception);
if (connectionLostListener != null) {
try {
_controllerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE).close();
} catch (JMSException e) {
LOGGER.warn("Unable to create/close a new session, assuming the connection is lost ", exception);
connectionLostListener.connectionLost();
}
}
}
});
} catch (JMSException e) {
// ignore
}
}
use of javax.jms.ExceptionListener in project qpid-broker-j by apache.
the class ExceptionListenerTest method testExceptionListenerClosesConnectionIsAllowed.
@Test
public void testExceptionListenerClosesConnectionIsAllowed() throws Exception {
assumeThat(getBrokerAdmin().supportsRestart(), is(equalTo(true)));
final Connection connection = getConnection();
try {
final CountDownLatch exceptionReceivedLatch = new CountDownLatch(1);
final AtomicReference<JMSException> exceptionHolder = new AtomicReference<>();
final AtomicReference<Throwable> unexpectedExceptionHolder = new AtomicReference<>();
final ExceptionListener listener = exception -> {
exceptionHolder.set(exception);
try {
connection.close();
// PASS
} catch (Throwable t) {
unexpectedExceptionHolder.set(t);
} finally {
exceptionReceivedLatch.countDown();
}
};
connection.setExceptionListener(listener);
getBrokerAdmin().restart();
assertTrue("Exception was not propagated into exception listener in timely manner", exceptionReceivedLatch.await(getReceiveTimeout(), TimeUnit.MILLISECONDS));
assertNotNull("Unexpected exception", exceptionHolder.get());
assertNull("Connection#close() should not have thrown exception", unexpectedExceptionHolder.get());
} finally {
connection.close();
}
}
Aggregations