use of jakarta.jms.ConnectionFactory in project spring-framework by spring-projects.
the class SingleConnectionFactoryTests method testWithConnectionFactoryAndExceptionListenerAndReconnectOnException.
@Test
public void testWithConnectionFactoryAndExceptionListenerAndReconnectOnException() throws JMSException {
ConnectionFactory cf = mock(ConnectionFactory.class);
TestConnection con = new TestConnection();
given(cf.createConnection()).willReturn(con);
TestExceptionListener listener = new TestExceptionListener();
SingleConnectionFactory scf = new SingleConnectionFactory(cf);
scf.setExceptionListener(listener);
scf.setReconnectOnException(true);
Connection con1 = scf.createConnection();
assertThat(con1.getExceptionListener()).isSameAs(listener);
con1.start();
con.getExceptionListener().onException(new JMSException(""));
Connection con2 = scf.createConnection();
con2.start();
// should trigger actual close
scf.destroy();
assertThat(con.getStartCount()).isEqualTo(2);
assertThat(con.getCloseCount()).isEqualTo(2);
assertThat(listener.getCount()).isEqualTo(1);
}
use of jakarta.jms.ConnectionFactory 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.ConnectionFactory 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.ConnectionFactory in project spring-framework by spring-projects.
the class DefaultMessageListenerContainerTests method createFailingContainerFactory.
private ConnectionFactory createFailingContainerFactory() {
try {
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
given(connectionFactory.createConnection()).will(invocation -> {
throw new JMSException("Test exception");
});
return connectionFactory;
} catch (JMSException ex) {
// never happen
throw new IllegalStateException(ex);
}
}
use of jakarta.jms.ConnectionFactory in project spring-framework by spring-projects.
the class DefaultMessageListenerContainerTests method createRecoverableContainerFactory.
private ConnectionFactory createRecoverableContainerFactory(final int failingAttempts) {
try {
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
given(connectionFactory.createConnection()).will(new Answer<Object>() {
int currentAttempts = 0;
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
currentAttempts++;
if (currentAttempts <= failingAttempts) {
throw new JMSException("Test exception (attempt " + currentAttempts + ")");
} else {
return mock(Connection.class);
}
}
});
return connectionFactory;
} catch (JMSException ex) {
// never happen
throw new IllegalStateException(ex);
}
}
Aggregations