Search in sources :

Example 1 with QueueSession

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

the class SingleConnectionFactoryTests method testCachingConnectionFactoryWithQueueConnectionFactoryAndJms102Usage.

@Test
public void testCachingConnectionFactoryWithQueueConnectionFactoryAndJms102Usage() throws JMSException {
    QueueConnectionFactory cf = mock(QueueConnectionFactory.class);
    QueueConnection con = mock(QueueConnection.class);
    QueueSession txSession = mock(QueueSession.class);
    QueueSession nonTxSession = mock(QueueSession.class);
    given(cf.createQueueConnection()).willReturn(con);
    given(con.createQueueSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession);
    given(txSession.getTransacted()).willReturn(true);
    given(con.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession);
    CachingConnectionFactory scf = new CachingConnectionFactory(cf);
    scf.setReconnectOnException(false);
    Connection con1 = scf.createQueueConnection();
    Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session1.rollback();
    session1.close();
    session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    session1.close();
    con1.start();
    QueueConnection con2 = scf.createQueueConnection();
    Session session2 = con2.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
    session2.close();
    session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session2.getTransacted();
    // should lead to rollback
    session2.close();
    con2.start();
    con1.close();
    con2.close();
    // should trigger actual close
    scf.destroy();
    verify(txSession).rollback();
    verify(txSession).close();
    verify(nonTxSession).close();
    verify(con).start();
    verify(con).stop();
    verify(con).close();
}
Also used : QueueConnection(jakarta.jms.QueueConnection) QueueConnectionFactory(jakarta.jms.QueueConnectionFactory) QueueConnection(jakarta.jms.QueueConnection) TopicConnection(jakarta.jms.TopicConnection) Connection(jakarta.jms.Connection) QueueSession(jakarta.jms.QueueSession) QueueSession(jakarta.jms.QueueSession) Session(jakarta.jms.Session) TopicSession(jakarta.jms.TopicSession) Test(org.junit.jupiter.api.Test)

Example 2 with QueueSession

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

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateNoDefaultDestinationAndNoReplyToDestination_SendsReturnTextMessageWhenSessionSupplied.

@Test
void testWithResponsiveMessageDelegateNoDefaultDestinationAndNoReplyToDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception {
    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(null);
    TextMessage responseTextMessage = mock(TextMessage.class);
    final QueueSession session = mock(QueueSession.class);
    given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
    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(InvalidDestinationException.class);
    verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
    verify(delegate).handleMessage(sentTextMessage);
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) ObjectMessage(jakarta.jms.ObjectMessage) TextMessage(jakarta.jms.TextMessage) QueueSession(jakarta.jms.QueueSession) Test(org.junit.jupiter.api.Test)

Example 3 with QueueSession

use of jakarta.jms.QueueSession 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 4 with QueueSession

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

the class MessageListenerAdapterTests method testWithResponsiveMessageDelegateDoesNotSendReturnTextMessageWhenSessionSupplied_AndListenerMethodThrowsException.

@Test
void testWithResponsiveMessageDelegateDoesNotSendReturnTextMessageWhenSessionSupplied_AndListenerMethodThrowsException() throws Exception {
    final TextMessage message = mock(TextMessage.class);
    final QueueSession session = mock(QueueSession.class);
    ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
    willThrow(new IllegalArgumentException("Doe!")).given(delegate).handleMessage(message);
    final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {

        @Override
        protected Object extractMessage(Message message) {
            return message;
        }
    };
    assertThatExceptionOfType(ListenerExecutionFailedException.class).isThrownBy(() -> adapter.onMessage(message, session));
}
Also used : Message(jakarta.jms.Message) BytesMessage(jakarta.jms.BytesMessage) TextMessage(jakarta.jms.TextMessage) ObjectMessage(jakarta.jms.ObjectMessage) TextMessage(jakarta.jms.TextMessage) QueueSession(jakarta.jms.QueueSession) Test(org.junit.jupiter.api.Test)

Aggregations

QueueSession (jakarta.jms.QueueSession)4 Test (org.junit.jupiter.api.Test)4 BytesMessage (jakarta.jms.BytesMessage)3 Message (jakarta.jms.Message)3 ObjectMessage (jakarta.jms.ObjectMessage)3 TextMessage (jakarta.jms.TextMessage)3 Connection (jakarta.jms.Connection)1 JMSException (jakarta.jms.JMSException)1 MessageProducer (jakarta.jms.MessageProducer)1 Queue (jakarta.jms.Queue)1 QueueConnection (jakarta.jms.QueueConnection)1 QueueConnectionFactory (jakarta.jms.QueueConnectionFactory)1 Session (jakarta.jms.Session)1 TopicConnection (jakarta.jms.TopicConnection)1 TopicSession (jakarta.jms.TopicSession)1