I have a class/bean that manages an object (in this example the EngineManager contains an Engine object). The Engine object cannot be used concurrently and its initialization is a bit time consuming. However it is possible to create multiple instances of the EngineManager and hence multiple Engine instances.
public class EngineManager
{
private Engine engine;
@PostConstruct
public void init()
{
this.engine = // ... perform costly initialization
}
public void doSomethingWithEngine()
{
// ...
}
}
I'm trying to figure out which CDI scope to use for the class, that manages this object.
- I don't want to make the class a singleton, since I can create multiple instances of it and a singleton would be a bottleneck.
- I cannot use @ApplicationScoped due to concurrency issues.
- I don't want to use RequestScoped, because to my understanding this creates a new instance for every single request and the costly initialization of the Engine object would be a lot of overhead.
So my question is: Is there a (CDI) way to
- make the access to the EngineManager class thread-safe and
- have multiple instances of the EngineManager class, that are reused?