4种模式:Direct模式(简单的一种)、Topic模式、Fanout模式、Headers模式
第一步:MQConfig
1、书写队列
2、书写交换机
3、将队列和交换机进行绑定
package com.jack.seckill.rabbitmq;
import java.util.HashMap;
import java.util.Map;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Configuration相当于xml配置
*
*/
@Configuration
public class MQconfig {
public static final String QUEUE="queue";
public static final String TOPIC_QUEUE1="topic.queue1";
public static final String TOPIC_QUEUE2="topic.queue2";
public static final String TOPIC_EXCHANGE="topicExchange";
public static final String HEADERS_QUEUE="headers.queue";
public static final String FANOUT_EXCHANGE="fanoutExchange";
public static final String HEADERS_EXCHANGE="headersExchange";
public static final String ROUTING_KEY1="topic.key1";
public static final String ROUTING_KEY2="topic.#";
/**
* Direct模式 交换机Exchange
* @return
*/
@Bean
public Queue queue() {
return new Queue(QUEUE,true);
}
/**
* Topic模式 交换机Exchange
* @return
*/
@Bean
public Queue topicQueue1() {
return new Queue(TOPIC_QUEUE1,true);
}
@Bean
public Queue topicQueue2() {
return new Queue(TOPIC_QUEUE2,true);
}
@Bean
public TopicExchange topicExchange() {
return new TopicExchange(TOPIC_EXCHANGE);
}
@Bean
public Binding topicBinding1() {
return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("topic.key1");
}
@Bean
public Binding topicBinding2() {
return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("topic.#");
}
/**
* fanout模式 交换机Exchange
*/
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange(FANOUT_EXCHANGE);
}
@Bean
public Binding fanoutBinding1() {
return BindingBuilder.bind(topicQueue1()).to(fanoutExchange());
}
@Bean
public Binding fanoutBinding2() {
return BindingBuilder.bind(topicQueue2()).to(fanoutExchange());
}
/**
* Header模式 交换机Exchange
*/
@Bean
public HeadersExchange headersExchange() {
return new HeadersExchange(HEADERS_EXCHANGE);
}
@Bean
public Queue HeadersQueue() {
return new Queue(HEADERS_QUEUE,true);
}
@Bean
public Binding HeadersBinding() {
Map<String,Object> map=new HashMap<String,Object>();
map.put("headers1", "value1");
map.put("headers2", "value2");
return BindingBuilder.bind(HeadersQueue()).to(headersExchange()).whereAll(map).match();
}
}
第二步:MQSender
package com.jack.seckill.rabbitmq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jack.seckill.redis.RedisService;
@Service
public class MQSender {
private static Logger log=LoggerFactory.getLogger(MQSender.class);
@Autowired
AmqpTemplate amqpTemplete;
public void send(Object message) {
String msg=RedisService.beanToString(message);
log.info("send message:"+msg);
amqpTemplete.convertAndSend(MQconfig.QUEUE,msg);
}
public void sendTopic(Object message) {
String msg=RedisService.beanToString(message);
log.info("send topic message:"+msg);
amqpTemplete.convertAndSend(MQconfig.TOPIC_EXCHANGE,"topic.key1",msg+"1");
amqpTemplete.convertAndSend(MQconfig.TOPIC_EXCHANGE,"topic.key2",msg+"2");
}
public void sendFanout(Object message) {
String msg=RedisService.beanToString(message);
log.info("send fanout message:"+msg);
amqpTemplete.convertAndSend(MQconfig.FANOUT_EXCHANGE,"",msg+"1");
}
public void sendHeaders(Object message) {
String msg=RedisService.beanToString(message);
log.info("send headers message:"+msg);
MessageProperties properties=new MessageProperties();
properties.setHeader("headers1", "value1");
properties.setHeader("headers2", "value2");
Message obj=new Message(msg.getBytes(),properties);
amqpTemplete.convertAndSend(MQconfig.HEADERS_EXCHANGE,"",obj);
}
}
第三步:MQReceiver
package com.jack.seckill.rabbitmq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class MQReceiver {
private static Logger log=LoggerFactory.getLogger(MQReceiver.class);
@RabbitListener(queues=MQconfig.QUEUE)
public void receive(String message) {
log.info("receive message:"+message);
}
@RabbitListener(queues=MQconfig.TOPIC_QUEUE1)
public void receiveTopic1(String message) {
log.info("topic queue1 message:"+message);
}
@RabbitListener(queues=MQconfig.TOPIC_QUEUE2)
public void receiveTopic2(String message) {
log.info("topic queue2 message:"+message);
}
@RabbitListener(queues=MQconfig.HEADERS_QUEUE)
public void receiveHeaders(byte[] message) {
log.info("header queue message:"+new String(message));
}
}
第四步:controller
package com.jack.seckill.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jack.seckill.domain.User;
import com.jack.seckill.rabbitmq.MQSender;
import com.jack.seckill.redis.RedisService;
import com.jack.seckill.redis.UserKey;
import com.jack.seckill.result.CodeMsg;
import com.jack.seckill.result.Result;
import com.jack.seckill.service.UserService;
@Controller
@RequestMapping("/demo")
public class SampleController {
@Autowired
MQSender sender;
@RequestMapping("/mq")
@ResponseBody
public Result<String> mq(){
sender.send("nice to meet you");
return Result.success("hello,two");
}
@RequestMapping("/mq/topic")
@ResponseBody
public Result<String> topic(){
sender.sendTopic("nice to meet you");
return Result.success("hello,two");
}
@RequestMapping("/mq/fanout")
@ResponseBody
public Result<String> fanout(){
sender.sendFanout("nice to meet you");
return Result.success("hello,two");
}
@RequestMapping("/mq/headers")
@ResponseBody
public Result<String> headers(){
sender.sendHeaders("nice to meet you");
return Result.success("hello,two");
}
}