赞
踩
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-amqp</artifactId>
- </dependency>
在application.properties
或application.yml
中配置RabbitMQ服务器的连接参数
- package com.jyx.consumer;
-
- import org.springframework.amqp.core.*;
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.stereotype.Component;
-
-
- @Configuration
-
- public class DirectConsumer {
- // 声明队列
- @Bean
- public Queue queue(){
- return QueueBuilder.durable("Direct_01").build();
- }
- // 声明交换机
- @Bean
- public DirectExchange exchange(){
- return ExchangeBuilder.directExchange("Exchange_01").build();
- }
- // 绑定交换机和队列
- @Bean
- public Binding binding(Queue queue,DirectExchange exchange){
- return BindingBuilder.bind(queue).to(exchange).with("RK01");
- }
- // 声明消费者
- @RabbitListener(queues = "Direct_01")
- public void receive(String msg){
- System.out.println("Direct_01消费者收到消息:"+msg);
- }
-
- }
- package com.jyx.producer;
-
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- @Service
- public class DirectProducer {
- @Autowired
- private RabbitTemplate rabbitTemplate;
- // 生产者 发送消息
- public void send(Object message) {
- rabbitTemplate.convertAndSend("Exchange_01", "RK01", message);
- System.out.println("发送消息:" + message);
- }
- }
- package com.jyx;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class App {
- public static void main(String[] args) {
- SpringApplication.run(App.class, args);
- }
- }
- package com.jyx;
-
- import com.jyx.model.OrderKO;
- import com.jyx.producer.DirectProducer;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
-
- import java.io.IOException;
-
- @SpringBootTest
- class AppTest {
- @Autowired
- private DirectProducer directProducer;
- @Test
- void contextLoads() throws IOException {
- for (int i = 0; i < 10; i++) {
- directProducer.send("hello world");
- }
- System.in.read();
- }
-
-
- }
- package com.jyx.model;
-
- import lombok.AllArgsConstructor;
- import lombok.Builder;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- import java.io.Serializable;
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- @Builder
- public class OrderKO implements Serializable {
- private int id;
- private String name;
- }
把String换成对象
- // 声明消费者
- @RabbitListener(queues = "Direct_01")
- public void receive(OrderKO msg){
- System.out.println("Direct_01消费者收到消息:"+msg);
- }
- @Test
- void contextLoads2() throws IOException {
- for (int i = 0; i < 10; i++) {
- directProducer.send(OrderKO.builder().id(i).name("张"+i).build());
- }
- System.in.read();
- }
- package com.jyx.config;
-
- 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 RabbitConfig {
- @Bean
- public MessageConverter messageConverter() {
- return new Jackson2JsonMessageConverter();
- }
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。