这是springboot封装的rabbitmq依赖,非rabbitmq原装
org.springframework.boot
spring-boot-starter-amqp
spring:
rabbitmq:
host: 192.168.64.140
username: admin
password: admin
package cn.tedu.m1;
import org.springframework.amqp.core.Queue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Main {
@Autowired
private Producer p;
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@Bean
public Queue task_queue() {
/*
* 可用以下形式:
* new Queue("helloworld") - 持久,非排他,非自动删除
* new Queue("helloworld",false,false,false,null)
*/
return new Queue("helloworld",false);
}
//spring扫描创建完所有的实例,完成所有的依赖注入后,自动执行@PostConsutruct注解的方法
@PostConstruct
public void test(){
p.send();
}
}
@Component
public class SimpleSender {
@Autowired
AmqpTemplate t;
public void send() {
// 这里向 helloworld 队列发送消息
t.convertAndSend("helloworld", "Hello world!! "+System.currentTimeMillis());
System.out.println("消息已发送");
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "helloworld")
public class SimpleReceiver {
@RabbitHandler
public void receive(String msg) {
System.out.println("收到: "+msg);
}
}
@Component
public class SimpleReceiver {
@RabbitListener(queues = "helloworld")
public void receive(String msg) {
System.out.println("收到: "+msg);
}
}
@RabbitListener(queuesToDeclare = @Queue(name = "helloworld",durable = "false"))
package cn.tedu.rabbitmqspringboot.m2;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import javax.annotation.PostConstruct;
@SpringBootApplication
public class Main {
@Autowired
private Producer p;
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@Bean
public Queue task_queue() {
//默认是持久队列,true,false,false
return new Queue("task_queue", true);
}
//spring扫描创建完所有的实例,完成所有的依赖注入后,自动执行@PostConsutruct注解的方法
@PostConstruct
public void test() {
/*new Thread(new Runnable() {
@Override
public void run() {
p.send();
}
}).start();*/
//lamda表达式 == Runnable匿名内部类
new Thread(() -> p.send()).start();
}
}
package cn.tedu.rabbitmqspringboot.m2;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Scanner;
@Component
public class Producer {
@Autowired
AmqpTemplate t;
public void send() {
while (true) {
System.out.println("输入消息: ");
String s = new Scanner(System.in).nextLine();
t.convertAndSend("task_queue", s);
}
}
}
//如果需要设置消息为非持久化,可以取得消息的属性对象,修改它的deliveryMode属性
t.convertAndSend("task_queue", (Object) s, new MessagePostProcessor() {
@Override
public Message postProcessMessage(Message message) throws AmqpException {
MessageProperties props = message.getMessageProperties();
props.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
return message;
}
});
package cn.tedu.rabbitmqspringboot.m2;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer {
//每个@RabbitListener 都会注册成为一个消费者
@RabbitListener(queues = "task_queue")
public void receive1(String msg) {
System.out.println("消费者1收到: " + msg);
}
@RabbitListener(queues = "task_queue")
public void receive2(String msg) {
System.out.println("消费者2收到: " + msg);
}
}
在 spring boot 中提供了三种确认模式:
默认的 AUTO 模式中, 处理消息的方法抛出异常, 则表示消息没有被正确处理, 该消息会被重新发送.
spring:
rabbitmq:
listener:
simple:
# acknowledgeMode: NONE # rabbitmq的自动确认
acknowledgeMode: AUTO # rabbitmq的手动确认, springboot会自动发送确认回执 (默认)
# acknowledgeMode: MANUAL # rabbitmq的手动确认, springboot不发送回执, 必须自己编码发送回执
如果设置为 MANUAL 模式,必须手动执行确认操作
@RabbitListener(queues="task_queue")
public void receive1(String s, Channel c, @Header(name=AmqpHeaders.DELIVERY_TAG) long tag) throws Exception {
System.out.println("receiver1 - 收到: "+s);
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '.') {
Thread.sleep(1000);
}
}
// 手动发送确认回执
c.basicAck(tag, false);
}
工作模式中, 为了合理地分发数据, 需要将 qos 设置成 1, 每次只接收一条消息, 处理完成后才接收下一条消息.
配置application.yml
spring:
rabbitmq:
host: 192.168.64.140
username: admin
password: admin
listener:
simple:
prefetch: 1 #Qos=1,默认250
package cn.tedu.rabbitmqspringboot.m3;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import javax.annotation.PostConstruct;
@SpringBootApplication
public class Main {
@Autowired
private Producer p;
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@Bean
public FanoutExchange logsExchange(){
return new FanoutExchange("logs", false, false);
}
//spring扫描创建完所有的实例,完成所有的依赖注入后,自动执行@PostConsutruct注解的方法
@PostConstruct
public void test() {
/*new Thread(new Runnable() {
@Override
public void run() {
p.send();
}
}).start();*/
//lamda表达式 == Runnable匿名内部类
new Thread(() -> p.send()).start();
}
}
package cn.tedu.rabbitmqspringboot.m3;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Scanner;
@Component
public class Producer {
@Autowired
AmqpTemplate t;
public void send() {
while (true) {
System.out.println("输入消息: ");
String s = new Scanner(System.in).nextLine();
t.convertAndSend("logs", "", s);
}
}
}
package cn.tedu.rabbitmqspringboot.m3;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import javax.xml.ws.BindingType;
@Component
public class Consumer {
//每个@RabbitListener 都会注册成为一个消费者
@RabbitListener(bindings = @QueueBinding(
//不配置就是随机队列,非持久,独占,自动删除
value = @Queue(),
//交换机,declare=false -- 不重复定义交换机
exchange = @Exchange(name = "logs", declare = "false")
))
public void receive1(String msg) {
System.out.println("消费者1收到: " + msg);
}
@RabbitListener(bindings = @QueueBinding(
//不配置就是随机队列,非持久,独占,自动删除
value = @Queue(),
//交换机,declare=false -- 不重复定义交换机
exchange = @Exchange(name = "logs", declare = "false")
))
public void receive2(String msg) {
System.out.println("消费者2收到: " + msg);
}
}
package cn.tedu.rabbitmqspringboot.m4;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import javax.annotation.PostConstruct;
@SpringBootApplication
public class Main {
@Autowired
private Producer p;
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
@Bean
public DirectExchange logsExchange(){
return new DirectExchange("direct_logs", false, false);
}
//spring扫描创建完所有的实例,完成所有的依赖注入后,自动执行@PostConsutruct注解的方法
@PostConstruct
public void test() {
/*new Thread(new Runnable() {
@Override
public void run() {
p.send();
}
}).start();*/
//lamda表达式 == Runnable匿名内部类
new Thread(() -> p.send()).start();
}
}
package cn.tedu.rabbitmqspringboot.m4;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Scanner;
@Component
public class Producer {
@Autowired
AmqpTemplate t;
public void send() {
while (true) {
System.out.println("输入消息: ");
String s = new Scanner(System.in).nextLine();
System.out.println("输入路由键: ");
String key = new Scanner(System.in).nextLine();
//默认发送持久消息
t.convertAndSend("direct_logs", key, s);
}
}
}
package cn.tedu.rabbitmqspringboot.m4;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer {
//每个@RabbitListener 都会注册成为一个消费者
@RabbitListener(bindings = @QueueBinding(
//不配置就是随机队列,非持久,独占,自动删除
value = @Queue(),
//交换机,declare=false -- 不重复定义交换机
exchange = @Exchange(name = "direct_logs", declare = "false"),
key = {"error"}
))
public void receive1(String msg) {
System.out.println("消费者1收到: " + msg);
}
@RabbitListener(bindings = @QueueBinding(
//不配置就是随机队列,非持久,独占,自动删除
value = @Queue(),
//交换机,declare=false -- 不重复定义交换机
exchange = @Exchange(name = "direct_logs", declare = "false"),
key = {"info","warning","error"}
))
public void receive2(String msg) {
System.out.println("消费者2收到: " + msg);
}
}
package cn.tedu.rabbitmqspringboot.m5;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import javax.annotation.PostConstruct;
import java.util.UUID;
@SpringBootApplication
public class Main {
@Autowired
private Producer p;
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
//手动定义交换机
@Bean
public Queue rndQueue1(){
return new Queue(UUID.randomUUID().toString(), false, true, true);
}
@Bean
public Queue rndQueue2(){
return new Queue(UUID.randomUUID().toString(), false, true, true);
}
@Bean
public TopicExchange logsExchange(){
return new TopicExchange("topic_logs", false, false);
}
//spring扫描创建完所有的实例,完成所有的依赖注入后,自动执行@PostConsutruct注解的方法
@PostConstruct
public void test() {
/*new Thread(new Runnable() {
@Override
public void run() {
p.send();
}
}).start();*/
//lamda表达式 == Runnable匿名内部类
new Thread(() -> p.send()).start();
}
}
package cn.tedu.rabbitmqspringboot.m5;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Scanner;
@Component
public class Producer {
@Autowired
AmqpTemplate t;
public void send() {
while (true) {
System.out.println("输入消息: ");
String s = new Scanner(System.in).nextLine();
System.out.println("输入路由键: ");
String key = new Scanner(System.in).nextLine();
//默认发送持久消息
t.convertAndSend("topic_logs", key, s);
}
}
}
package cn.tedu.rabbitmqspringboot.m5;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class Consumer {
//每个@RabbitListener 都会注册成为一个消费者
@RabbitListener(bindings = @QueueBinding(
//不配置就是随机队列,非持久,独占,自动删除
//#{} - spring导航语言 spnl: 可以直接访问spring容器中的对象
//${} - Object导航语言 ognl
value = @Queue(name = "#{rndQueue1.name}",declare = "false"),
//交换机,declare=false -- 不重复定义交换机
exchange = @Exchange(name = "topic_logs", declare = "false"),
key = {"*.orange.*"}
))
public void receive1(String msg) {
System.out.println("消费者1收到: " + msg);
}
@RabbitListener(bindings = @QueueBinding(
//不配置就是随机队列,非持久,独占,自动删除
value = @Queue(name = "#{rndQueue2.name}",declare = "false"),
//交换机,declare=false -- 不重复定义交换机
exchange = @Exchange(name = "topic_logs", declare = "false"),
key = {"*.*.error","lazy.#"}
))
public void receive2(String msg) {
System.out.println("消费者2收到: " + msg);
}
}