/** * Registers this listener container as JMS ExceptionListener on the shared connection. */ @Override protected void prepareSharedConnection(Connection connection) throws JMSException { super.prepareSharedConnection(connection); connection.setExceptionListener(this); }
@Override public void setExceptionListener(ExceptionListener listener) throws JMSException { delegate.setExceptionListener(TracingExceptionListener.create(listener, jmsTracing)); }
/** * 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; }
/** * Prepare the given Connection before it is exposed. * <p>The default implementation applies ExceptionListener and client id. * Can be overridden in subclasses. * @param con the Connection to prepare * @throws JMSException if thrown by JMS API methods * @see #setExceptionListener * @see #setReconnectOnException */ protected void prepareConnection(Connection con) throws JMSException { if (getClientId() != null) { con.setClientID(getClientId()); } if (this.aggregatedExceptionListener != null) { con.setExceptionListener(this.aggregatedExceptionListener); } else if (getExceptionListener() != null || isReconnectOnException()) { ExceptionListener listenerToUse = getExceptionListener(); if (isReconnectOnException()) { this.aggregatedExceptionListener = new AggregatedExceptionListener(); this.aggregatedExceptionListener.delegates.add(this); if (listenerToUse != null) { this.aggregatedExceptionListener.delegates.add(listenerToUse); } listenerToUse = this.aggregatedExceptionListener; } con.setExceptionListener(listenerToUse); } }
connection.setExceptionListener(new IgniteJmsExceptionListener());
@Test public void testWithConnectionFactoryAndExceptionListener() throws JMSException { ConnectionFactory cf = mock(ConnectionFactory.class); Connection con = mock(Connection.class); ExceptionListener listener = new ChainedExceptionListener(); given(cf.createConnection()).willReturn(con); given(con.getExceptionListener()).willReturn(listener); SingleConnectionFactory scf = new SingleConnectionFactory(cf); scf.setExceptionListener(listener); Connection con1 = scf.createConnection(); assertEquals(listener, con1.getExceptionListener()); con1.start(); con1.stop(); con1.close(); Connection con2 = scf.createConnection(); con2.start(); con2.stop(); con2.close(); scf.destroy(); // should trigger actual close verify(con).setExceptionListener(listener); verify(con, times(2)).start(); verify(con, times(2)).stop(); verify(con).close(); }
@Test public void testContextRefreshedEventStartsTheConnectionByDefault() throws Exception { MessageConsumer messageConsumer = mock(MessageConsumer.class); Session session = mock(Session.class); // Queue gets created in order to create MessageConsumer for that Destination... given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION); // and then the MessageConsumer gets created... given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer); // no MessageSelector... Connection connection = mock(Connection.class); // session gets created in order to register MessageListener... given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session); // and the connection is start()ed after the listener is registered... ConnectionFactory connectionFactory = mock(ConnectionFactory.class); given(connectionFactory.createConnection()).willReturn(connection); this.container.setConnectionFactory(connectionFactory); this.container.setDestinationName(DESTINATION_NAME); this.container.setMessageListener(new TestMessageListener()); this.container.afterPropertiesSet(); GenericApplicationContext context = new GenericApplicationContext(); context.getBeanFactory().registerSingleton("messageListenerContainer", this.container); context.refresh(); verify(connection).setExceptionListener(this.container); verify(connection).start(); }
verify(connection).setExceptionListener(this.container); verify(connection).start();
@Test public void testDestroyClosesConsumersSessionsAndConnectionInThatOrder() throws Exception { MessageConsumer messageConsumer = mock(MessageConsumer.class); Session session = mock(Session.class); // Queue gets created in order to create MessageConsumer for that Destination... given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION); // and then the MessageConsumer gets created... given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer); // no MessageSelector... Connection connection = mock(Connection.class); // session gets created in order to register MessageListener... given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session); // and the connection is start()ed after the listener is registered... ConnectionFactory connectionFactory = mock(ConnectionFactory.class); given(connectionFactory.createConnection()).willReturn(connection); this.container.setConnectionFactory(connectionFactory); this.container.setDestinationName(DESTINATION_NAME); this.container.setMessageListener(new TestMessageListener()); this.container.afterPropertiesSet(); this.container.start(); this.container.destroy(); verify(messageConsumer).close(); verify(session).close(); verify(connection).setExceptionListener(this.container); verify(connection).start(); verify(connection).close(); }
@Test public void testContextRefreshedEventDoesNotStartTheConnectionIfAutoStartIsSetToFalse() throws Exception { MessageConsumer messageConsumer = mock(MessageConsumer.class); Session session = mock(Session.class); // Queue gets created in order to create MessageConsumer for that Destination... given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION); // and then the MessageConsumer gets created... given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer); // no MessageSelector... Connection connection = mock(Connection.class); // session gets created in order to register MessageListener... given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); given(connectionFactory.createConnection()).willReturn(connection); this.container.setConnectionFactory(connectionFactory); this.container.setDestinationName(DESTINATION_NAME); this.container.setMessageListener(new TestMessageListener()); this.container.setAutoStartup(false); this.container.afterPropertiesSet(); GenericApplicationContext context = new GenericApplicationContext(); context.getBeanFactory().registerSingleton("messageListenerContainer", this.container); context.refresh(); verify(connection).setExceptionListener(this.container); }
verify(connection).setExceptionListener(this.container); verify(connection).start();
verify(connection).setExceptionListener(this.container); verify(connection).start(); verify(errorHandler).handleError(theException);
@Test public void testWithConnectionFactoryAndLocalExceptionListenerWithReconnect() throws JMSException { ConnectionFactory cf = mock(ConnectionFactory.class); TestConnection con = new TestConnection(); given(cf.createConnection()).willReturn(con); TestExceptionListener listener0 = new TestExceptionListener(); TestExceptionListener listener1 = new TestExceptionListener(); TestExceptionListener listener2 = new TestExceptionListener(); SingleConnectionFactory scf = new SingleConnectionFactory(cf); scf.setReconnectOnException(true); scf.setExceptionListener(listener0); Connection con1 = scf.createConnection(); con1.setExceptionListener(listener1); assertSame(listener1, con1.getExceptionListener()); con1.start(); Connection con2 = scf.createConnection(); con2.setExceptionListener(listener2); assertSame(listener2, con2.getExceptionListener()); con.getExceptionListener().onException(new JMSException("")); con2.close(); con1.getMetaData(); con.getExceptionListener().onException(new JMSException("")); con1.close(); scf.destroy(); // should trigger actual close assertEquals(2, con.getStartCount()); assertEquals(2, con.getCloseCount()); assertEquals(2, listener0.getCount()); assertEquals(2, listener1.getCount()); assertEquals(1, listener2.getCount()); }
verify(connection).setExceptionListener(this.container); verify(connection).start();
verify(connection).setExceptionListener(this.container); verify(connection).start(); verify(exceptionListener).onException(theException);
scf.setExceptionListener(listener0); Connection con1 = scf.createConnection(); con1.setExceptionListener(listener1); assertSame(listener1, con1.getExceptionListener()); Connection con2 = scf.createConnection(); con2.setExceptionListener(listener2); assertSame(listener2, con2.getExceptionListener()); con.getExceptionListener().onException(new JMSException(""));
assertTrue(listener.listenerInvoked); verify(connection).setExceptionListener(this.container); verify(connection).start();
/** * Registers this listener container as JMS ExceptionListener on the shared connection. */ @Override protected void prepareSharedConnection(Connection connection) throws JMSException { super.prepareSharedConnection(connection); connection.setExceptionListener(this); }
@Test public void connectTestEmptyUserNameAndPassword() throws JMSException { clusterService.connect(); verify(connection).setExceptionListener(any()); verify(connection).start(); verify(factory).createConnection(); }
@Test public void connectTest() throws JMSException { System.setProperty(ClusterParameters.APPFORMER_JMS_USERNAME, "dora"); System.setProperty(ClusterParameters.APPFORMER_JMS_PASSWORD, "bento"); clusterService = getClusterService(factory); clusterService.connect(); verify(connection).setExceptionListener(any()); verify(connection).start(); verify(factory).createConnection(any(), any()); }