use of javax.jms.Connection in project spring-framework by spring-projects.
the class SingleConnectionFactory method createQueueConnection.
@Override
public QueueConnection createQueueConnection() throws JMSException {
Connection con;
synchronized (this.connectionMonitor) {
this.pubSubMode = Boolean.FALSE;
con = createConnection();
}
if (!(con instanceof QueueConnection)) {
throw new javax.jms.IllegalStateException("This SingleConnectionFactory does not hold a QueueConnection but rather: " + con);
}
return ((QueueConnection) con);
}
use of javax.jms.Connection in project spring-framework by spring-projects.
the class JmsTemplate method executeLocal.
/**
* A variant of {@link #execute(SessionCallback, boolean)} that explicitly
* creates a non-transactional {@link Session}. The given {@link SessionCallback}
* does not participate in an existing transaction.
*/
private <T> T executeLocal(SessionCallback<T> action, boolean startConnection) throws JmsException {
Assert.notNull(action, "Callback object must not be null");
Connection con = null;
Session session = null;
try {
con = getConnectionFactory().createConnection();
session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
if (startConnection) {
con.start();
}
if (logger.isDebugEnabled()) {
logger.debug("Executing callback on JMS Session: " + session);
}
return action.doInJms(session);
} catch (JMSException ex) {
throw convertJmsAccessException(ex);
} finally {
JmsUtils.closeSession(session);
ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory(), startConnection);
}
}
use of javax.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(new SessionCallback<Void>() {
@Override
public Void doInJms(Session sess) {
assertTrue(sess == session);
return null;
}
});
tm.commit(ts);
verify(session).commit();
verify(session).close();
verify(con).close();
}
use of javax.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(new SessionCallback<Void>() {
@Override
public Void doInJms(Session sess) {
assertTrue(sess == session);
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(new SessionCallback<Void>() {
@Override
public Void doInJms(Session sess) {
assertTrue(sess != session);
return null;
}
});
}
});
jt.execute(new SessionCallback<Void>() {
@Override
public Void doInJms(Session sess) {
assertTrue(sess == session);
return null;
}
});
tm.commit(ts);
verify(session).commit();
verify(session2).commit();
verify(session).close();
verify(session2).close();
verify(con, times(2)).close();
}
use of javax.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(new SessionCallback<Void>() {
@Override
public Void doInJms(Session sess) {
assertTrue(sess == session);
return null;
}
});
TransactionTemplate tt = new TransactionTemplate(tm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
jt.execute(new SessionCallback<Void>() {
@Override
public Void doInJms(Session sess) {
assertTrue(sess == session);
return null;
}
});
}
});
tm.commit(ts);
verify(session).commit();
verify(session).close();
verify(con).close();
}
Aggregations