模型:
1、生产者
package com.june.rabbitmq.topic;
import com.june.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Send {
private static final String EXCHANGE_NAME = "june_exchange_topic";
public static void main(String[] args) {
try {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
//声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, "topic");//"fanout"分发
String routingKey = "goods.update";
String msg = "routingKey = " + routingKey + ", juneli direct!";
channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes());
System.out.println("routingKey = " + routingKey + ", send message : " + msg );
channel.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
2、消费者1
package com.june.rabbitmq.topic;
import com.june.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Recevice1 {
private static final String QUEUE_NAME = "june_work_topic_queue1";
private static final String EXCHANGE_NAME = "june_exchange_topic";
public static void main(String[] args) {
try {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");//声明topic交换机
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String routingKey = "goods.add";
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME , routingKey);
//设置同时只消费一条
channel.basicQos(1);
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "utf-8");
System.out.println("[1] recevice message : " + message);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println("[1] done!");
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
//自动应答改为false
boolean autoAck = false;
//监听队列 注册一个监听事件
channel.basicConsume(QUEUE_NAME, autoAck, consumer);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
3、消费者2
package com.june.rabbitmq.topic;
import com.june.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Recevice2 {
private static final String QUEUE_NAME = "june_work_topic_queue2";
private static final String EXCHANGE_NAME = "june_exchange_topic";
public static void main(String[] args) {
try {
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");//声明topic交换机
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String routingKey = "goods.#";
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME , routingKey);
channel.basicQos(1);
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "utf-8");
System.out.println("[2] recevice message : " + message);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println("[2] done!");
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
boolean autoAck = false;
//监听队列 注册一个监听事件
channel.basicConsume(QUEUE_NAME, autoAck, consumer);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}