rabbitMQ系列5:springboot整合rabbitMQ+消息轉換


一、簡介:

有時候生產者需要發送給消息隊列的是java對象,這時我們需要將java對象轉換為JSON字符串,然后再消息隊列再將JSON字符串發送給消費者,消費者通過解析JSON字符串並轉換為原來的java對象。

 

二、案例:

1、引入依賴

<!-- rabbitmq依賴 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

<!--fastJson-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.49</version>
</dependency>

<!--lombok-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

 

2、編寫RabbitMQ配置屬性:

# rabbitMQ配置
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
spring.rabbitmq.listener.simple.concurrency= 10
spring.rabbitmq.listener.simple.max-concurrency= 10
spring.rabbitmq.listener.simple.prefetch= 1
spring.rabbitmq.listener.simple.auto-startup=true
spring.rabbitmq.listener.simple.default-requeue-rejected= true
spring.rabbitmq.template.retry.enabled=true 
spring.rabbitmq.template.retry.max-attempts=3
spring.rabbitmq.template.retry.multiplier=1.0

 

3、編寫RabbitMQ配置類:

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQConfig {
    // 隊列名稱
    public static final String QUEUE = "queue";

    /*
    在springboot啟動的時候創建一個隊列
     */
    @Bean
    public Queue queue() {
        return new Queue(QUEUE, true);
    }

    /*
    消息轉換器:將消息轉換為JSON格式
     */
    @Bean
    public MessageConverter getMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}

 

4、編寫消息類:該消息類是生產者發送給消息隊列的參數

@Data
public class CustomMessage {
    private int id;
    private String name;
}

 

5、編寫控制層:

@Autowrite
public MQSender mQSender;

@RequestMapping("/test")
public String test(){
    CustomMessage customMessage = new CustomMessage();
    customMessage.setId(1);
    customMessage.setName("miss");
    mQSender.send(customMessage);
    return "ok";
}

 

6、編寫生產者:生產者將java對象轉換為json字符串

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MQSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(CustomMessage customMessage){
        String json = JSON.toJSONString(customMessage);
        log.info("send:"+json);
        rabbitTemplate.convertAndSend(MQConfig.QUEUE,json);
    }
}

 

7、編寫消費者:消費者監聽“QUEUE”隊列是否有新數據,如果有則獲取,並將json字符串轉換為java對象

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MQReceiver {

    @RabbitListener(queues=MQConfig.QUEUE)  //監聽
    public void  receive(String json){
        CustomMessage customMessage = JSON.toJavaObject(JSON.parseObject(json),                                             CustomMessage.class);
        system.out.println(customMessage);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM