RabbitMQ Client implementation in springboot
Posted By : Kundan Ray Akela | 09-Jan-2017
In this blog I am going to share the information about how to configure and implement RabbitMQ in springboot application. So first of all some basic information about what RabbitMQ does.
RabbitMQ is basically use to implement queueing based messages between applications.It is opensource,robust,easy to use and runs on all major operating systems.It implements AMQP.
Let's start the configuration.First of all define the dependency in pom.xml.
org.springframework.boot spring-boot-starter-amqp
Next step should be to define properties of RabbitMQ server to connect it in application.properties file.
#Rabbitmq configuration spring.rabbitmq.host=hostUrl spring.rabbitmq.port = hostPort spring.rabbitmq.username =username spring.rabbitmq.password =password spring.rabbitmq.virtual-host=virtualHost
Next step will be to configure RabbitMQ client.
public class RobbitMqConfig {
public final static String publishQName ="publishQueue";
public final static String consumeQName ="consumeQueue";
@Bean
public Jackson2JsonMessageConverter jsonMessageConverter(){
Jackson2JsonMessageConverter con= new Jackson2JsonMessageConverter();
return con;
}
@Bean
RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
RabbitTemplate templete=new RabbitTemplate();
templete.setExchange("exchange");//Set exchange
templete.setQueue(publishQName);//Declare queue
templete.setMessageConverter(jsonMessageConverter());
templete.setConnectionFactory(connectionFactory);
return templete;
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(consumeQName);
container.setMessageConverter(jsonMessageConverter());
container.setMessageListener(consumer());
return container;
}
@Bean MessageListener consumer(){
return new Consumer();
}
}
All are set now and we can send and recieve messages.
Publish message:
public class RabbitmqTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void test() throws Exception {
rabbitTemplate.convertAndSend(RobbitMqConfig.publishQName, "RabbitMQ Spring JSON Example");
System.out.println("Is listener returned ::: "+rabbitTemplate.isReturnListener());
}
}
Listening messages:
@Service
public class Consumer implements MessageListener {
private final Logger log = LoggerFactory.getLogger(Consumer.class);
@Override
public void onMessage(Message message) {
String parseMessage=new String(message.getBody());
log.info("consumer message {}",parseMessage);
}
}
You may also want to check blog for implementing RabbitMQ in Java from http://www.oodlestechnologies.com/blogs/Implement-RabbitMQ-for-JMS.
Hope it will help:).Looking forward for any query and comments. Will share the information about how to connect to SSL enabled RabbitMQ server in next blog.
Thanks
Kundan Ray
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Kundan Ray Akela
Kundan holds years of industry experience as a Fullstack Developer in various technologies and is focused in defining the architecture of the system to ensure reliability and resilience. He possess good knowledge & understanding of latest technologies and hands-on experience in Core Java, Spring-Boot, hibernate, React, Angular , Apache Kafka messaging queue , AI Development like Computer Vision/Generative AI/Prediction System, Internet of Things based technologies and relational database like MySql, PostgreSQL etc. He is proficient in API Implementations, Webservices, Development Testings and deployments, code enhancements and have been contributing to company values through his deliverable in various client projects namely VirginMedia, Konfer, TIHM, Herdsy, HP1T and many more. He has a creative mind and has good analytical skills and likes reading and exploring new technologies.