Reputation: 737
I use Camel IMAPS and Timer components.
An emails are received successfully and I can process them using MailboxConsumer.
And Timer is starting some processing using MyConsumer bean.
The problem is that I need only single thread for every consumer I use.
But the Camel framework provides for me two threads for every consumer I use in my project.
It leads to double processing of the same objects that is harmful for my business logic.
I need advice how to configure camel to consume mailbox in single thread mode (not concurrently).
My camel-context.xml fragment:
<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 class:
public class MyConsumer {
public synchronized void process(){
// here my code runs
}
}
inputMialboxEndpoint consumer:
public class MailboxConsumer {
@Consume(ref="mailboxEndpoint")
public synchronized void process(Exchange exchange) {
// here my code do runs
}
}
Upvotes: 0
Views: 169
Reputation: 21
It's runinng in two threads because you have 2 routes. One is directly defined, second is defined using @Consume annotation.
Why you need use only one thread? What's a reason? Maybe you should try to refactor application to thread safe?
But if you really need single thread than maybe you should try something like that:
<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
Upvotes: 2