图解
1、web端操作
1、新建3个队列
2、新建一个交换机
3、交换机绑定队列
这里的路由key是什么意思呢?留一个悬念
4、测试发送消息
发送消息前:
发送消息:
再次测试
经过多次测试发现了规律吗?
5、小结
#:任意级目录,任意目录什么意思呢?就是可以为空也可以多个 举例: .com.sky 或者 com.com.com.sky
*:一级目录,一级就是com。就没了
2、代码操作
生产者
package com.sky.rabbitmq.topics;
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 = "topic-exchange";
// 路由key
String routeKey = "com.user.sky";
// 交换机类型
String type = "topic";
// 发送消息给中间件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.topics;
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 = "topic-exchange";
// 路由key
String routeKey = "";
// 交换机类型
String type = "topic";
// 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();
}
}