I am working on a MicroBlog spring mvc hibernate application. I need to implement a publish subscribe functionality like twitter.
I am using RabbitMQ for messaging with Spring AMQP abstraction.
Everywhere I see on web the pubsub examples are given involving
Spring Integration
Spring AMQP & RabbitMQ
I researched a little more on Spring-Integration & found that a publish subscribe can be implemented with it even without using RabbitMQ.
Now my question is
Why do I need to use Spring Integration with [Spring AMQP & RabbitMQ] to implement a pubsub functionality. Why can't I just use Spring AMQP with Rabbit to do that?
Does Spring integration provide any additional features?
My Spring AMQP & RabbitMQ configuration
<rabbit:connection-factory id="connectionFactory" virtual-host="/" host="localhost"
username="guest" password="guest"/>
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="UserPostpublishQueue" />
<fanout-exchange name="broadcastUserPosts" xmlns="http://www.springframework.org/schema/rabbit">
<bindings>
<binding queue="UserPostpublishQueue"/>
</bindings>
</fanout-exchange>
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="broadcastUserPosts"
queue="UserPostpublishQueue"/>
</beans>
Test code in my controller
@Autowire
private AmqpTemplate amqpTemplate;
try{
amqpTemplate.convertAndSend(post);
Post receivedPost = (Post)amqpTemplate.receiveAndConvert();
System.out.println("received Post "+receivedPost);
}catch(AmqpException e){
//deal with exception
}
The Spring AMQP project applies core Spring concepts to the development of AMQP-based messaging solutions. It provides a "template" as a high-level abstraction for sending and receiving messages. It also provides support for Message-driven POJOs with a "listener container".
Run the Application There is a Runner bean, which is then automatically run. It retrieves the RabbitTemplate from the application context and sends a Hello from RabbitMQ! message on the spring-boot queue. Finally, it closes the Spring application context, and the application ends.
Spring Integration provides JDBC channel adapters that connect a channel to a database. In the case of the inbound adapter, a database is the source on which an SQL query can be run and the complete result set is available as a message with a Java List payload.
RabbitTemplate() Convenient constructor for use with setter injection. RabbitTemplate(ConnectionFactory connectionFactory) Create a rabbit template with default strategies and settings.
Spring Integration implements the patterns from http://www.enterpriseintegrationpatterns.com/books1.html while using AMQP/RabbitMQ as one of its many transports.
I understand that spring-amqp
is more the AMQP client functionality. If you don't want to use spring. Then we have a plain java client: https://www.rabbitmq.com/java-client.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With