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());
}
}
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations