instanceOfObject instanceOfObject - 7 months ago 137
Java Question

Multiple spring task executors with annotation-driven spring tasks

I have a class

MessageProcessor
being called by another method in another class (i.e. Caller).

public class Caller {
@Scheduled(filxedDelay=10)
public void poll(){
//do stuff
messageProcessor.process(msg);
}

}

public class MessageProcessor{

@Async(value="abcExecutor")
public void process(String msg){
//do stuff here.
}

}


Spring file looks like:

<task:executor id="abcExecutor" pool-size="9" rejection-policy-"CALLER_RUNS"/>


I want to add another @Async executor:

@Async(value="defExecutor")
public void remove(String msg){
//do stuff here.
}

@Scheduled(filxedDelay=10)
public void kill(){
//do stuff
messageProcessor.remove(msg);
}


By adding another executor in spring file:

<task:executor id="defExecutor" pool-size="9" rejection-policy="CALLER_RUNS"/>


But how to add multiple executors in
<task:annotation-driven executor="abcExecutor" scheduler="scheduler" mode="proxy" proxy-target-class="true"/>


How can I make these multiple executors run with annotation?

PS: Obviously, I don't want to have the same pool being used for both the
@Async
methods

Answer

The @Async("defExecutor") is sufficient to designate the method to be handled by the 2nd executor. The xml declaration specifies only the default executor, which will be used whenever no value is specified in @Async.

See the explanation of Chris Beams in this issue:

Comments