Communication between two java classes using Spring - spring

I have two java classes that implements Runnable within the same application, let's say A and B classes.
These two classes share a BlockingQueue to pass some data. Class A sends data to class B through BlockingQueue. Then class B takes the data and process it. What I am trying to do is that when some conditions happen in class B as a result of process this data, it sends a messages to class A, and class A prints these messages.
So I was having a look at Spring messaging using Apache ActiveMQ, but it seems a broker must be started in order communication works using "activemq start" command. Is there any possibility to do this without having this broker started?
What is the best option to do this? I would like to do this using Spring framework so can be done using some kind of approach in Spring? Or the only possiblity using spring is through apache activemq?

Take a looks at Spring Integration; components communicate using Messages.
You can use an entirely in-memory configuration or provide persistence via some broker such as ActiveMQ.
The components themselves are not concerned with that; they simply send/receive messages.

Create the Queue in spring and inject it into both Runnables

Related

difference between usage of #RabbitListener() and usage of DirectMessageListenerContainer in consuming from a rabbitMQ in springboot

I am trying to configure concurrent consumers in spring to consume messages from RabbitMQ, in order to achieve that i have configured consumers in two ways
1.annotated a method with #RabbitListener(queues = "name of queue")
2.implementing "MessageListener" interface and overriding onMessage(Message message)
In my case both the ways worked fine, but i am unable to figure out what is the advantage/disadvantage of using #RabbitListener() for starting a consumer over the other way.
Also adding to that i have configured "DirectMessageListenerContainer" in my configuration and mapped it to "MessageListener" implementation to achieve concurrent consumers, my question here is can we do the same mapping for consumer implemented through #RabbitListener() and if so how. I couldnt find any source on how a consumer started with a #RabbitListener() annotated method can be configured with a "DirectMessageListenerContainer"
Any Help is appreciated.
#RabbitListener is simply a higher-level abstraction. It uses the listener container underneath.
When using spring boot, use the ...listener.type application property to specify which type of container you want.
The default is simple.

Creating a library to have rabbit spring beans created

I am creating a message bus jar which connects to different MQ broker like Rabbit MQ. The jar will be used by many applications to send and receive message from Rabbit.
While sending message is working as expected from my application, not clear about how to design the listener. Obviously the #RabbitListener is in the jar but I want the message should reach the class in applications.
Any suggestions appreciated.
Is there any better way to create a kind of bus library for rabbit spring and use in applications.
Sounds like you need to make yourself familiar with https://spring.io/projects/spring-integration. And also take a look, please, into this project which already implements something similar: https://spring.io/projects/spring-cloud-bus

Using rabbitmq to send String/Custom Object from one spring boot application to another

My requirement is to for starters send a string from one spring-boot application to another using AMQP.
I am new to it and I have gone through this spring-boot guide, so i know the basic fundamentals of Queue, Exchange, Binding, Container and listener.
So, above guide shows the steps when amqp is received in same application.
I am a little confused on where to start if I want to achieve above type of communication between 2 different spring-boot applications.
What are the properties needed for that, etc.
Let me know if any details required.
Just divide the application into two:
One without Receiver and ...
Another without Sender
Make sure your application and configuration etc stays the same. With Spring boot's built-in RabbitMQ, you will be able to run it alright.
Next step is to call sender as and when needed from your business logic.

Using AggregateApplicationBuilder with a local binder

I'm trying to aggregate different Sink and Source spring boot applications using the AggregateApplicationBuilder as described here: http://docs.spring.io/spring-cloud-stream/docs/current-SNAPSHOT/reference/htmlsingle/#_aggregation
Since I expect in process communication, I don't want to setup kafka or rabbitmq binder. How to configure a local one? I found that a spring-cloud-stream-binder-local exists but it's in M2 since a long time and is not embedded with a release train.
How I can use the AggregateApplicationBuilder with no external system dependency?
Thanks
With AggregateApplicationBuilder you don't have to configure the binder for the in-process communication of the directly bound channels within the aggregated application. The binder is required only if you need the aggregate application itself consumes messages from broker or produces messages to broker. If the aggregated application itself is self-contained, then there is no need for the binder at all.

Spring JavaMailSender: Making it asynchronous and persistent

Is there an easy/lightweight way to add persistence to Spring's JavaMailSender and have it operate asynchronously? Does Spring provide any "built-in" support for this? I'm currently looking at queues with JMS, but they seem like overkill for the task at hand (looking at ActiveMQ and RabbitMQ). Is there a lightweight JMS option?
Your approach with jms is fine. Unfortunately persistence and asynchronous processing is not such a simple task and you will have to code a bit.
However have a look at Spring integration, it provides built-in support for JMS inbounds and e-mail outbounds - all you have to do is connect the pieces via XML DSL.
If you want to make any method in Spring asynchronous, all you need to do is configure task namespace in the xml config via <task:annotation-driven/>. Then, you just annotate the method with #Async and it will run in its own thread. Note that an async call will run in its own transaction, as Spring grabs a new thread from its internal pool to service the call. If you do this, then you don't need JMS for aynchronous processing.

Resources