Search in sources :

Example 1 with SessionCallback

use of org.springframework.jms.core.SessionCallback in project spring-framework by spring-projects.

the class JmsTransactionManagerTests method testLazyTransactionalSession.

@Test
public void testLazyTransactionalSession() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    final Session session = mock(Session.class);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    tm.setLazyResourceRetrieval(true);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    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();
}
Also used : MessageProducer(jakarta.jms.MessageProducer) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JMSException(jakarta.jms.JMSException) Session(jakarta.jms.Session) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) BDDMockito.given(org.mockito.BDDMockito.given) Destination(jakarta.jms.Destination) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) JmsTemplate(org.springframework.jms.core.JmsTemplate) ConnectionFactory(jakarta.jms.ConnectionFactory) Connection(jakarta.jms.Connection) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Message(jakarta.jms.Message) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) StubQueue(org.springframework.jms.StubQueue) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Mockito.times(org.mockito.Mockito.times) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) Mockito.mock(org.mockito.Mockito.mock) ConnectionFactory(jakarta.jms.ConnectionFactory) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(jakarta.jms.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) JmsTemplate(org.springframework.jms.core.JmsTemplate) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 2 with SessionCallback

use of org.springframework.jms.core.SessionCallback 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();
}
Also used : MessageProducer(jakarta.jms.MessageProducer) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JMSException(jakarta.jms.JMSException) Session(jakarta.jms.Session) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) BDDMockito.given(org.mockito.BDDMockito.given) Destination(jakarta.jms.Destination) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) JmsTemplate(org.springframework.jms.core.JmsTemplate) ConnectionFactory(jakarta.jms.ConnectionFactory) Connection(jakarta.jms.Connection) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Message(jakarta.jms.Message) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) StubQueue(org.springframework.jms.StubQueue) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Mockito.times(org.mockito.Mockito.times) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) Mockito.mock(org.mockito.Mockito.mock) ConnectionFactory(jakarta.jms.ConnectionFactory) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(jakarta.jms.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) JmsTemplate(org.springframework.jms.core.JmsTemplate) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 3 with SessionCallback

use of org.springframework.jms.core.SessionCallback 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();
}
Also used : MessageProducer(jakarta.jms.MessageProducer) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JMSException(jakarta.jms.JMSException) Session(jakarta.jms.Session) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) BDDMockito.given(org.mockito.BDDMockito.given) Destination(jakarta.jms.Destination) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) JmsTemplate(org.springframework.jms.core.JmsTemplate) ConnectionFactory(jakarta.jms.ConnectionFactory) Connection(jakarta.jms.Connection) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Message(jakarta.jms.Message) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) StubQueue(org.springframework.jms.StubQueue) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Mockito.times(org.mockito.Mockito.times) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) Mockito.mock(org.mockito.Mockito.mock) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(jakarta.jms.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) ConnectionFactory(jakarta.jms.ConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 4 with SessionCallback

use of org.springframework.jms.core.SessionCallback 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();
}
Also used : MessageProducer(jakarta.jms.MessageProducer) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JMSException(jakarta.jms.JMSException) Session(jakarta.jms.Session) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) BDDMockito.given(org.mockito.BDDMockito.given) Destination(jakarta.jms.Destination) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) JmsTemplate(org.springframework.jms.core.JmsTemplate) ConnectionFactory(jakarta.jms.ConnectionFactory) Connection(jakarta.jms.Connection) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Message(jakarta.jms.Message) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) StubQueue(org.springframework.jms.StubQueue) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Mockito.times(org.mockito.Mockito.times) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) Mockito.mock(org.mockito.Mockito.mock) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(jakarta.jms.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) ConnectionFactory(jakarta.jms.ConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 5 with SessionCallback

use of org.springframework.jms.core.SessionCallback 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((SessionCallback<Void>) sess -> {
        assertThat(session).isSameAs(sess);
        return null;
    });
    tm.rollback(ts);
    verify(session).rollback();
    verify(session).close();
    verify(con).close();
}
Also used : MessageProducer(jakarta.jms.MessageProducer) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JMSException(jakarta.jms.JMSException) Session(jakarta.jms.Session) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) BDDMockito.given(org.mockito.BDDMockito.given) Destination(jakarta.jms.Destination) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) JmsTemplate(org.springframework.jms.core.JmsTemplate) ConnectionFactory(jakarta.jms.ConnectionFactory) Connection(jakarta.jms.Connection) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Message(jakarta.jms.Message) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) StubQueue(org.springframework.jms.StubQueue) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Mockito.times(org.mockito.Mockito.times) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) Mockito.mock(org.mockito.Mockito.mock) ConnectionFactory(jakarta.jms.ConnectionFactory) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(jakarta.jms.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) JmsTemplate(org.springframework.jms.core.JmsTemplate) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Aggregations

Connection (jakarta.jms.Connection)7 ConnectionFactory (jakarta.jms.ConnectionFactory)7 Destination (jakarta.jms.Destination)7 JMSException (jakarta.jms.JMSException)7 Message (jakarta.jms.Message)7 MessageProducer (jakarta.jms.MessageProducer)7 Session (jakarta.jms.Session)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)7 AfterEach (org.junit.jupiter.api.AfterEach)7 Test (org.junit.jupiter.api.Test)7 BDDMockito.given (org.mockito.BDDMockito.given)7 Mockito.mock (org.mockito.Mockito.mock)7 Mockito.times (org.mockito.Mockito.times)7 Mockito.verify (org.mockito.Mockito.verify)7 StubQueue (org.springframework.jms.StubQueue)7 JmsTemplate (org.springframework.jms.core.JmsTemplate)7 SessionCallback (org.springframework.jms.core.SessionCallback)7 TransactionDefinition (org.springframework.transaction.TransactionDefinition)7 TransactionStatus (org.springframework.transaction.TransactionStatus)7