use of jakarta.jms.Connection in project spring-framework by spring-projects.
the class SingleConnectionFactoryTests method testWithConnectionFactoryAndLocalExceptionListenerWithCleanup.
@Test
public void testWithConnectionFactoryAndLocalExceptionListenerWithCleanup() 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) {
@Override
public void onException(JMSException ex) {
// no-op
}
};
scf.setReconnectOnException(true);
scf.setExceptionListener(listener0);
Connection con1 = scf.createConnection();
con1.setExceptionListener(listener1);
assertThat(con1.getExceptionListener()).isSameAs(listener1);
Connection con2 = scf.createConnection();
con2.setExceptionListener(listener2);
assertThat(con2.getExceptionListener()).isSameAs(listener2);
con.getExceptionListener().onException(new JMSException(""));
con2.close();
con.getExceptionListener().onException(new JMSException(""));
con1.close();
con.getExceptionListener().onException(new JMSException(""));
// should trigger actual close
scf.destroy();
assertThat(con.getStartCount()).isEqualTo(0);
assertThat(con.getCloseCount()).isEqualTo(1);
assertThat(listener0.getCount()).isEqualTo(3);
assertThat(listener1.getCount()).isEqualTo(2);
assertThat(listener2.getCount()).isEqualTo(1);
}
use of jakarta.jms.Connection in project spring-framework by spring-projects.
the class SingleConnectionFactoryTests method testWithConnectionFactoryAndLocalExceptionListenerWithReconnect.
@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);
assertThat(con1.getExceptionListener()).isSameAs(listener1);
con1.start();
Connection con2 = scf.createConnection();
con2.setExceptionListener(listener2);
assertThat(con2.getExceptionListener()).isSameAs(listener2);
con.getExceptionListener().onException(new JMSException(""));
con2.close();
con1.getMetaData();
con.getExceptionListener().onException(new JMSException(""));
con1.close();
// should trigger actual close
scf.destroy();
assertThat(con.getStartCount()).isEqualTo(2);
assertThat(con.getCloseCount()).isEqualTo(2);
assertThat(listener0.getCount()).isEqualTo(2);
assertThat(listener1.getCount()).isEqualTo(2);
assertThat(listener2.getCount()).isEqualTo(1);
}
use of jakarta.jms.Connection in project spring-framework by spring-projects.
the class SingleConnectionFactoryTests method testWithConnection.
@Test
public void testWithConnection() throws JMSException {
Connection con = mock(Connection.class);
SingleConnectionFactory scf = new SingleConnectionFactory(con);
Connection con1 = scf.createConnection();
con1.start();
con1.stop();
con1.close();
Connection con2 = scf.createConnection();
con2.start();
con2.stop();
con2.close();
// should trigger actual close
scf.destroy();
verify(con, times(2)).start();
verify(con, times(2)).stop();
verify(con).close();
verifyNoMoreInteractions(con);
}
use of jakarta.jms.Connection in project spring-framework by spring-projects.
the class SingleConnectionFactoryTests method testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage.
@Test
public void testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage() throws JMSException {
TopicConnectionFactory cf = mock(TopicConnectionFactory.class);
TopicConnection con = mock(TopicConnection.class);
TopicSession txSession = mock(TopicSession.class);
TopicSession nonTxSession = mock(TopicSession.class);
given(cf.createTopicConnection()).willReturn(con);
given(con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession);
given(txSession.getTransacted()).willReturn(true);
given(con.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession);
CachingConnectionFactory scf = new CachingConnectionFactory(cf);
scf.setReconnectOnException(false);
Connection con1 = scf.createTopicConnection();
Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE);
session1.getTransacted();
// should lead to rollback
session1.close();
session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
session1.close();
con1.start();
TopicConnection con2 = scf.createTopicConnection();
Session session2 = con2.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
session2.close();
session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE);
session2.getTransacted();
session2.close();
con2.start();
con1.close();
con2.close();
// should trigger actual close
scf.destroy();
verify(txSession).close();
verify(nonTxSession).close();
verify(con).start();
verify(con).stop();
verify(con).close();
}
use of jakarta.jms.Connection in project spring-framework by spring-projects.
the class SimpleMessageListenerContainerTests method testContextRefreshedEventStartsTheConnectionByDefault.
@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...
// no MessageSelector...
given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer);
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();
context.close();
verify(connection).setExceptionListener(this.container);
verify(connection).start();
}
Aggregations