1.连接
package com.utils;
import java.io.IOException;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class ConnectionUtils {
public static Connection getConnetions() throws IOException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("127.0.0.1");
factory.setPort(5672);
factory.setVirtualHost("/vhost_zq");
factory.setUsername("develop");
factory.setPassword("123456");
return factory.newConnection();
}
}
2.生产者
package com.routing;
import java.io.IOException;
import com.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
public class Send {
/**
* 路由模式: error
|===>queue---------->消费者
生产者-->交换机【type=direct】
* error&info&warning
|===>queue---------->消费者
*/
// 定义交换机
private static final String EXCHANGE_NAME = "test_exchange_direct";
public static void main(String[] args) throws IOException {
// 建立连接
Connection connetions = ConnectionUtils.getConnetions();
// 创建渠道
Channel channel = connetions.createChannel();
// 声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
// 定义每次消费者只能消费一条信息
channel.basicQos(1);
String msg = "hello zhangsan!!!";
// 定义路由
String routingKey = "info";
// 发布消息
channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes());
System.out.println("send " + msg);
channel.close();
connetions.close();
}
}
3.消费者1
package com.routing;
import java.io.IOException;
import com.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
public class Rec1 {
// 队列名称
private static final String QUEUE_NAME = "test_queue_direct_1";
// 交换机名称
private static final String EXCHANGE_NAME = "test_exchange_direct";
public static void main(String[] args) throws IOException {
// 建立连接
Connection connection = ConnectionUtils.getConnetions();
Channel channel = connection.createChannel();
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 声明路由
String routingKey = "error";
// 绑定交换机
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKey);
// 设置每次只消费一条消息
channel.basicQos(1);
// 定义消费者
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body, "utf-8");
System.out.println("[1] recieve" + msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[1] done!!!");
// 手动应答
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
// 开始消费消息 关闭自动应答
channel.basicConsume(QUEUE_NAME, false, consumer);
}
}
4.消费者2
package com.routing;
import java.io.IOException;
import com.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
public class Rec2 {
private static final String QUEUE_NAME = "test_queue_direct_2";
private static final String EXCHANGE_NAME = "test_exchange_direct";
public static void main(String[] args) throws IOException {
Connection connection = ConnectionUtils.getConnetions();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 声明多个路由
String routingKey1 = "error";
String routingKey2 = "info";
String routingKey3 = "warning";
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKey1);
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKey2);
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKey3);
channel.basicQos(1);
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body, "utf-8");
System.out.println("[2] recieve" + msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("[2] done!!!");
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
channel.basicConsume(QUEUE_NAME, false, consumer);
}
}
根据路由定义走那个队列。就可以根据业务需求来定义queue