代码语言:javascript复制
spring:
rabbitmq:
host: 192.168.3.193
username: guest
password: guest
代码语言:javascript复制package com.shi.amqp;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 自动配置
* 1. RabbitAutoConfiguration 帮我们配置了很多东西
* 2. 有自动连接工厂 ConntectionFactory
* 3. RabbitProperties :封装了 rabbitMq的配置,
* 4. RabbitTemplate: 给rabbitMQ发送和接收消息
* 5. amqpAdmin: rabbitmq系统管理功能组件,
* amqpAdmin:用来创建 queue,exchange,binbing
* 6. @EnableRabbit RabbitListener 监听消息
*/
@EnableRabbit //开启基于注解的rabbitmq模式
@SpringBootApplication
public class AmqpApplication {
public static void main(String[] args) {
SpringApplication.run(AmqpApplication.class, args);
}
}
代码语言:javascript复制package com.shi.amqp;
import com.shi.amqp.model.Book;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AmqpApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void contextLoads() {
}
@Test
public void create(){
amqpAdmin.declareQueue(new Queue("amqp.queue"));//创建队列
System.out.println("队列创建成功");
amqpAdmin.declareExchange(new DirectExchange("ampq.exchage"));//创建直连型交换机
System.out.println("交换机创建成功");
amqpAdmin.declareBinding(new Binding("amqp.queue",
Binding.DestinationType.QUEUE,"ampq.exchage","amqp",null));//绑定信息
System.out.println("队列与交换机的绑定创建成功");
}
/**
* 1 单播 (点对点)
*/
@Test
public void sendMessage(){
//message 需要自己构造一个,定义消息体内容和消息头
//rabbitTemplate.send(exchange,routingKey,message);
//obj 默认当成消息体,只需要传入要发送的对象,自动序列化发送给rabbitMq
//rabbitTemplate.convertAndSend(exchange,routingKey, obj);
/*Map<String,Object> map = new HashMap<>();
map.put("msg","我的测试直连消息队列");
map.put("date",new Date());
rabbitTemplate.convertAndSend("amq.direct","direct",map);
*/
Book book = new Book();
book.setName("三国演义3333");
book.setAuthor("罗贯中3333");
rabbitTemplate.convertAndSend("amq.direct","direct",book);
}
/**
* 接收消息队列中的数据
*/
@Test
public void receiverMsg(){
Object obj = rabbitTemplate.receiveAndConvert("TESTQUEUE");
System.out.println(obj.getClass());
System.out.println(obj);
}
}
代码语言:javascript复制package com.shi.amqp.service;
import com.shi.amqp.model.Book;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class BooService {
/**
* 用来监听队列,队列中有消息过来就调用该方法
* @param book
*/
@RabbitListener(queues = "TESTQUEUE")
public void recive1(Book book){
System.out.println(book.getAuthor());
System.out.println(book.getName());
}
}
代码语言:javascript复制package com.shi.amqp.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 MyAMQPConfig {
/**
* 把消息对队列中的消息转换成json格式
* @return
*/
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}