Annotation configuration for kafka High level consumer - spring-integration

I wonder if there's a way to configure the kafka-integration Inbound Channel Adapter as explained in https://github.com/spring-projects/spring-integration-kafka using Java annotations instead of xml. I don't quite get the clue on it. I was able to configure the Message Driven Channel Adapter but now I need one that doesn't re-reads consumed messages in case of re-starting the server

See the main Spring Integration Documentation for configuring with annotations; also see the Java DSL.
We are adding more and more java configuration examples; see here for example.
The kafka high-level message source would be configured as an #InboundChannelAdapter #Bean.

Related

Spring Integration and the Message Bridge Pattern

Learning Spring Integration. I am trying to understand the IntegrationFlow DSL and the use of its to(IntegrationFlow) method.
It seems that this allows us to daisy-chain the end of Flow 1 with the beginning of Flow 2.
Is this the DSL's implementation of the Message Bridge pattern, where channels are connected to each other? If not, how is this different than a Message Bridge? Is it similar to the direct: and seda: endpoints in Apache Camel parlance, that is, a way of snapping routes together?
Yes, we can treat it that way, but technically it is just composition of more high-level messaging abstractions together. There is no EIP Message Bridge pattern implementation as a single top-level component.
Let's see it objective:
How can multiple messaging systems be connected so that messages available on one are also available on the others?
Use a Messaging Bridge, a connection between messaging systems, to replicate messages between systems.
So, let's say we need to transfer data from Apache Kafka topic into some IBM MQ queue. For Kafka we use an KafkaMessageDrivenChannelAdapter and for IBM MQ - JmsSendingMessageHandler. We connect them via DirectChannel the rest is done with internal (de)serializers to remap Kafka records into JMS messages. Does this approach implement the mentioned pattern? I think yes. And with different channel adapters we can implement many use-cases transferring data from one source to another.
And that Message Bridge confirms our assumption:
The Messaging Bridge is a set of Channel Adapters.
Now about to(IntegrationFlow) operator. This is just a convenient API to decompose your configuration between different logical, reusable pieces. At runtime we don't have any IntegrationFlows interacting: only endpoints are exchanging messages via channels between them.
Yes, you can treat Camel's direct: and seda: as a MessageChannel abstraction in terms of Spring Integration. Yes, we can say that this separation via channel is a bridge we have talked before. But in terms of Spring Integration sometimes there is no reason to separate the logic and people just do this:
IntegrationFlow.from(Kafka.messageDrivenChannelAdapter())
.handle(Jms.outboundAdapter())
.get();
Is this a bridge we saw before? I guess yes. Even if we don't have an explicit channel definition it is still there for us auto-created by the framework.

Implement Spring Batch tasklet with 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.

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: inbound and outbound adapters to MQTT

I'm trying to build MQTT support into my Spring application. For this I'm looking at the references provided by Spring, which can be found here here.
They provide sample code for inbound and outbound channel adapters, but this concept of adapters is unclear to me. I couldn't find a lot of information about it on the web.
What exactly are these adapters and why are they needed?
Where do I need to define these adapters and how do I access them?
I'm using eclipse as my IDE and maven as my buildtool.
The adapters are a concept from Spring Integration which is yet another Spring project that provides an implementation of the Enterprise Integration Patterns and provides an abstraction layer that standardizes the way you integrate with external system, whether they are based on JMS, FTP or as in your case MQTT.
As with so many other Spring projects, the driver behind it is a loose coupling and standardization that will enable you to swap your channel provider (e.g. if its a JMS provider swap RabbitMQ with ActiveMQ or vice-versa) with a minimal impact to the rest of you code.
Its comparable to Apache Camel, in case your more familiar with it, and this comparison is quite an interesting read

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.

Resources