1.工作者模式:
特点:
1. 一个生产者
2. 由多个消费。
3. 统一个队列。
4. 这些消费者之间存在竞争关系。用处:
比如批量处理上. rabbitMQ里面积压了大量的消息。
生产者
package com.ykq.work;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Product {
public static void main(String[] args)throws Exception {
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.213.188");
//创建连接对象Connection
Connection connection=factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//创建队列
/**
* String queue, 队列的名称
* boolean durable, 是否该队列持久化 rabbitMQ服务重启后该存放是否存在。
* boolean exclusive, 是否独占 false
* boolean autoDelete, 是否自动删除 如果长时间没有发生消息 则自动删除
* Map<String, Object> arguments 额外参数 先给null
*/
channel.queueDeclare("ban129_queue_work",true,false,false,null);
//发生消息
/**
* String exchange: 交换机的名称 如果没有则使用“” 它回自动采用默认
* String routingKey, 路由key 如果没有交换机的绑定 使用队列的名称
* BasicProperties props, 消息的一些额外配置 目前先不加 null
* byte[] body 消息的内容
*/
for(int i=0;i<10;i++) {
String msg = "真难!!!!!!!!!!!"+i;
channel.basicPublish("", "ban129_queue_work", null, msg.getBytes());
}
//生产者这里可以管理资源 消费者不能关闭资源。
channel.close();
connection.close();
}
}
消费者01:
package com.ykq.work;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Consumer01 {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.213.188");
//创建连接对象Connection
Connection connection=factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//接受消息
/**
* (String queue, 队列的名称
* boolean autoAck, 是否自动确认
* Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
*/
DefaultConsumer callback=new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//body 接受的信息
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者01:"+new String(body));
}
};
channel.basicConsume("ban129_queue_work",true,callback);
}
}
消费者02:
package com.ykq.work;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Consumer02 {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.213.188");
//创建连接对象Connection
Connection connection=factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//接受消息
/**
* (String queue, 队列的名称
* boolean autoAck, 是否自动确认
* Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
*/
DefaultConsumer callback=new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//body 接受的信息
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者01:"+new String(body));
}
};
channel.basicConsume("ban129_queue_work",true,callback);
}
}
2.发布订阅模式
- 特点:
1.一个生产者
2.多个消费者
3.多个队列。
4.交换机 转发消息。
生产者:
package com.ykq.fanout;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Product {
public static void main(String[] args)throws Exception {
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.213.188");
//创建连接对象Connection
Connection connection=factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//创建队列
/**
* String queue, 队列的名称
* boolean durable, 是否该队列持久化 rabbitMQ服务重启后该存放是否存在。
* boolean exclusive, 是否独占 false
* boolean autoDelete, 是否自动删除 如果长时间没有发生消息 则自动删除
* Map<String, Object> arguments 额外参数 先给null
*/
channel.queueDeclare("ban129_queue_fanout01",true,false,false,null);
channel.queueDeclare("ban129_queue_fanout02",true,false,false,null);
//创建交换机
/**
* String exchange,交换机的名称
* BuiltinExchangeType type, 交换机的类型
* boolean durable:是否持久化
*/
channel.exchangeDeclare("ban129_exchange", BuiltinExchangeType.FANOUT,true);
/**
* String queue, 队列名
* String exchange, 交换机的名称
* String routingKey:路由key 如果交换机为fanout模式则不需要路由key
*/
channel.queueBind("ban129_queue_fanout01","ban129_exchange","");
channel.queueBind("ban129_queue_fanout02","ban129_exchange","");
//发生消息
/**
* String exchange: 交换机的名称 如果没有则使用“” 它回自动采用默认
* String routingKey, 路由key 如果没有交换机的绑定 使用队列的名称
* BasicProperties props, 消息的一些额外配置 目前先不加 null
* byte[] body 消息的内容
*/
for(int i=0;i<10;i++) {
String msg = "刘亚飞真帅,好像没有见过他说话!!!!!!!!!!!"+i;
channel.basicPublish("ban129_exchange", "", null, msg.getBytes());
}
//生产者这里可以管理资源 消费者不能关闭资源。
channel.close();
connection.close();
}
}
消费者:
package com.ykq.fanout;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Consumer01 {
public static void main(String[] args) throws Exception{
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.213.188");
//创建连接对象Connection
Connection connection=factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//接受消息
/**
* (String queue, 队列的名称
* boolean autoAck, 是否自动确认
* Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
*/
DefaultConsumer callback=new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//body 接受的信息
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者01:"+new String(body));
}
};
channel.basicConsume("ban129_queue_fanout01",true,callback);
}
}
3.路由模式:
特点:
1.一个生产者
2.多个消费者
3.多个队列。
4.交换机 转发消息。
5.routekey:路由key 只要routekey匹配的消息可以到达对应队列。
package com.ykq.direct;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Product {
public static void main(String[] args)throws Exception {
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.213.188");
//创建连接对象Connection
Connection connection=factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//创建队列
/**
* String queue, 队列的名称
* boolean durable, 是否该队列持久化 rabbitMQ服务重启后该存放是否存在。
* boolean exclusive, 是否独占 false
* boolean autoDelete, 是否自动删除 如果长时间没有发生消息 则自动删除
* Map<String, Object> arguments 额外参数 先给null
*/
channel.queueDeclare("ban129_queue_direct01",true,false,false,null);
channel.queueDeclare("ban129_queue_direct02",true,false,false,null);
//创建交换机
/**
* String exchange,交换机的名称
* BuiltinExchangeType type, 交换机的类型
* boolean durable:是否持久化
*/
channel.exchangeDeclare("ban129_exchange_direct", BuiltinExchangeType.DIRECT,true);
/**
* String queue, 队列名
* String exchange, 交换机的名称
* String routingKey:路由key 如果交换机为fanout模式则不需要路由key
*/
channel.queueBind("ban129_queue_direct01","ban129_exchange_direct","error");
channel.queueBind("ban129_queue_direct02","ban129_exchange_direct","info");
channel.queueBind("ban129_queue_direct02","ban129_exchange_direct","error");
channel.queueBind("ban129_queue_direct02","ban129_exchange_direct","warning");
//发生消息
/**
* String exchange: 交换机的名称 如果没有则使用“” 它回自动采用默认
* String routingKey, 路由key 如果没有交换机的绑定 使用队列的名称
* BasicProperties props, 消息的一些额外配置 目前先不加 null
* byte[] body 消息的内容
*/
for(int i=0;i<10;i++) {
String msg = "你们有没有打疫苗!!!!!!!!!!!"+i;
channel.basicPublish("ban129_exchange_direct", "error", null, msg.getBytes());
}
//生产者这里可以管理资源 消费者不能关闭资源。
channel.close();
connection.close();
}
}
4. topic主体模式
- 绑定按照通配符的模式。
*: 统配一个单词。
#: 统配n个单词hello.orange.rabbit
lazy.orange
生产者:
package com.ykq.topic;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Product {
public static void main(String[] args)throws Exception {
//创建连接工厂 --配置连接信息
ConnectionFactory factory=new ConnectionFactory();
factory.setHost("192.168.213.188");
//创建连接对象Connection
Connection connection=factory.newConnection();
//创建信道
Channel channel = connection.createChannel();
//创建队列
/**
* String queue, 队列的名称
* boolean durable, 是否该队列持久化 rabbitMQ服务重启后该存放是否存在。
* boolean exclusive, 是否独占 false
* boolean autoDelete, 是否自动删除 如果长时间没有发生消息 则自动删除
* Map<String, Object> arguments 额外参数 先给null
*/
channel.queueDeclare("ban129_queue_topic01",true,false,false,null);
channel.queueDeclare("ban129_queue_topic02",true,false,false,null);
//创建交换机
/**
* String exchange,交换机的名称
* BuiltinExchangeType type, 交换机的类型
* boolean durable:是否持久化
*/
channel.exchangeDeclare("ban129_exchange_topic", BuiltinExchangeType.TOPIC,true);
/**
* String queue, 队列名
* String exchange, 交换机的名称
* String routingKey:路由key 如果交换机为fanout模式则不需要路由key
*/
channel.queueBind("ban129_queue_topic01","ban129_exchange_topic","*.orange.*");
channel.queueBind("ban129_queue_topic02","ban129_exchange_topic","*.*.rabbit");
channel.queueBind("ban129_queue_topic02","ban129_exchange_topic","lazy.#");
//发生消息
/**
* String exchange: 交换机的名称 如果没有则使用“” 它回自动采用默认
* String routingKey, 路由key 如果没有交换机的绑定 使用队列的名称
* BasicProperties props, 消息的一些额外配置 目前先不加 null
* byte[] body 消息的内容
*/
for(int i=0;i<10;i++) {
String msg = "你们有没有打疫苗!!!!!!!!!!!"+i;
channel.basicPublish("ban129_exchange_topic", "lazy.orange.rabbit", null, msg.getBytes());
}
//生产者这里可以管理资源 消费者不能关闭资源。
channel.close();
connection.close();
}
}
5. 简单模式
(1)简单模式:特点:只有生产者消费者和队列组成。