Search in sources :

Example 1 with SingleConnectionFactory

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;
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) SingleConnectionFactory(org.springframework.jms.connection.SingleConnectionFactory) PooledConnectionFactory(org.apache.activemq.jms.pool.PooledConnectionFactory) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Bean(org.springframework.context.annotation.Bean) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty)

Example 2 with 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;
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) SingleConnectionFactory(org.springframework.jms.connection.SingleConnectionFactory) PooledConnectionFactory(org.apache.activemq.jms.pool.PooledConnectionFactory) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Bean(org.springframework.context.annotation.Bean)

Example 3 with 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();
}
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)

Aggregations

SingleConnectionFactory (org.springframework.jms.connection.SingleConnectionFactory)3 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)2 PooledConnectionFactory (org.apache.activemq.jms.pool.PooledConnectionFactory)2 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)2 Bean (org.springframework.context.annotation.Bean)2 Connection (jakarta.jms.Connection)1 ConnectionFactory (jakarta.jms.ConnectionFactory)1 DeliveryMode (jakarta.jms.DeliveryMode)1 Destination (jakarta.jms.Destination)1 JMSException (jakarta.jms.JMSException)1 Message (jakarta.jms.Message)1 MessageConsumer (jakarta.jms.MessageConsumer)1 MessageProducer (jakarta.jms.MessageProducer)1 Queue (jakarta.jms.Queue)1 Session (jakarta.jms.Session)1 TemporaryQueue (jakarta.jms.TemporaryQueue)1 TextMessage (jakarta.jms.TextMessage)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 List (java.util.List)1