rabbitmq学习——routingkey
public class Send {
public static final String exchange = "direct_log_son";
public static void main(String[] args) throws Exception{
Connection conn = ChanncelFactory.getChanncelFactory();
Channel channel = conn.createChannel();
//这里采用了直连 之前的扇形 是对所有接收者进行发送
channel.exchangeDeclare(exchange, "direct");
String body = "获取子集";
//这里与之前的增加了 routingkey 获取的时候 可以根据这个进行区分
channel.basicPublish(exchange, "abcde", null, body.getBytes());
ChanncelFactory.close(channel, conn);
}
}
public class Receive {
public static void main(String[] args) throws Exception{
Connection conn = ChanncelFactory.getChanncelFactory();
Channel channel = conn.createChannel();
channel.exchangeDeclare(Send.exchange, "direct");
String queuename = channel.queueDeclare().getQueue();
//这里的abcde 也可是 defg 就是获取的不同而已
channel.queueBind(queuename, Send.exchange, "abcde");
System.out.println(queuename);
Consumer callback = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
BasicProperties properties, byte[] body) throws IOException {
System.out.println("内容: "+new String(body, "utf-8"));
}
};
for(int i=1; i<6; i++){
Thread.sleep(2000);
channel.basicConsume(queuename, true, callback);
}
ChanncelFactory.close(channel, conn);
}
}