I've managed to find a partial solution. I now have an instance of my serviceActivator(EchoService) per simulation. But I am still searching for a way to have a DEDICATED thread per simulation.
Code:
@Component
@Scope( "game" )
public class ScopedThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
private static final int CORE_POOL_SIZE = 1;
private static final int MAX_POOL_SIZE = 1;
public ScopedThreadPoolTaskExecutor() {
super();
setCorePoolSize( CORE_POOL_SIZE );
setMaxPoolSize( MAX_POOL_SIZE );
}
}
and
Code:
<integration:channel id="singleThreadedSimulationChannel">
<integration:dispatcher task-executor="scopedThreadPoolTaskExecutor"/>
</integration:channel>
solved the problem.
For completeness of the example; this is my serviceActivator definition
Code:
<integration:service-activator input-channel="singleThreadedSimulationChannel" output-channel="outChannel"
ref="echoService"/>
There where my previous solution always pointed to the same instance of my serviceActivator(EchoService), the new solution points to an instance per simulation.
Eg. log output before changes:
Code:
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## instance = org.acme.EchoService@150382b5
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## message = Message for simulation 2
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## instance = org.acme.EchoService@150382b5
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## message = Mensaje para simulation 3
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## instance = org.acme.EchoService@150382b5
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## message = Mensaje 2 para simulation 3
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## instance = org.acme.EchoService@150382b5
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## message = Other message for simulation 2
vs. log output after changes:
Code:
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## instance = org.acme.EchoService@702b4b78
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## message = Message for simulation 2
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## instance = org.acme.EchoService@7abbf606
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## message = Mensaje para simulation 3
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## instance = org.acme.EchoService@7abbf606
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## message = Mensaje 2 para simulation 3
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## instance = org.acme.EchoService@702b4b78
DEBUG[Sim-Exec-Thread-1] org.acme.EchoService - ## message = Other message for simulation 2
They are still all on Sim-Exec-Thread-1, but other instances are handling the messages, based on their simulation-id. Note that I can verify this because the EchoService also writes the message in the database for the given simulation. This way, I can assure myself that the code works as desired.
However, one problem remains: HOW do I move execution of the messages to a DEDICATED thread per simulation?