Search in sources :

Example 11 with JMSException

use of jakarta.jms.JMSException 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 12 with JMSException

use of jakarta.jms.JMSException 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 13 with JMSException

use of jakarta.jms.JMSException 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 14 with JMSException

use of jakarta.jms.JMSException in project spring-framework by spring-projects.

the class JmsMessagingTemplateTests method createTextMessage.

protected TextMessage createTextMessage(MessageCreator creator) throws JMSException {
    Session mock = mock(Session.class);
    given(mock.createTextMessage(any())).willAnswer((Answer<TextMessage>) invocation -> new StubTextMessage((String) invocation.getArguments()[0]));
    jakarta.jms.Message message = creator.createMessage(mock);
    verify(mock).createTextMessage(any());
    return (TextMessage) message;
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) MessagingException(org.springframework.messaging.MessagingException) MessageNotReadableException(org.springframework.jms.MessageNotReadableException) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) JMSException(jakarta.jms.JMSException) HashMap(java.util.HashMap) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) DestinationResolutionException(org.springframework.jms.support.destination.DestinationResolutionException) Captor(org.mockito.Captor) Session(jakarta.jms.Session) InvalidDestinationException(org.springframework.jms.InvalidDestinationException) Answer(org.mockito.stubbing.Answer) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) StubTextMessage(org.springframework.jms.StubTextMessage) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) Destination(jakarta.jms.Destination) MessageFormatException(jakarta.jms.MessageFormatException) MessagingMessageConverter(org.springframework.jms.support.converter.MessagingMessageConverter) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Message(org.springframework.messaging.Message) GenericMessageConverter(org.springframework.messaging.converter.GenericMessageConverter) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) MessageNotWriteableException(jakarta.jms.MessageNotWriteableException) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) Mockito.times(org.mockito.Mockito.times) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) TextMessage(jakarta.jms.TextMessage) Writer(java.io.Writer) MessageConverter(org.springframework.jms.support.converter.MessageConverter) MessageBuilder(org.springframework.messaging.support.MessageBuilder) Mockito.mock(org.mockito.Mockito.mock) StubTextMessage(org.springframework.jms.StubTextMessage) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session)

Example 15 with JMSException

use of jakarta.jms.JMSException in project spring-framework by spring-projects.

the class JmsUtils method buildExceptionMessage.

/**
 * Build a descriptive exception message for the given JMSException,
 * incorporating a linked exception's message if appropriate.
 * @param ex the JMSException to build a message for
 * @return the descriptive message String
 * @see jakarta.jms.JMSException#getLinkedException()
 */
public static String buildExceptionMessage(JMSException ex) {
    String message = ex.getMessage();
    Exception linkedEx = ex.getLinkedException();
    if (linkedEx != null) {
        if (message == null) {
            message = linkedEx.toString();
        } else {
            String linkedMessage = linkedEx.getMessage();
            if (linkedMessage != null && !message.contains(linkedMessage)) {
                message = message + "; nested exception is " + linkedEx;
            }
        }
    }
    return message;
}
Also used : JmsException(org.springframework.jms.JmsException) MessageNotReadableException(org.springframework.jms.MessageNotReadableException) UncategorizedJmsException(org.springframework.jms.UncategorizedJmsException) JMSException(jakarta.jms.JMSException) TransactionInProgressException(org.springframework.jms.TransactionInProgressException) InvalidDestinationException(org.springframework.jms.InvalidDestinationException) MessageNotWriteableException(org.springframework.jms.MessageNotWriteableException) ResourceAllocationException(org.springframework.jms.ResourceAllocationException) MessageFormatException(org.springframework.jms.MessageFormatException) MessageEOFException(org.springframework.jms.MessageEOFException) TransactionRolledBackException(org.springframework.jms.TransactionRolledBackException) InvalidClientIDException(org.springframework.jms.InvalidClientIDException) JmsSecurityException(org.springframework.jms.JmsSecurityException) InvalidSelectorException(org.springframework.jms.InvalidSelectorException)

Aggregations

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