生产者将消息发送到消息系统的时候,不是直接发送到队列,而是发送到交换机,由交换机路由到相应的队列
交换机有三种路由模式
1.direct
交换机通过一个routing key和队列绑定,当生产者生产消息的时候指定一个routing key,当绑定队列的routing key和生产者发送的routing key一致,那么交换机就会把消息发送到这个队列
public class ConnectionUtil {
public static String QUEUE_NAME = "testQueue";
public static String EXCHANGE_NAME = "testExchange";
public static Connection getConnection() throws Exception{
ConnectionFactory factory = new ConnectionFactory();
//设置服务端所在地址
factory.setHost("127.0.0.1");
//设置端口号
factory.setPort(5672);
//设置用户名
factory.setUsername("helloWorld");
//设置密码
factory.setPassword("helloWorld");
//设置虚拟地址
factory.setVirtualHost("testHost");
return factory.newConnection();
}
}
public class Send {
public static void main(String[] args) throws Exception{
//获取连接
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
//声明队列
channel.queueDeclare("testQueue1",false,false,false,null);
channel.queueDeclare("testQueue2",false,false,false,null);
channel.queueDeclare("testQueue3",false,fals