Java Code Examples for org.springframework.jms.connection.JmsTransactionManager

Following code examples demonstrate how to use org.springframework.jms.connection.JmsTransactionManagerfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.jms.connection.JmsTransactionManagerand various code implementation of this class.

	private void addTestJmsComponent() {

		org.springframework.jms.connection.JmsTransactionManager transactionManager = new org.springframework.jms.connection.JmsTransactionManager();
		transactionManager.setConnectionFactory(connectionFactory);

		ActiveMQComponent c = new ActiveMQComponent();
		c.setConnectionFactory(connectionFactory);
		c.setTransacted(true);

		c.setTransactionManager((JmsTransactionManager) context.getRegistry()
				.lookup("txManager"));
		context.addComponent("jms", c);

	} 


    @Override
    protected ActiveMQConfiguration createActiveMQConfiguration()
    {
        ActiveMQConfiguration config = super.createActiveMQConfiguration();
        config.setTransactionManager(new JmsTransactionManager(config.getConnectionFactory()));
        return config;
    } 

  @Bean
  public JmsTransactionManager transactionManager(
      ConnectionFactory connectionFactory) {
    return new JmsTransactionManager(connectionFactory);
  } 

  @Bean
  public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(
      ConnectionFactory connectionFactory,
      JmsTransactionManager transactionManager) {
    DefaultJmsListenerContainerFactory factory =
        new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setTransactionManager(transactionManager);
    return factory;
  } 

	private void addTestJmsComponent() {

		org.springframework.jms.connection.JmsTransactionManager transactionManager = new org.springframework.jms.connection.JmsTransactionManager();
		transactionManager.setConnectionFactory(connectionFactory);

		ActiveMQComponent c = new ActiveMQComponent();
		c.setConnectionFactory(connectionFactory);
		c.setTransacted(true);

		c.setTransactionManager((JmsTransactionManager) context.getRegistry()
				.lookup("txManager"));
		context.addComponent("jms", c);

	} 

    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory =  new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setTransactionManager(new JmsTransactionManager(connectionFactory));
        factory.setSubscriptionDurable(true);
        return factory;
    } 

    @Override
    protected ActiveMQConfiguration createActiveMQConfiguration()
    {
        ActiveMQConfiguration config = super.createActiveMQConfiguration();
        config.setTransactionManager(new JmsTransactionManager(config.getConnectionFactory()));
        return config;
    } 

Advertisement
Javadoc
org.springframework.transaction.PlatformTransactionManager implementation for a single JMS javax.jms

.ConnectionFactory. Binds a JMS Connection/Session pair from the specified ConnectionFactory to the thread, potentially allowing for one thread-bound Session per ConnectionFactory.

This local strategy is an alternative to executing JMS operations within JTA transactions. Its advantage is that it is able to work in any environment, for example a standalone application or a test suite, with any message broker as target. However, this strategy is not able to provide XA transactions, for example in order to share transactions between messaging and database access. A full JTA/XA setup is required for XA transactions, typically using Spring's org.springframework.transaction.jta.JtaTransactionManager as strategy.

Application code is required to retrieve the transactional JMS Session via ConnectionFactoryUtils#getTransactionalSession instead of a standard Java EE-style ConnectionFactory#createConnection() call with subsequent Session creation. Spring's org.springframework.jms.core.JmsTemplate will autodetect a thread-bound Session and automatically participate in it.

Alternatively, you can allow application code to work with the standard Java EE-style lookup pattern on a ConnectionFactory, for example for legacy code that is not aware of Spring at all. In that case, define a TransactionAwareConnectionFactoryProxy for your target ConnectionFactory, which will automatically participate in Spring-managed transactions.

The use of CachingConnectionFactory as a target for this transaction manager is strongly recommended. CachingConnectionFactory uses a single JMS Connection for all JMS access in order to avoid the overhead of repeated Connection creation, as well as maintaining a cache of Sessions. Each transaction will then share the same JMS Connection, while still using its own individual JMS Session.

The use of a raw target ConnectionFactory would not only be inefficient because of the lack of resource reuse. It might also lead to strange effects when your JMS driver doesn't accept MessageProducer.close() calls and/or MessageConsumer.close() calls before Session.commit(), with the latter supposed to commit all the messages that have been sent through the producer handle and received through the consumer handle. As a safe general solution, always pass in a CachingConnectionFactory into this transaction manager's #setConnectionFactory "connectionFactory" property.

Transaction synchronization is turned off by default, as this manager might be used alongside a datastore-based Spring transaction manager such as the JDBC org.springframework.jdbc.datasource.DataSourceTransactionManager, which has stronger needs for synchronization. @author Juergen Hoeller @since 1.1 @see ConnectionFactoryUtils#getTransactionalSession @see TransactionAwareConnectionFactoryProxy @see org.springframework.jms.core.JmsTemplate

Read More
Advertisement