Search in sources :

Example 36 with JMSException

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

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied_AndSendingThrowsJMSException.

@Test
void testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied_AndSendingThrowsJMSException() throws Exception {
    Queue destination = mock(Queue.class);
    final TextMessage sentTextMessage = mock(TextMessage.class);
    // correlation ID is queried when response is being created...
    given(sentTextMessage.getJMSCorrelationID()).willReturn(CORRELATION_ID);
    // Reply-To is queried when response is being created...
    given(sentTextMessage.getJMSReplyTo()).willReturn(destination);
    TextMessage responseTextMessage = mock(TextMessage.class);
    MessageProducer messageProducer = mock(MessageProducer.class);
    willThrow(new JMSException("Doe!")).given(messageProducer).send(responseTextMessage);
    final QueueSession session = mock(QueueSession.class);
    given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
    given(session.createProducer(destination)).willReturn(messageProducer);
    ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
    given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);
    final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {

        @Override
        protected Object extractMessage(Message message) {
            return message;
        }
    };
    assertThatExceptionOfType(ReplyFailureException.class).isThrownBy(() -> adapter.onMessage(sentTextMessage, session)).withCauseExactlyInstanceOf(JMSException.class);
    verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
    verify(messageProducer).close();
    verify(delegate).handleMessage(sentTextMessage);
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) ObjectMessage(jakarta.jms.ObjectMessage) JMSException(jakarta.jms.JMSException) MessageProducer(jakarta.jms.MessageProducer) Queue(jakarta.jms.Queue) TextMessage(jakarta.jms.TextMessage) QueueSession(jakarta.jms.QueueSession) Test(org.junit.jupiter.api.Test)

Example 37 with JMSException

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

the class JmsTemplateTests method testExceptionStackTrace.

@Test
void testExceptionStackTrace() {
    JMSException jmsEx = new JMSException("could not connect");
    Exception innerEx = new Exception("host not found");
    jmsEx.setLinkedException(innerEx);
    JmsException springJmsEx = JmsUtils.convertJmsAccessException(jmsEx);
    StringWriter sw = new StringWriter();
    PrintWriter out = new PrintWriter(sw);
    springJmsEx.printStackTrace(out);
    String trace = sw.toString();
    assertThat(trace.indexOf("host not found") > 0).as("inner jms exception not found").isTrue();
}
Also used : StringWriter(java.io.StringWriter) UncategorizedJmsException(org.springframework.jms.UncategorizedJmsException) JmsException(org.springframework.jms.JmsException) JMSException(jakarta.jms.JMSException) MessageNotReadableException(org.springframework.jms.MessageNotReadableException) UncategorizedJmsException(org.springframework.jms.UncategorizedJmsException) JMSException(jakarta.jms.JMSException) InvalidDestinationException(org.springframework.jms.InvalidDestinationException) ResourceAllocationException(org.springframework.jms.ResourceAllocationException) MessageFormatException(org.springframework.jms.MessageFormatException) MessageEOFException(org.springframework.jms.MessageEOFException) InvalidClientIDException(org.springframework.jms.InvalidClientIDException) JmsException(org.springframework.jms.JmsException) TransactionInProgressException(org.springframework.jms.TransactionInProgressException) MessageNotWriteableException(org.springframework.jms.MessageNotWriteableException) TransactionRolledBackException(org.springframework.jms.TransactionRolledBackException) JmsSecurityException(org.springframework.jms.JmsSecurityException) InvalidSelectorException(org.springframework.jms.InvalidSelectorException) PrintWriter(java.io.PrintWriter) Test(org.junit.jupiter.api.Test)

Example 38 with JMSException

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

the class JmsMessagingTemplateTests method convertAndSendCustomJmsMessageConverter.

@Test
public void convertAndSendCustomJmsMessageConverter() throws JMSException {
    this.messagingTemplate.setJmsMessageConverter(new SimpleMessageConverter() {

        @Override
        public jakarta.jms.Message toMessage(Object object, Session session) throws JMSException, org.springframework.jms.support.converter.MessageConversionException {
            throw new org.springframework.jms.support.converter.MessageConversionException("Test exception");
        }
    });
    this.messagingTemplate.convertAndSend("myQueue", "msg to convert");
    verify(this.jmsTemplate).send(eq("myQueue"), this.messageCreator.capture());
    assertThatExceptionOfType(org.springframework.messaging.converter.MessageConversionException.class).isThrownBy(() -> this.messageCreator.getValue().createMessage(mock(Session.class))).withMessageContaining("Test exception");
}
Also used : StubTextMessage(org.springframework.jms.StubTextMessage) Message(org.springframework.messaging.Message) TextMessage(jakarta.jms.TextMessage) JMSException(jakarta.jms.JMSException) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

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