Should I use HornetQ JMS API to integrate with Spring TransactionManager? - spring

I have a basic doubt regarding Transactions and the HornetQ native API.
If my application is Spring-based, with Spring managed transactions, how should I configure HornetQ to participate in the same transaction managed by Spring (AOP)?
Using the JMS API I guess it's fairly obvious, since Spring considers JMS resources as transactional.
But I don't know how to do it correctly using the nativa API. Could someone shed a light on this problem?
Many thanks!

HornetQ supports XA on the core API, they are just resources.
You should be able to enlist these resources on Spring if that's just using regular XA API.
http://docs.jboss.org/hornetq/2.2.5.Final/api/org/hornetq/api/core/client/ClientSession.html
I'm not sure though how that integration is done on Spring. If there's anything not working it could (and should) be fixed or improved on a spring-integration module.

Not sure if I understand correctly, but if you're trying to simply do declarative transaction managment using Spring and the #Transactional annotation for JUST JMS, then simply use the <tx:annotation-driven transaction-manager = "myTransactionManager" /> and then - for the "myTranactionManager," register a bean of type JmsTransactionManager, e.g.,
#Bean public PlatformTransactionManager myTranactionManager (){
return new JmsTransactionManager( this.connectionFactory());
}
Now, you can use the annotation as normal and the JMS interactions will be wrapped in a transaction
#Transactional
public void doSomethingWithJms(){
jmsTemplate.send(...);
jmsTemplate.convertAndSend(...)
}
If your goal is to work with MULTIPLE resources, e.g., JMS and JDBC, then you need to use JTA. To see how to setup JTA (for JPA and JMS), check out this blog post http://blog.springsource.com/2011/08/15/configuring-spring-and-jta-without-full-java-ee/

Related

JMS configuration for Spring Integration

I am trying to implement activemq(just want to receive messages) with spring integration.I cant find any clues how to provide java configuration for activemq. What are the minimum required components for job. Somewhere we have channel, adapter somewhere we dont. I am unable to understand spring concepts of adapter, channel and service activator. They are all feeling same to me. I find the integration documentation going above my head. I never had problems with understanding other spring modules(boot, mvc, cloud, batch). Can someone point me in the right direction or what is it that I am doing wrong.
You probably are missing the fact that Spring Integration is a reference implementation for well-known Enterprise Integration Patterns. So, please, consider to start from the theory and ideas. Then you can come back to Spring Integration as an API for those EIP. See respective book on the matter: https://www.enterpriseintegrationpatterns.com.
To read messages from JMS destination you need to use a JmsMessageDrivenEndpoint with respective ConnectionFactory injected.
There is nothing more about that than an ActiveMQConnectionFactory as a bean.
For example in tests we do like this:
new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false")
And an in-memory broker is started.
See a test class with Java DSL for some way how to configure JMS components: https://github.com/spring-projects/spring-integration/blob/master/spring-integration-jms/src/test/java/org/springframework/integration/jms/dsl/JmsTests.java

Is JTA recommended with Hibernate in Springboot

I have to use hibernate along with spring boot. Is it recommended to use JTA transaction manager in this stack?
If JTA is recommended, how to access current session programmatically in controller or service layer ? with example will be more helpful.
As spring documentation says:
Typically, you need an application server’s JTA capability only if your application needs to handle transactions across multiple resources, which is not a requirement for many applications.
And according to spring boot documentation:
Spring Boot supports distributed JTA transactions across multiple XA resources by using either an Atomikos or Bitronix embedded transaction manager. JTA transactions are also supported when deploying to a suitable Java EE Application Server.
When a JTA environment is detected, Spring’s JtaTransactionManager is used to manage transactions. Auto-configured JMS, DataSource, and JPA beans are upgraded to support XA transactions. You can use standard Spring idioms, such as #Transactional, to participate in a distributed transaction.
As for your second question you can have a look at this answer.

Spring Transaction Management usage

Can anybody provide me an example for Spring Transaction management? I have queries like how it basically works? In Java EE , Application server container used to take care of the transaction using EJB's. I wanted to know ,how spring frameworks helps in the same way?
I hope Introduction to Spring Framework transaction management will help.
The Spring Framework’s transaction management support doesn't requires an application server.
Spring Frameworks Claims declarative transactions of spring framework offer more powerful and more productive programming model than EJB CMT.
Compared to transaction management using EJB Spring frameworks enables application developers to use a consistent programming model in any environment. Once we write our code once it can benefit from different transaction management strategies in different environments.
The Spring Framework provides both declarative and programmatic transaction management.
programmatic transaction management - developers work with the Spring Framework transaction abstraction, which can run over any underlying transaction infrastructure.
declarative model - developers typically write little or no code related to transaction management, and hence do not depend on the Spring Framework transaction API, or any other transaction API.
Spring framework provides central interface for transaction
management i.e. 'PlatformTransactionManager'
There are many implementations if it, one which you can quickly
relate to is DataSourceTransactionManager.
Now, this transaction manager does the lower level work of beginning,
rollback and commit of transaction for you.
If you see the source of DataSourceTransactionManager you will see
the same kind of transaction management code that you have seen when
you handle transaction using JDBC api
But importantly, all these things happen declaratively as part of
proxy advise using Spring AOP
Begin Transaction
DataSource ds = /*initialize DS here*/
Connection con = ds.getConnection();
con.setAutoCommit(false);
Commit Transaction
com.commit();

Spring Boot JMS only transactions support

I'm writing a service and I need to use transactions for JDBC & JMS but not distributed.Is it possible use Spring #Transactional annotation on JMS Listener method for a JMS only Transacion using Bitronix or Atomikos since these 2 are also autoconfigured by Spring ?
As far as I know, when I add this dependency, my dataBase transaction also became XA.
What are my options ?
Please see the boot documentation about Mixing XA and non-XA JMS connections.

Spring JMS 2-phase-commit in java SE

I am not running under Java EE.
I want to have an XA transaction using Spring to share a transaction between DB and JMS.
Does spring provide such a functionality or must I use an external transaction manager such as Atomikos?
I use currently the DataSourceTransactionManager for the DB, and I see I can also use the JMSTransactionManager. Do they work together? Not clear from the documentation as JtaTransactionManager is mentioned.
Please advise.
Yair
Spring only provides a framework for transaction management, it as such doesn't provide any transaction manager. If you are running your application outside a Java EE container and you need a transaction between resources like a DB and JMS, you have to use an external TransactionManager like Atomikos or JOTM (Java Open Transaction Manager).
You might want to refer to http://www.javaworld.com/javaworld/jw-04-2007/jw-04-xa.html for more details on XA using Spring.
they are resource local, but Spring does support XA (see this post for explanation and example code) : http://blog.springsource.com/2011/08/15/configuring-spring-and-jta-without-full-java-ee/

Resources