add spring-rabbitmq example

Signed-off-by: Mohammad Sadegh Sheikh Zahedi <sheikhoo.iran@gmail.com>
This commit is contained in:
Mohammad Sadegh Sheikh Zahedi
2023-01-02 14:47:16 +03:30
parent 3746d65ad1
commit b670c71c4a
12 changed files with 391 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
package com.company.project;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,33 @@
package com.company.project.config;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.company.project.model.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class QueueConsumer {
private final RabbitTemplate rabbitTemplate;
@Value("${queue.name}")
private String queueName;
@Autowired
public QueueConsumer(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
private String receiveMessage() {
String message = (String) rabbitTemplate.receiveAndConvert(queueName);
return message;
}
public Message processMessage() throws JsonProcessingException {
String message = receiveMessage();
return new ObjectMapper().readValue(message, Message.class);
}
}

View File

@@ -0,0 +1,29 @@
package com.company.project.config;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.company.project.model.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class QueueProducer {
@Value("${fanout.exchange}")
private String fanoutExchange;
private final RabbitTemplate rabbitTemplate;
@Autowired
public QueueProducer(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
public void produce(Message message) throws JsonProcessingException {
rabbitTemplate.setExchange(fanoutExchange);
rabbitTemplate.convertAndSend(new ObjectMapper().writeValueAsString(message));
}
}

View File

@@ -0,0 +1,33 @@
package com.company.project.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfiguration {
@Value("${fanout.exchange}")
private String fanoutExchange;
@Value("${queue.name}")
private String queueName;
@Bean
Queue queue() {
return new Queue(queueName, true);
}
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange(fanoutExchange);
}
@Bean
Binding binding(Queue queue, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(queue).to(fanoutExchange);
}
}

View File

@@ -0,0 +1,36 @@
package com.company.project.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.company.project.config.QueueConsumer;
import com.company.project.config.QueueProducer;
import com.company.project.model.Message;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/")
public class MessageController {
private final QueueProducer queueProducer;
private final QueueConsumer queueConsumer;
public MessageController(QueueProducer queueProducer, QueueConsumer queueConsumer) {
this.queueProducer = queueProducer;
this.queueConsumer = queueConsumer;
}
@GetMapping("getMessage")
public ResponseEntity<?> getMessage() throws JsonProcessingException {
Message message = queueConsumer.processMessage();
return new ResponseEntity<Message>(message, HttpStatus.OK);
}
@PostMapping("sendMessage")
public ResponseEntity<?> sendMessage(@RequestBody Message message) throws JsonProcessingException {
queueProducer.produce(message);
return new ResponseEntity<Message>(HttpStatus.CREATED);
}
}

View File

@@ -0,0 +1,32 @@
package com.company.project.model;
public class Message {
private String title;
private String text;
private String sender;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
}

View File

@@ -0,0 +1,9 @@
server.port= 8080
spring.main.banner-mode= off
spring.rabbitmq.host= ${RABBITMQ_HOST:localhost}
spring.rabbitmq.port= 5672
spring.rabbitmq.username= guest
spring.rabbitmq.password= guest
queue.name= ${RABBITMQ_QUEUE_NAME:springboot-queue}
fanout.exchange= ${RABBITMQ_FANOUT_EXCHANGE:springboot-exchange}