use of org.springframework.jms.connection.SingleConnectionFactory in project elephant by yanghuijava.
the class ActivemqConfig method singleConnectionFactory.
@Bean
@ConditionalOnProperty(prefix = "elephant.mq", value = "activemq-broker-url")
public SingleConnectionFactory singleConnectionFactory() {
ActiveMQConnectionFactory targetConnectionFactory = new ActiveMQConnectionFactory(this.mqProperties.getActivemqBrokerUrl());
if (!StringUtils.isEmpty(this.mqProperties.getActivemqUserName()) && !StringUtils.isEmpty(this.mqProperties.getActivemqPassword())) {
targetConnectionFactory.setUserName(this.mqProperties.getActivemqUserName());
targetConnectionFactory.setPassword(this.mqProperties.getActivemqPassword());
} else {
targetConnectionFactory.setUserName(ActiveMQConnectionFactory.DEFAULT_USER);
targetConnectionFactory.setPassword(ActiveMQConnectionFactory.DEFAULT_PASSWORD);
}
PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory();
pooledConnectionFactory.setConnectionFactory(targetConnectionFactory);
pooledConnectionFactory.setMaxConnections(this.mqProperties.getActivemqPoolMaxConnections());
SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory();
singleConnectionFactory.setTargetConnectionFactory(pooledConnectionFactory);
return singleConnectionFactory;
}
use of org.springframework.jms.connection.SingleConnectionFactory in project elephant by yanghuijava.
the class ActivemqConfig method singleConnectionFactory.
@Bean
public SingleConnectionFactory singleConnectionFactory(RedeliveryPolicy redeliveryPolicy) {
ActiveMQConnectionFactory targetConnectionFactory = new ActiveMQConnectionFactory(this.mqProperties.getActivemqBrokerUrl());
if (!StringUtils.isEmpty(this.mqProperties.getActivemqUserName()) && !StringUtils.isEmpty(this.mqProperties.getActivemqPassword())) {
targetConnectionFactory.setUserName(this.mqProperties.getActivemqUserName());
targetConnectionFactory.setPassword(this.mqProperties.getActivemqPassword());
} else {
targetConnectionFactory.setUserName(ActiveMQConnectionFactory.DEFAULT_USER);
targetConnectionFactory.setPassword(ActiveMQConnectionFactory.DEFAULT_PASSWORD);
}
targetConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);
PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory();
pooledConnectionFactory.setConnectionFactory(targetConnectionFactory);
pooledConnectionFactory.setMaxConnections(this.mqProperties.getActivemqPoolMaxConnections());
SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory();
singleConnectionFactory.setTargetConnectionFactory(pooledConnectionFactory);
return singleConnectionFactory;
}
use of org.springframework.jms.connection.SingleConnectionFactory 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();
}
Aggregations