use of javax.jms.Connection in project spring-framework by spring-projects.
the class JmsTransactionManagerTests method testTransactionCommitWithMessageProducer.
@Test
public void testTransactionCommitWithMessageProducer() throws JMSException {
Destination dest = new StubQueue();
ConnectionFactory cf = mock(ConnectionFactory.class);
Connection con = mock(Connection.class);
Session session = mock(Session.class);
MessageProducer producer = mock(MessageProducer.class);
final Message message = mock(Message.class);
given(cf.createConnection()).willReturn(con);
given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
given(session.createProducer(dest)).willReturn(producer);
given(session.getTransacted()).willReturn(true);
JmsTransactionManager tm = new JmsTransactionManager(cf);
TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
JmsTemplate jt = new JmsTemplate(cf);
jt.send(dest, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return message;
}
});
tm.commit(ts);
verify(producer).send(message);
verify(session).commit();
verify(producer).close();
verify(session).close();
verify(con).close();
}
use of javax.jms.Connection in project spring-framework by spring-projects.
the class JmsTransactionManagerTests method testTransactionRollback.
@Test
public void testTransactionRollback() 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(new SessionCallback<Void>() {
@Override
public Void doInJms(Session sess) {
assertTrue(sess == session);
return null;
}
});
tm.rollback(ts);
verify(session).rollback();
verify(session).close();
verify(con).close();
}
use of javax.jms.Connection in project spring-framework by spring-projects.
the class SingleConnectionFactoryTests method testWithConnectionFactory.
@Test
public void testWithConnectionFactory() throws JMSException {
ConnectionFactory cf = mock(ConnectionFactory.class);
Connection con = mock(Connection.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);
}
use of javax.jms.Connection in project spring-framework by spring-projects.
the class SimpleMessageListenerContainerTests method testDestroyClosesConsumersSessionsAndConnectionInThatOrder.
@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...
// 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();
this.container.start();
this.container.destroy();
verify(messageConsumer).close();
verify(session).close();
verify(connection).setExceptionListener(this.container);
verify(connection).start();
verify(connection).close();
}
use of javax.jms.Connection in project spring-framework by spring-projects.
the class SimpleMessageListenerContainerTests method testRegisteredErrorHandlerIsInvokedOnException.
@Test
public void testRegisteredErrorHandlerIsInvokedOnException() throws Exception {
final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer();
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);
// an exception is thrown, so the rollback logic is being applied here...
given(session.getTransacted()).willReturn(false);
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);
final IllegalStateException theException = new IllegalStateException("intentional test failure");
this.container.setConnectionFactory(connectionFactory);
this.container.setDestinationName(DESTINATION_NAME);
this.container.setMessageListener(new SessionAwareMessageListener<Message>() {
@Override
public void onMessage(Message message, Session session) throws JMSException {
throw theException;
}
});
ErrorHandler errorHandler = mock(ErrorHandler.class);
this.container.setErrorHandler(errorHandler);
this.container.afterPropertiesSet();
this.container.start();
// manually trigger an Exception with the above bad MessageListener...
Message message = mock(Message.class);
// a Throwable from a MessageListener MUST simply be swallowed...
messageConsumer.sendMessage(message);
verify(connection).setExceptionListener(this.container);
verify(connection).start();
verify(errorHandler).handleError(theException);
}
Aggregations