Implement Spring Batch tasklet with Spring Integration? - spring-integration

I manage a now fairly large Spring Integration application. Recently, we wanted to add a batch job using Spring Batch (rationale: administration, monitoring, scheduling, ability to restart in case of failure anywhere along the way).
Since we have developed a lot of connectors to the company services, we planned to reuse them in the batch, so I have been looking closely at how Spring Batch and Spring Integration work together (among other things: Spring Batch Integration). What we had in mind was to somehow implement Spring Batch tasklets using (already available) Spring Integration components.
I may have missed something fundamental at the Spring core level but I wasn't able to figure out a simple way to "invoke" a Spring Integration endpoint from a Spring Batch tasklet (if we forget complicated plumbing like RMI calls, "remote chunking", "remote partitioning"...)
Did I miss something ?

This is a very common use case (invoking an integration flow from a batch job/step).
Simply wire an integration gateway into the tasklet.
In recent applications, it's quite common to use the MessagingGateway annotation or the DSL. But, if you're more familiar with XML, then a <gateway/> will work well too.

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.

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 Integration is an alternate to JMS Queues?

Spring integration is used to make the communication between two systems easier.
So does that mean the if two systems are talking using JMS queus, then we can remove queues and integrate two systems using Spring Integration Framework?
Looks like you should study more on the matter. I mean EIP-book, Spring Integration in Action or, at least the Reference Manual from Spring IO site.
The main Spring Integration goal is integration and JMS is only one way to do that.
If your two system can get deal with JMS, there is no stops to integrate them using Spring Integration: just provide JMS adapters for boht of them.
As per my understanding,
Spring Integration solution a framework level solutions to Design Patterns listed in http://eaipatterns.com/
I might be an alternate to ESB which is like one big tunnel & everything passes through that.
Spring Integration provides end to end message between different de-coupled, disparate connecting points.
On the other hand JMS is an API in the Java EE spec which can be used in conjunction with Spring-Integration.
You might as well want to read about AMQP which is a messaging protocol.

Streaming Transformation in Spring Integration

We have a Spring Integration application which polls from a database, marshalls into XML, and then sends that XML as a message to a downstream system. The application works, but the issue we face is that some of the result sets can be massive. In such cases, it appears Spring Integration cannot handle the transformation because the result set is too big to handle in memory. I do not see a Stax marshaller in Spring Integration, as there is say in Spring Batch. That actually makes sense, because messaging usually means working with many small messages and not large files.
One option we have is to develop a Spring Batch application instead.
Is there a design we could adopt for Spring Integration to handle this? Does Spring Integration have any notion of streaming? For instance, would it be possible to read the result set in chunks, transform each piece, and each piece as a separate message which is part of a set? Or is Spring Batch just a better fit?
Thanks very much
You can set max-rows-per-poll to limit the size of each result set.
If that's not practical then you can use a combination of Spring Batch and Spring Integration - have the batch ItemWriter send the chunks into a Spring Integration flow via a <gateway/>.

Use Spring Integration or not? on a pure Java distributed System that is connected with JMS (ActiveMq)

we are building a distributed Java system (should be scalable ;-) ) that is connected only with JMS (ActiveMQ). I studied Spring Integration and I am not sure what the advantage would be if we use it. I think we are better off with using the JMS-Templeate from the Spring Core Project as we send only messages from a JavaService to another JavaService and so on.
Use Spring Integration (or any other framework) if you think the extra abstraction that it buys you is worth the cost. It should give you a more solid foundation on which to build your application. Software written by Spring is better than anything you or I would write from scratch.
All frameworks have a cost. There are additional dependencies. Sometimes greater abstraction can obscure too much.
You should prototype with and without Spring Integration to see if it's worth the cost.
If you're not a Spring user already, I'd recommend that you learn Spring before jumping into a big enterprise project.
SpringIntegration will give your Enterprise Integration Pattens ready to use.
Are you going to need splitters, routers, filters, gateways, aggregators, transformers, etc?
If the answer is no, go for plain Java+ActiveMQ.
If you need a really powerful system integration tier, then you should use Spring Integration - it's an additional level of abstraction which may help when your system will grow. With SI it's a matter of seconds to add new integration processing rule between two systems.
From the other hand, I've worked on some SpringIntegration+ActiveMQ project, and it was almost impossible to configure this broker to work with SI in the reliable way. So if you decide to use SI I'd recommend HornetQ as a JMS broker - this one works fine.

Resources