forked from andyadc/codeblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
codeblocks-framework/src/test/java/com/andyadc/test/rabbitmq/PerformanceTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} | ||
} |