图解
1、web端操作
1、创建一个exchange
模式要选direct
2、绑定队列
一个队列可以绑定多次
路由key就相当于条件筛选
3、发布消息
还没发消息前:
发消息后:
路由key为wechat的都收到了消息
举例:
比如说我们刷b站的时候,你关注了一个up主,然后你的同学没有关注,那么这个up主更新视频的时候只要关注了的收到了消息,没关注的就没收到。
2、代码操作
生产者
package com.sky.rabbitmq.direct;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
* @author 尹稳健~
* @version 1.0
*/
public class Producer {
public static void main(String[] args) {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("localhost");
connectionFactory.setPort(5672);
connectionFactory.setUsername("admin");
connectionFactory.setPassword("admin");
connectionFactory.setVirtualHost("/");
Connection connection = null;
Channel channel = null;
try {
connection = connectionFactory.newConnection();
channel = connection.createChannel();
// 消息
String message = "谢谢关注";
// 交换机名字
String exchange_name = "direct-exchange";
// 路由key
String routeKey = "wechat";
// 交换机类型
String type = "direct";
// 发送消息给中间件rabbitmq-server
// @params1: 交换机exchange
// @params2: 队列名称/routingkey
// @params3: 属性配置
// @params4: 发送消息的内容
channel.basicPublish(exchange_name,routeKey,null,message.getBytes());
System.out.println("消息发送成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("发送消息出现异常...");
} finally {
if (channel != null && channel.isOpen()) {
try {
channel.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
消费者
package com.sky.rabbitmq.direct;
import com.rabbitmq.client.*;
import java.io.IOException;
/**
* @author 尹稳健~
* @version 1.0
*/
public class Consumer {
private static Runnable runnable = new Runnable() {
public void run() {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("localhost");
connectionFactory.setPort(5672);
connectionFactory.setUsername("admin");
connectionFactory.setPassword("admin");
connectionFactory.setVirtualHost("/");
//获取队列的名称
final String queueName = Thread.currentThread().getName();
Connection connection = null;
Channel channel = null;
try {
connection = connectionFactory.newConnection();
channel = connection.createChannel();
// 交换机名字
String exchange_name = "direct-exchange";
// 路由key
String routeKey = "";
// 交换机类型
String type = "direct";
// 6: 定义接受消息的回调
channel.basicConsume(queueName, true, new DeliverCallback() {
public void handle(String s, Delivery delivery) throws IOException {
System.out.println(queueName + ":收到消息是:" + new String(delivery.getBody(), "UTF-8"));
}
}, new CancelCallback() {
public void handle(String s) throws IOException {
System.out.println("异常");
}
});
System.out.println(queueName + ":开始接受消息");
System.in.read();
} catch (Exception e) {
e.printStackTrace();
System.out.println("发送消息出现异常...");
} finally {
if (channel != null && channel.isOpen()) {
try {
channel.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
};
public static void main(String[] args) {
new Thread(runnable,"queue1").start();
new Thread(runnable,"queue2").start();
new Thread(runnable,"queue3").start();
}
}