消费者
import com.rabbitmq.client.*;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.shterm.test.ConnectionUtil;
import java.io.IOException;
import java.util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MyConsumer {
private final static String QUEUE_NAME = "topic_queue_1";
private final static String EXCHANGE_NAME = "topic_exchange";
public static void main(String[] args) throws InterruptedException {
System.out.println("Consumer start.......");
ExecutorService exe= Executors.newCachedThreadPool();
for (int i=0;i<10;i++){
Thread.sleep(2);
exe.execute(()->work());
}
}
public static void work(){
try(Connection connection = ConnectionUtil.getConnection("127.0.0.1",5671,"/","test","test");){
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"update.#");
channel.basicQos(1);
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
AMQP.BasicProperties replyProps = new AMQP.BasicProperties.Builder()
.correlationId(properties.getCorrelationId()).build();
String response = "";
try {
response += "ok";
} catch (RuntimeException e) {
System.out.println(" [.] " + e.toString());
} finally {
channel.basicPublish("", properties.getReplyTo(), replyProps, response.getBytes("UTF-8"));
channel.basicAck(envelope.getDeliveryTag(), false);
synchronized (this) {
this.notify();
}
}
}
};
channel.basicConsume(QUEUE_NAME,consumer);
while (true) {
synchronized (consumer) {
try {
consumer.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
生产者
import com.rabbitmq.client.*;
import com.shterm.test.ConnectionUtil;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Producer {
private final static String EXCHANGE_NAME = "topic_exchange";
public static void main(String[] args) throws InterruptedException {
ExecutorService exe = Executors.newFixedThreadPool(5);
System.out.println("Producer start.......");
while(true){
Thread.sleep(1000);
for(int i=0;i<130;i++){
exe.execute(() -> produce());
}
}
}
public static void produce() {
try (Connection connection = ConnectionUtil.getConnection("127.0.0.1", 5671, "/", "test", "test");) {
String replyQueueName;
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
String corrId = UUID.randomUUID().toString();
String message = "this is topic :" + corrId;
replyQueueName = channel.queueDeclare().getQueue();
synchronized (replyQueueName) {
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().correlationId(corrId).replyTo(replyQueueName)
.build();
channel.basicPublish(EXCHANGE_NAME, "update.Name", props, message.getBytes());
final BlockingQueue<String> response = new ArrayBlockingQueue<String>(1);
channel.basicConsume(replyQueueName, true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
if (properties.getCorrelationId().equals(corrId)) {
response.offer(new String(body, "UTF-8"));
}
}
});
channel.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}