我使用 Camel IMAPS 和计时器组件。
电子邮件已成功接收,我可以使用 MailboxConsumer 处理它们。
Timer 正在使用 MyConsumer bean 开始一些处理。
问题是我使用的每个消费者只需要单个线程。
但是 Camel 框架为我在项目中使用的每个消费者提供了两个线程。
它会导致对相同对象的双重处理,这对我的业务逻辑有害。
我需要建议如何配置camel以单线程模式(非并发)使用邮箱。
我的camel-context.xml片段:
<camelContext id="camel-mailbox" xmlns="http://camel.apache.org/schema/spring">
<package>my.package</package>
<endpoint id="mailboxEndpoint" uri="imaps://host?password=****&consumer.delay=60000&disconnect=false&closeFolder=false&peek=false&delete=true&fetchSize=1&maxMessagesPerPoll=1&mapMailMessage=false&unseen=true"/>
<route>
<from uri="timer://myTimer?period=180000"/>
<setBody><constant></constant></setBody>
<to uri="bean:myConsumer?method=process"/>
</route>
</camelContext>
<bean class="my.package.MyConsumer" name="myConsumer"/>
<bean class="my.package.MailboxConsumer"/>
MyConsumer 类:
public class MyConsumer {
public synchronized void process(){
// here my code runs
}
}
inputMialboxEndpoint 消费者:
public class MailboxConsumer {
@Consume(ref="mailboxEndpoint")
public synchronized void process(Exchange exchange) {
// here my code do runs
}
}
最佳答案
它在两个线程中运行,因为你有 2 个路由。一是直接定义,二是使用@Consume注解定义。
为什么只需要使用一个线程?有什么理由呢?也许您应该尝试将应用程序重构为线程安全?
但是如果你真的需要单线程,也许你应该尝试这样的事情:
<from uri="timer://myTimer?period=180000"/>
<setBody><constant></constant></setBody>
<setHeader headerName="componentType"><constant>timer</constant></setBody>
<to uri="seda:componentQueue">
<from endpoint="mailboxEndpoint"/>
<setHeader headerName="componentType"><constant>mail</constant></setBody>
<to uri="seda:componentQueue">
<from uri="seda:componentQueue?concurrentConsumers=1">
<choice //here choose component by header and proceed
关于java - Camel IMAPS 和 TIMER 组件 - 排除冗余线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23286295/