Install RabbitMQ
brew install rabbitmq
PATH=$PATH:/usr/local/sbin
Start the server: rabbitmq-server
Go to home page of rabbit mq
http://localhost:15672/
Provide default userid & password:
guest/guest
You will be able to login to the Rabbit MQ console to see the traffic and other details.
Sending a message from the Spring boot application.
Include the dependency in the Gradle / maven.
compile('org.springframework.boot:spring-boot-starter-amqp')
ProducerCode:
/**
* Created by deiveehannallazhagappan on 3/15/17.
*/
@Service
public class ProducerService {
private RabbitTemplate rabbitTemplate;
public ProducerService(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
public void sendMessage(String messageToBeSent) {
rabbitTemplate.convertAndSend("deiveeQueue", messageToBeSent);
}
@Bean
public Queue queue() {
return new Queue("deiveeQueue", false);
}
}
* Created by deiveehannallazhagappan on 3/15/17.
*/
@Service
public class ProducerService {
private RabbitTemplate rabbitTemplate;
public ProducerService(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
public void sendMessage(String messageToBeSent) {
rabbitTemplate.convertAndSend("deiveeQueue", messageToBeSent);
}
@Bean
public Queue queue() {
return new Queue("deiveeQueue", false);
}
}
Create a controller which will trigger sending message to the queue.
@RestController
public class ProducerController {
@Autowired
private ProducerService producerService;
@RequestMapping("/queue/sendMessage/{messageToBeSent}")
public String sendMessageToQueue(@PathVariable("messageToBeSent") String messageToBeSent) {
String timestamp = new SimpleDateFormat("HH:mm:ss").format(new Date());
producerService.sendMessage(timestamp + ": " + messageToBeSent);
return "Sent message - " + messageToBeSent + " to the queue";
}
}
public class ProducerController {
@Autowired
private ProducerService producerService;
@RequestMapping("/queue/sendMessage/{messageToBeSent}")
public String sendMessageToQueue(@PathVariable("messageToBeSent") String messageToBeSent) {
String timestamp = new SimpleDateFormat("HH:mm:ss").format(new Date());
producerService.sendMessage(timestamp + ": " + messageToBeSent);
return "Sent message - " + messageToBeSent + " to the queue";
}
}
ConsumerCode:
@Service
public class ConsumerService {
private RabbitTemplate rabbitTemplate;
public ConsumerService(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
@RabbitListener(queues = "deiveeQueue")
public void processMessage(String message) {
System.out.println(message);
}
@Bean
public Queue queue() {
return new Queue("deiveeQueue", false);
}
}
public class ConsumerService {
private RabbitTemplate rabbitTemplate;
public ConsumerService(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
@RabbitListener(queues = "deiveeQueue")
public void processMessage(String message) {
System.out.println(message);
}
@Bean
public Queue queue() {
return new Queue("deiveeQueue", false);
}
}
Send the message by hitting the url
View the message in the console:
More:
Changing the default userid and password:
Where do we configure in the application that messages from producer should be sent to particular message queue. Did you already configure deiveeQueue in Message broker.
ReplyDeleteWe dont have to do on the management console, it is sufficient to define in the code itself Refer @Queue definition for both producer and consumer.
ReplyDelete