use of jakarta.jms.Connection in project spring-framework by spring-projects.
the class JmsTransactionManagerTests method testTransactionCommit.
@Test
public void testTransactionCommit() throws JMSException {
ConnectionFactory cf = mock(ConnectionFactory.class);
Connection con = mock(Connection.class);
final Session session = mock(Session.class);
given(cf.createConnection()).willReturn(con);
given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
JmsTransactionManager tm = new JmsTransactionManager(cf);
TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
JmsTemplate jt = new JmsTemplate(cf);
jt.execute((SessionCallback<Void>) sess -> {
assertThat(session).isSameAs(sess);
return null;
});
tm.commit(ts);
verify(session).commit();
verify(session).close();
verify(con).close();
}
use of jakarta.jms.Connection in project spring-framework by spring-projects.
the class JmsTransactionManagerTests method testParticipatingTransactionWithCommit.
@Test
public void testParticipatingTransactionWithCommit() throws JMSException {
ConnectionFactory cf = mock(ConnectionFactory.class);
Connection con = mock(Connection.class);
final Session session = mock(Session.class);
given(cf.createConnection()).willReturn(con);
given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
JmsTransactionManager tm = new JmsTransactionManager(cf);
TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
final JmsTemplate jt = new JmsTemplate(cf);
jt.execute((SessionCallback<Void>) sess -> {
assertThat(session).isSameAs(sess);
return null;
});
TransactionTemplate tt = new TransactionTemplate(tm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
jt.execute((SessionCallback<Void>) sess -> {
assertThat(session).isSameAs(sess);
return null;
});
}
});
tm.commit(ts);
verify(session).commit();
verify(session).close();
verify(con).close();
}
use of jakarta.jms.Connection in project spring-framework by spring-projects.
the class JmsTransactionManagerTests method testTransactionSuspension.
@Test
public void testTransactionSuspension() throws JMSException {
final ConnectionFactory cf = mock(ConnectionFactory.class);
Connection con = mock(Connection.class);
final Session session = mock(Session.class);
final Session session2 = mock(Session.class);
given(cf.createConnection()).willReturn(con);
given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session, session2);
JmsTransactionManager tm = new JmsTransactionManager(cf);
TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
final JmsTemplate jt = new JmsTemplate(cf);
jt.execute((SessionCallback<Void>) sess -> {
assertThat(session).isSameAs(sess);
return null;
});
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
jt.execute((SessionCallback<Void>) sess -> {
assertThat(session).isNotSameAs(sess);
return null;
});
}
});
jt.execute((SessionCallback<Void>) sess -> {
assertThat(session).isSameAs(sess);
return null;
});
tm.commit(ts);
verify(session).commit();
verify(session2).commit();
verify(session).close();
verify(session2).close();
verify(con, times(2)).close();
}
use of jakarta.jms.Connection 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.Connection in project spring-framework by spring-projects.
the class SingleConnectionFactoryTests method testWithTopicConnectionFactoryAndJms11Usage.
@Test
public void testWithTopicConnectionFactoryAndJms11Usage() throws JMSException {
TopicConnectionFactory cf = mock(TopicConnectionFactory.class);
TopicConnection con = mock(TopicConnection.class);
given(cf.createConnection()).willReturn(con);
SingleConnectionFactory scf = new SingleConnectionFactory(cf);
Connection con1 = scf.createConnection();
Connection con2 = scf.createConnection();
con1.start();
con2.start();
con1.close();
con2.close();
// should trigger actual close
scf.destroy();
verify(con).start();
verify(con).stop();
verify(con).close();
verifyNoMoreInteractions(con);
}
Aggregations