我正在开发一个应用程序(使用Camel 2.13.2),它需要使用ProducerTemplate向不同的端点发送不同的消息。最初,我们为每个消息创建了一个新的ProducerTemplate,但是在阅读了本文讨论ProducerTemplate的使用之后,我决定重构并尝试在每个camel上下文中使用一个ProducerTemplate。
然而,事实证明,这比最初看来要复杂得多。我遇到了使用单个ProducerTemplate进行junit测试的困难(我们有一些复杂的测试,涉及关闭和启动各个路由),现在我想知道,我是否试图过高地接受这个警告,并且在这种情况下可以接受多个ProducerTemplate。
这里是我的问题:根据上面链接的文章中的建议,创建每个端点实例的是否可以接受?(只要需要模板,模板就会被保存在身边)
示例:(如果我有端点direct:processA、direct:processB和direct:processC ),是否可以这样做:
ProducerTemplate templateA = context.createProducerTemplate();
ProducerTemplate templateB = context.createProducerTemplate();
ProducerTemplate templateC = context.createProducerTemplate();
templateA.setDefaultEndpointUri("direct:processA");
templateB.setDefaultEndpointUri("direct:processB");
templateC.setDefaultEndpointUri("direct:processC");
// in thread A
templateA.sendBody(bodyA);
// in thread B
templateB.sendBody(bodyB);
// in thread C
templateC.sendBody(bodyC);还是作者打算只为所有端点创建一个ProducerTemplate:
ProducerTemplate template = context.createProducerTemplate();
// in thread A
template.sendBody("direct:processA", bodyA);
// in thread B
template.sendBody("direct:processB", bodyB);
// in thread C
template.sendBody("direct:processC", bodyC);发布于 2015-01-04 08:43:14
你可以两者兼得。例如,如果您使用Camel POJO制作它的工作,就像封面下的第一个。
它是创建每个请求的新模板是错误的。参见这个常见问题:http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html
https://stackoverflow.com/questions/27749646
复制相似问题