Global Transaction Handling in Spring Integration - spring-integration

Its a Spring Integration application.I have a requirement where I need to persist to DB and then post to Queue/Topic. This should be part of a single transaction. I am planning to use the JTATransactionManager. Application Server is Tomcat. Would someone please provide some sample configuration code required for this implementation.

You need a third party stand-alone XA transaction manager such as Atomikos. Tomcat doesn't have one.
You might also consider alternatives to using full-blown 2pc.

Related

Can I use spring integration in stand alone application?

I am new to spring integration, and was wondering if I can use spring integration in an application that is stand alone.
The various services or workflows are communicating via channels , by posting messages. I want to control the flow of application decoratively and I find spring integration quite interesting in this regard.
Please advice.
Yes; one of the design goals for Spring Integration was to enable loose coupling of components within an application where they exchange messages.

How does Spring Integration "internal" messaging work?

Perhaps a silly question, but keep reading about SIs "lightweight messaging within Spring-based applications". I want to know how (if) SI uses messaging internally. When I run an SI (Boot) application (one that doesn't require AMPQ ... aka 'messaging' support), I don't have to run a Rabbit server. But, from what I gather, SI uses messaging internally. How is this accomplished? I can't seem to find any reference explaining this & what infrastructure is required to make this possible. Thanks!
The messages are simply Java objects (o.s.messaging.Message) passed between components. No external broker is needed, unless you need persistence.
I suggest you read Mark Fisher's book (Spring Integration in Action) and/or the reference manual.
The messaging inside spring integration are in-memory java objects passed from one service to another via channels/queue. It provides a mechanism to define the flow and order of processing, also allowing each service step to work in isolation. The spring integration queue is eventually an implementation of java.util.Queue interface.
It is different from commercial Messaging tools like IBM MQ or Active MQ as it doesnt offer persistence. Which means if you kill the jvm or the app process is stopped, all the messages in flight on the Spring queue/channel are lost. A lot if times this is acceptable if the process in idempotent, i.e When the application comes up, I can restart the process from beginning.

Which library of spring should I use to send emails with multy-threading

I have too many emails. I should write scheduler in order to send messages to them. Messages are different. I use spring framework 4.x.
I can write simple class, which connects to SMTP server. But in this case I should write my thread library too in order to send emails parallel.
Do spring have already written library which give me more flexible way to do this tasks? I do not want to use threads. It will be nice if spring already have this functionality.
Do I need Spring integration for this?
Best regards,
Yes, you definitely can do that with Spring Integration, because there is an ExecutorChannel implementation with can be supplied with an TaskExecutor from the Spring Core:
<channel id="sendEmailChannel">
<dispatcher task-executor="threadPoolTaskExecutor"/>
</channel>
<int-mail:outbound-channel-adapter channel="sendEmailChannel" mail-sender="mailSender"/>
But anyway you should keep in mind that all Spring Integration components are based on the Java and that ExecutorService is used on the background.
From other side if you need only the mail sending stuff from the Spring Integration, it would be an overhead and can simply use Core Spring Framework legacy like JavaMailSender as a bean and #Async for the sendMail method to achieve your parallel requirement.
UPDATE
could you tell me whether I need JMS for this situation?
I don't see any JMS-related stuff here. You don't have (or at least don't show) any real integration points in your solution. The same I can say even about Spring Integration just for email sending. However with the Spring Boot your SI config will be enough short. From other side if you'll study Spring Integration better eventually you'll get more gain to rely on the Integration components for your systems, as internally, as well as externally with other systems through JMS, AMQP, Kafka etc.
To be honest: a lot of years ago my first acquaintance with Spring Integration was due the requirement to get files from the FTP and have ability to pick up new files automatically. I found the solution only in the Spring Integration 1.0.0.M1. After that short XML config for the <int-ftp:inbound-channel-adapter> I loved Spring Integration and since that time it became as a part of my life. :-)
So, it's up to you to go ahead with Spring Integration in your simple app, or just follow with more formal solution with JavaMailSender direct usage.
You should use java executors framework. For example you can write something like the code below:
ExecutorService executor = Executors.newWorkStealingPool();
executor.execute(() -> mailSender.send(mail));

Spring Hibernate SessionFactory.getCurrentSession in multithreaded environment

I have written a batch application which spawns multiple threads to read assigned files and save records to database. The architecture uses Spring context and Hibernate.
Transaction is managed by Spring and I am using SessionFactory.getCurrentSession to get a session to perform a save operation for each thread.
Consider that I have a generic DAO that handles get, save, update operations and a facade to hide Hibernate implementation, how can I be assured that two threads when invoking SessionFactory.getCurrentSession() are getting their dedicated Session object to perform DB operations.
I found a post in StackOverflow where someone recommended not to use current_session_context_class=thread when using spring managed transaction. what is the default implementation used by Spring for current_session_context_class property?
Thanks in Advance!
As of Spring 2.0 Spring integrates with hibernate throuhg its own implementation(s) of the CurrentSessionContext interface provided by hibernate.
By default spring sets this to the SpringSessionContext to integrate properly. In general you don't want or need to mess with the current_session_context_class unless you are using JTA (although when using Hibernate 4 with a recent Spring version it should also just work).

Opensource ESB vs Apache ServiceMix vs Spring Integration?

I am new to the enterprise integration area.
We have a requirement to develop a solution where multiple OSS (operations support systems) should talk to multiple EMS (element management systems) and network devices (Different transports and protocols has to be supported), solution should be such that, that it should run in Weblogic.
Queries
Which will be the best fit for this situation ESB/Apache ServiceMix/Spring Integration?
If we use opensource ESBs ( like WSo2 and Talend ESBs) I think we need to maintain two servers ESB server and Weblogic server and ESB/Weblogic integration will be an issue?
Apache servicemix or Spring Integration be deployed/run inside Weblogic?
Whether Apache ServiceMix is supported now, as I could see most updates are happening in fuse ESB only?
You need to analyze your scenario and then decide. If you need only transformation or alongside with a simple routing you can use some frameworks like smooks, camel etc.
You need to transform and still a lot of system involved where you need those transformed messages then you could use an ESB.
Then comes selecting the ESB product is also based on you application eco system. All products are amazing and each fits the better than the other in their own application eco system.
First you need to know a few things on Camel / Fuse ESB / Service Mix
All the above revolve around the same, each of them are projects where camel integration framework is the coding De fact o
1.Camel -- Integration framework and the De fact o coding way(sophisticated in its own way and much flexible)
2.Service Mix -- Container for deploying the your integration code. (Camel integration code)
3.Fuse ESB -- Enterprise Feather on the hat of Service Mix where it provides a Studio for coding , a list of components and wrappers like clustering and other facilities around service service mix
.
I would like you to also consider Mule ESB which could also and it will be a good contender in your list.
Some answers for your questions
1.You can deploy Camel code or the spring integration code into the what so ever container (all in the hands Maven and jar management thing you need to do....)
2.Service mix is a Apache license and is complete open source and if you need some support I suggest you to choose the FUSE ESB which is not part of JBoss family and powered by RedHat
Please follow this link below for more detailed discussion from other stackoverflow.com users
use the below for your analysis
Apache Camel and other ESB products
What is an ESB and what is it good for?
Messaging, Queues and ESB's - I know where I want to be but not how to get there
JMS and ESB - how they are related?

Resources