Search in sources :

Example 51 with ConnectionFactory

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

the class SimpleMessageListenerContainer method processMessage.

/**
 * Process a message received from the provider.
 * <p>Executes the listener, exposing the current JMS Session as
 * thread-bound resource (if "exposeListenerSession" is "true").
 * @param message the received JMS Message
 * @param session the JMS Session to operate on
 * @see #executeListener
 * @see #setExposeListenerSession
 */
protected void processMessage(Message message, Session session) {
    ConnectionFactory connectionFactory = getConnectionFactory();
    boolean exposeResource = (connectionFactory != null && isExposeListenerSession());
    if (exposeResource) {
        TransactionSynchronizationManager.bindResource(connectionFactory, new LocallyExposedJmsResourceHolder(session));
    }
    try {
        executeListener(session, message);
    } finally {
        if (exposeResource) {
            TransactionSynchronizationManager.unbindResource(getConnectionFactory());
        }
    }
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory)

Example 52 with ConnectionFactory

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

the class JmsTemplate method doReceive.

/**
 * Actually receive a JMS message.
 * @param session the JMS Session to operate on
 * @param consumer the JMS MessageConsumer to receive with
 * @return the JMS Message received, or {@code null} if none
 * @throws JMSException if thrown by JMS API methods
 */
@Nullable
protected Message doReceive(Session session, MessageConsumer consumer) throws JMSException {
    try {
        // Use transaction timeout (if available).
        long timeout = getReceiveTimeout();
        ConnectionFactory connectionFactory = getConnectionFactory();
        JmsResourceHolder resourceHolder = null;
        if (connectionFactory != null) {
            resourceHolder = (JmsResourceHolder) TransactionSynchronizationManager.getResource(connectionFactory);
        }
        if (resourceHolder != null && resourceHolder.hasTimeout()) {
            timeout = Math.min(timeout, resourceHolder.getTimeToLiveInMillis());
        }
        Message message = receiveFromConsumer(consumer, timeout);
        if (session.getTransacted()) {
            // Commit necessary - but avoid commit call within a JTA transaction.
            if (isSessionLocallyTransacted(session)) {
                // Transacted session created by this template -> commit.
                JmsUtils.commitIfNecessary(session);
            }
        } else if (isClientAcknowledge(session)) {
            // Manually acknowledge message, if any.
            if (message != null) {
                message.acknowledge();
            }
        }
        return message;
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
    }
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) JmsResourceHolder(org.springframework.jms.connection.JmsResourceHolder) Message(jakarta.jms.Message) Nullable(org.springframework.lang.Nullable)

Example 53 with ConnectionFactory

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

the class TransactionAwareConnectionFactoryProxy method createQueueConnection.

@Override
public QueueConnection createQueueConnection(String username, String password) throws JMSException {
    ConnectionFactory target = getTargetConnectionFactory();
    if (!(target instanceof QueueConnectionFactory)) {
        throw new jakarta.jms.IllegalStateException("'targetConnectionFactory' is no QueueConnectionFactory");
    }
    QueueConnection targetConnection = ((QueueConnectionFactory) target).createQueueConnection(username, password);
    return (QueueConnection) getTransactionAwareConnectionProxy(targetConnection);
}
Also used : TopicConnectionFactory(jakarta.jms.TopicConnectionFactory) ConnectionFactory(jakarta.jms.ConnectionFactory) QueueConnectionFactory(jakarta.jms.QueueConnectionFactory) QueueConnection(jakarta.jms.QueueConnection) QueueConnectionFactory(jakarta.jms.QueueConnectionFactory)

Example 54 with ConnectionFactory

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

the class TransactionAwareConnectionFactoryProxy method createTopicConnection.

@Override
public TopicConnection createTopicConnection(String username, String password) throws JMSException {
    ConnectionFactory target = getTargetConnectionFactory();
    if (!(target instanceof TopicConnectionFactory)) {
        throw new jakarta.jms.IllegalStateException("'targetConnectionFactory' is no TopicConnectionFactory");
    }
    TopicConnection targetConnection = ((TopicConnectionFactory) target).createTopicConnection(username, password);
    return (TopicConnection) getTransactionAwareConnectionProxy(targetConnection);
}
Also used : TopicConnectionFactory(jakarta.jms.TopicConnectionFactory) TopicConnectionFactory(jakarta.jms.TopicConnectionFactory) ConnectionFactory(jakarta.jms.ConnectionFactory) QueueConnectionFactory(jakarta.jms.QueueConnectionFactory) TopicConnection(jakarta.jms.TopicConnection)

Example 55 with ConnectionFactory

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

the class JmsGatewaySupportTests method testJmsGatewaySupportWithConnectionFactory.

@Test
void testJmsGatewaySupportWithConnectionFactory() throws Exception {
    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    final List<String> test = new ArrayList<>(1);
    JmsGatewaySupport gateway = new JmsGatewaySupport() {

        @Override
        protected void initGateway() {
            test.add("test");
        }
    };
    gateway.setConnectionFactory(mockConnectionFactory);
    gateway.afterPropertiesSet();
    assertThat(gateway.getConnectionFactory()).as("Correct ConnectionFactory").isEqualTo(mockConnectionFactory);
    assertThat(gateway.getJmsTemplate().getConnectionFactory()).as("Correct JmsTemplate").isEqualTo(mockConnectionFactory);
    assertThat(test.size()).as("initGateway called").isEqualTo(1);
}
Also used : ConnectionFactory(jakarta.jms.ConnectionFactory) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionFactory (jakarta.jms.ConnectionFactory)60 Test (org.junit.jupiter.api.Test)42 Connection (jakarta.jms.Connection)32 JMSException (jakarta.jms.JMSException)20 Session (jakarta.jms.Session)19 Message (jakarta.jms.Message)15 QueueConnectionFactory (jakarta.jms.QueueConnectionFactory)15 TopicConnectionFactory (jakarta.jms.TopicConnectionFactory)15 JmsTemplate (org.springframework.jms.core.JmsTemplate)11 QueueConnection (jakarta.jms.QueueConnection)10 TopicConnection (jakarta.jms.TopicConnection)10 StubQueue (org.springframework.jms.StubQueue)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 BDDMockito.given (org.mockito.BDDMockito.given)9 Mockito.mock (org.mockito.Mockito.mock)9 Mockito.verify (org.mockito.Mockito.verify)9 TransactionStatus (org.springframework.transaction.TransactionStatus)9 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)9 Destination (jakarta.jms.Destination)8 MessageProducer (jakarta.jms.MessageProducer)8