Skip to content

Commit

Permalink
rabbitmq
Browse files Browse the repository at this point in the history
  • Loading branch information
andyadc committed Dec 10, 2024
1 parent 0e9b935 commit 4c458f8
Showing 1 changed file with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.andyadc.test.rabbitmq;

import com.andyadc.codeblocks.framework.rabbitmq.ChannelPool;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class PerformanceTests {

public static void main(String[] args) throws Exception {
testThreadPool();
testNormal();
}

private static void testNormal() throws Exception {
ConnectionFactory connectionFactory = new ConnectionFactory();
final Connection connection = connectionFactory.newConnection();

for (int i = 0; i < 10; i++) {
new Thread(() -> {
try {
while (true) {
Channel channel = connection.createChannel();

final String EXCHANGE_NAME = "dyh";
channel.exchangeDeclare(EXCHANGE_NAME, "direct");

//发送消息
channel.basicPublish(EXCHANGE_NAME, "1", null, "s".getBytes());

//接收消息
// String queueName = channel.queueDeclare().getQueue();
// channel.queueBind(queueName, EXCHANGE_NAME, "1");
// QueueingConsumer consumer = new QueueingConsumer(channel);
// channel.basicConsume(queueName, false, consumer);
// QueueingConsumer.Delivery delivery = consumer.nextDelivery(1);
// if (delivery != null) {
// channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
// }
// channel.basicCancel(consumer.getConsumerTag());

//关闭资源
channel.close();
}
} catch (Exception e) {

}
}).start();
}

}

private static void testThreadPool() {
final ChannelPool channelPool = new ChannelPool();
for (int i = 0; i < 10; i++) {
new Thread(() -> {
try {
while (true) {
Channel channel = channelPool.getChannel();

final String EXCHANGE_NAME = "dyh";
channel.exchangeDeclare(EXCHANGE_NAME, "direct");

//发送消息
channel.basicPublish(EXCHANGE_NAME, "1", null, "s".getBytes());

//接收消息
// String queueName = channel.queueDeclare().getQueue();
// channel.queueBind(queueName, EXCHANGE_NAME, "1");
// QueueingConsumer consumer = new QueueingConsumer(channel);
// channel.basicConsume(queueName, false, consumer);
// QueueingConsumer.Delivery delivery = consumer.nextDelivery(1);
// if (delivery != null) {
// channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
// }
// channel.basicCancel(consumer.getConsumerTag());

//关闭资源
channelPool.returnChannel(channel);
}
} catch (Exception e) {

}
}).start();
}

}
}

0 comments on commit 4c458f8

Please sign in to comment.