Spring AMQP convertSendAndReceive 与 Restful 多线程生产者
Spring AMQP convertSendAndReceive with Restful multi threaded producer
我正在尝试确定 convertSendAndReceive 是否适用于以下用例:
我有一个 RESTful Web 服务需要进行 RPC 调用并获得响应才能为请求提供服务。我从来没有在 spring-amqp 或 RabbitMQ 中使用过回复功能。
这行得通吗,还是我正走在充满危险的道路上?
编辑:我担心的是生成消息的线程是否会得到正确的相应响应而不是另一个线程的响应。
我已经添加到 spring-amqp 文档中列出的名为 JavaConfigFixedReplyQueueTests 的测试中。我添加了以下测试用例:
(我的 connectionFactory bean 不同,但这只是为我公司的 rabbitmq 实例指定我们的 SSL 配置,所以我没有在此处列出。所有现有测试都通过了该更改。)
@Test
public void testReplyContainer_multiple_threads() throws Exception
{
fixedReplyQRabbitTemplate.setReplyTimeout(-1);
// limit the number of actual threads
int poolSize = 100;
ExecutorService service = Executors.newFixedThreadPool(poolSize);
List<Future<?>> futures = new ArrayList<>();
for(int n = 0; n < 1000; n++)
{
Future<?> f = service.submit(makeNumberedRunnable(n));
futures.add(f);
}
// wait for all tasks to complete before continuing
for(Future<?> f : futures)
{
f.get();
}
// shut down the executor service so that this thread can exit
service.shutdownNow();
}
private Runnable makeNumberedRunnable(int n)
{
return new Runnable()
{
@Override
public void run()
{
assertEquals("FOO" + n, fixedReplyQRabbitTemplate.convertSendAndReceive("foo" + n));
}
};
}
基本上我的测试是创建许多线程并确保返回正确的线程特定响应。测试顺利通过,这让我对自己的方法有了一点信心。
我希望 Gary Russell can chime in or maybe Artem Bilan 给我他们的专家意见。但是,我欢迎任何在这方面有知识的人给我他们的建议。
感谢您的宝贵时间。
所有 ...sendAndReceive(...)
方法都将正确关联 request/reply,以便在请求线程上返回正确的答复。
我正在尝试确定 convertSendAndReceive 是否适用于以下用例:
我有一个 RESTful Web 服务需要进行 RPC 调用并获得响应才能为请求提供服务。我从来没有在 spring-amqp 或 RabbitMQ 中使用过回复功能。
这行得通吗,还是我正走在充满危险的道路上?
编辑:我担心的是生成消息的线程是否会得到正确的相应响应而不是另一个线程的响应。
我已经添加到 spring-amqp 文档中列出的名为 JavaConfigFixedReplyQueueTests 的测试中。我添加了以下测试用例:
(我的 connectionFactory bean 不同,但这只是为我公司的 rabbitmq 实例指定我们的 SSL 配置,所以我没有在此处列出。所有现有测试都通过了该更改。)
@Test
public void testReplyContainer_multiple_threads() throws Exception
{
fixedReplyQRabbitTemplate.setReplyTimeout(-1);
// limit the number of actual threads
int poolSize = 100;
ExecutorService service = Executors.newFixedThreadPool(poolSize);
List<Future<?>> futures = new ArrayList<>();
for(int n = 0; n < 1000; n++)
{
Future<?> f = service.submit(makeNumberedRunnable(n));
futures.add(f);
}
// wait for all tasks to complete before continuing
for(Future<?> f : futures)
{
f.get();
}
// shut down the executor service so that this thread can exit
service.shutdownNow();
}
private Runnable makeNumberedRunnable(int n)
{
return new Runnable()
{
@Override
public void run()
{
assertEquals("FOO" + n, fixedReplyQRabbitTemplate.convertSendAndReceive("foo" + n));
}
};
}
基本上我的测试是创建许多线程并确保返回正确的线程特定响应。测试顺利通过,这让我对自己的方法有了一点信心。
我希望 Gary Russell can chime in or maybe Artem Bilan 给我他们的专家意见。但是,我欢迎任何在这方面有知识的人给我他们的建议。
感谢您的宝贵时间。
所有 ...sendAndReceive(...)
方法都将正确关联 request/reply,以便在请求线程上返回正确的答复。