Search in sources :

Example 6 with Session

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

the class JmsTemplateTests method doTestSendAndReceive.

private void doTestSendAndReceive(boolean explicitDestination, boolean useDefaultDestination, long timeout) throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(this.connectionFactory);
    String destinationName = "testDestination";
    if (useDefaultDestination) {
        if (explicitDestination) {
            template.setDefaultDestination(this.queue);
        } else {
            template.setDefaultDestinationName(destinationName);
        }
    }
    template.setReceiveTimeout(timeout);
    Session localSession = getLocalSession();
    TemporaryQueue replyDestination = mock(TemporaryQueue.class);
    MessageProducer messageProducer = mock(MessageProducer.class);
    given(localSession.createProducer(this.queue)).willReturn(messageProducer);
    given(localSession.createTemporaryQueue()).willReturn(replyDestination);
    MessageConsumer messageConsumer = mock(MessageConsumer.class);
    given(localSession.createConsumer(replyDestination)).willReturn(messageConsumer);
    TextMessage request = mock(TextMessage.class);
    MessageCreator messageCreator = mock(MessageCreator.class);
    given(messageCreator.createMessage(localSession)).willReturn(request);
    TextMessage reply = mock(TextMessage.class);
    if (timeout == JmsTemplate.RECEIVE_TIMEOUT_NO_WAIT) {
        given(messageConsumer.receiveNoWait()).willReturn(reply);
    } else if (timeout == JmsTemplate.RECEIVE_TIMEOUT_INDEFINITE_WAIT) {
        given(messageConsumer.receive()).willReturn(reply);
    } else {
        given(messageConsumer.receive(timeout)).willReturn(reply);
    }
    Message message = null;
    if (useDefaultDestination) {
        message = template.sendAndReceive(messageCreator);
    } else if (explicitDestination) {
        message = template.sendAndReceive(this.queue, messageCreator);
    } else {
        message = template.sendAndReceive(destinationName, messageCreator);
    }
    // replyTO set on the request
    verify(request).setJMSReplyTo(replyDestination);
    assertThat(message).as("Reply message not received").isSameAs(reply);
    verify(this.connection).start();
    verify(this.connection).close();
    verify(localSession).close();
    verify(messageConsumer).close();
    verify(messageProducer).close();
}
Also used : MessageConsumer(jakarta.jms.MessageConsumer) Message(jakarta.jms.Message) TextMessage(jakarta.jms.TextMessage) TemporaryQueue(jakarta.jms.TemporaryQueue) MessageProducer(jakarta.jms.MessageProducer) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session)

Example 7 with Session

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

the class JmsTemplateTests method testSessionCallbackWithinSynchronizedTransaction.

@Test
void testSessionCallbackWithinSynchronizedTransaction() throws Exception {
    SingleConnectionFactory scf = new SingleConnectionFactory(this.connectionFactory);
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(scf);
    TransactionSynchronizationManager.initSynchronization();
    try {
        template.execute((SessionCallback<Void>) session -> {
            session.getTransacted();
            return null;
        });
        template.execute((SessionCallback<Void>) session -> {
            session.getTransacted();
            return null;
        });
        assertThat(ConnectionFactoryUtils.getTransactionalSession(scf, null, false)).isSameAs(this.session);
        assertThat(ConnectionFactoryUtils.getTransactionalSession(scf, scf.createConnection(), false)).isSameAs(this.session);
        TransactionAwareConnectionFactoryProxy tacf = new TransactionAwareConnectionFactoryProxy(scf);
        Connection tac = tacf.createConnection();
        Session tas = tac.createSession(false, Session.AUTO_ACKNOWLEDGE);
        tas.getTransacted();
        tas.close();
        tac.close();
        List<TransactionSynchronization> synchs = TransactionSynchronizationManager.getSynchronizations();
        assertThat(synchs.size()).isEqualTo(1);
        TransactionSynchronization synch = synchs.get(0);
        synch.beforeCommit(false);
        synch.beforeCompletion();
        synch.afterCommit();
        synch.afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
    } finally {
        TransactionSynchronizationManager.clearSynchronization();
        scf.destroy();
    }
    assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty()).isTrue();
    verify(this.connection).start();
    if (useTransactedTemplate()) {
        verify(this.session).commit();
    }
    verify(this.session).close();
    verify(this.connection).stop();
    verify(this.connection).close();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) MessageNotReadableException(org.springframework.jms.MessageNotReadableException) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) MessageProducer(jakarta.jms.MessageProducer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UncategorizedJmsException(org.springframework.jms.UncategorizedJmsException) JMSException(jakarta.jms.JMSException) TemporaryQueue(jakarta.jms.TemporaryQueue) Queue(jakarta.jms.Queue) Session(jakarta.jms.Session) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) InvalidDestinationException(org.springframework.jms.InvalidDestinationException) ResourceAllocationException(org.springframework.jms.ResourceAllocationException) SingleConnectionFactory(org.springframework.jms.connection.SingleConnectionFactory) MessageConsumer(jakarta.jms.MessageConsumer) BDDMockito.given(org.mockito.BDDMockito.given) Destination(jakarta.jms.Destination) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MessageFormatException(org.springframework.jms.MessageFormatException) Context(javax.naming.Context) ConnectionFactory(jakarta.jms.ConnectionFactory) MessageEOFException(org.springframework.jms.MessageEOFException) Connection(jakarta.jms.Connection) QosSettings(org.springframework.jms.support.QosSettings) InvalidClientIDException(org.springframework.jms.InvalidClientIDException) TransactionAwareConnectionFactoryProxy(org.springframework.jms.connection.TransactionAwareConnectionFactoryProxy) PrintWriter(java.io.PrintWriter) Message(jakarta.jms.Message) JmsException(org.springframework.jms.JmsException) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) StringWriter(java.io.StringWriter) JndiDestinationResolver(org.springframework.jms.support.destination.JndiDestinationResolver) JndiTemplate(org.springframework.jndi.JndiTemplate) TransactionInProgressException(org.springframework.jms.TransactionInProgressException) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) MessageNotWriteableException(org.springframework.jms.MessageNotWriteableException) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) JmsUtils(org.springframework.jms.support.JmsUtils) TextMessage(jakarta.jms.TextMessage) ConnectionFactoryUtils(org.springframework.jms.connection.ConnectionFactoryUtils) DeliveryMode(jakarta.jms.DeliveryMode) TransactionRolledBackException(org.springframework.jms.TransactionRolledBackException) JmsSecurityException(org.springframework.jms.JmsSecurityException) Mockito.reset(org.mockito.Mockito.reset) InvalidSelectorException(org.springframework.jms.InvalidSelectorException) Mockito.mock(org.mockito.Mockito.mock) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) SingleConnectionFactory(org.springframework.jms.connection.SingleConnectionFactory) Connection(jakarta.jms.Connection) TransactionAwareConnectionFactoryProxy(org.springframework.jms.connection.TransactionAwareConnectionFactoryProxy) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 8 with Session

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

the class JmsTemplateTests method doTestSendDestination.

/**
 * Common method for testing a send method that uses the MessageCreator
 * callback but with different QOS options.
 * @param ignoreQOS test using default QOS options.
 */
private void doTestSendDestination(boolean explicitDestination, boolean useDefaultDestination, boolean ignoreQOS, boolean disableIdAndTimestamp) throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(this.connectionFactory);
    String destinationName = "testDestination";
    if (useDefaultDestination) {
        if (explicitDestination) {
            template.setDefaultDestination(this.queue);
        } else {
            template.setDefaultDestinationName(destinationName);
        }
    }
    if (disableIdAndTimestamp) {
        template.setMessageIdEnabled(false);
        template.setMessageTimestampEnabled(false);
    }
    MessageProducer messageProducer = mock(MessageProducer.class);
    TextMessage textMessage = mock(TextMessage.class);
    given(this.session.createProducer(this.queue)).willReturn(messageProducer);
    given(this.session.createTextMessage("just testing")).willReturn(textMessage);
    if (!ignoreQOS) {
        template.setQosSettings(this.qosSettings);
    }
    if (useDefaultDestination) {
        template.send(session -> session.createTextMessage("just testing"));
    } else {
        if (explicitDestination) {
            template.send(this.queue, (MessageCreator) session -> session.createTextMessage("just testing"));
        } else {
            template.send(destinationName, (MessageCreator) session -> session.createTextMessage("just testing"));
        }
    }
    if (useTransactedTemplate()) {
        verify(this.session).commit();
    }
    if (disableIdAndTimestamp) {
        verify(messageProducer).setDisableMessageID(true);
        verify(messageProducer).setDisableMessageTimestamp(true);
    }
    if (ignoreQOS) {
        verify(messageProducer).send(textMessage);
    } else {
        verify(messageProducer).send(textMessage, this.qosSettings.getDeliveryMode(), this.qosSettings.getPriority(), this.qosSettings.getTimeToLive());
    }
    verify(messageProducer).close();
    verify(this.session).close();
    verify(this.connection).close();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) MessageNotReadableException(org.springframework.jms.MessageNotReadableException) SimpleMessageConverter(org.springframework.jms.support.converter.SimpleMessageConverter) MessageProducer(jakarta.jms.MessageProducer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UncategorizedJmsException(org.springframework.jms.UncategorizedJmsException) JMSException(jakarta.jms.JMSException) TemporaryQueue(jakarta.jms.TemporaryQueue) Queue(jakarta.jms.Queue) Session(jakarta.jms.Session) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) InvalidDestinationException(org.springframework.jms.InvalidDestinationException) ResourceAllocationException(org.springframework.jms.ResourceAllocationException) SingleConnectionFactory(org.springframework.jms.connection.SingleConnectionFactory) MessageConsumer(jakarta.jms.MessageConsumer) BDDMockito.given(org.mockito.BDDMockito.given) Destination(jakarta.jms.Destination) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MessageFormatException(org.springframework.jms.MessageFormatException) Context(javax.naming.Context) ConnectionFactory(jakarta.jms.ConnectionFactory) MessageEOFException(org.springframework.jms.MessageEOFException) Connection(jakarta.jms.Connection) QosSettings(org.springframework.jms.support.QosSettings) InvalidClientIDException(org.springframework.jms.InvalidClientIDException) TransactionAwareConnectionFactoryProxy(org.springframework.jms.connection.TransactionAwareConnectionFactoryProxy) PrintWriter(java.io.PrintWriter) Message(jakarta.jms.Message) JmsException(org.springframework.jms.JmsException) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) StringWriter(java.io.StringWriter) JndiDestinationResolver(org.springframework.jms.support.destination.JndiDestinationResolver) JndiTemplate(org.springframework.jndi.JndiTemplate) TransactionInProgressException(org.springframework.jms.TransactionInProgressException) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) MessageNotWriteableException(org.springframework.jms.MessageNotWriteableException) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) JmsUtils(org.springframework.jms.support.JmsUtils) TextMessage(jakarta.jms.TextMessage) ConnectionFactoryUtils(org.springframework.jms.connection.ConnectionFactoryUtils) DeliveryMode(jakarta.jms.DeliveryMode) TransactionRolledBackException(org.springframework.jms.TransactionRolledBackException) JmsSecurityException(org.springframework.jms.JmsSecurityException) Mockito.reset(org.mockito.Mockito.reset) InvalidSelectorException(org.springframework.jms.InvalidSelectorException) Mockito.mock(org.mockito.Mockito.mock) MessageProducer(jakarta.jms.MessageProducer) TextMessage(jakarta.jms.TextMessage)

Example 9 with Session

use of jakarta.jms.Session 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 10 with Session

use of jakarta.jms.Session 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)

Aggregations

Session (jakarta.jms.Session)84 Test (org.junit.jupiter.api.Test)71 Connection (jakarta.jms.Connection)30 Message (jakarta.jms.Message)27 TextMessage (jakarta.jms.TextMessage)26 StubTextMessage (org.springframework.jms.StubTextMessage)22 ConnectionFactory (jakarta.jms.ConnectionFactory)21 JMSException (jakarta.jms.JMSException)20 MessageProducer (jakarta.jms.MessageProducer)19 Destination (jakarta.jms.Destination)18 MessagingMessageListenerAdapter (org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter)16 Queue (jakarta.jms.Queue)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 BDDMockito.given (org.mockito.BDDMockito.given)12 Mockito.mock (org.mockito.Mockito.mock)12 Mockito.verify (org.mockito.Mockito.verify)12 StubQueue (org.springframework.jms.StubQueue)12 ObjectMessage (jakarta.jms.ObjectMessage)11 QueueSession (jakarta.jms.QueueSession)11 SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)11