I have a class called ProcessOrchestrator, it is a stateless bean that has a process() method that orchestrates complex process. To be able to run the process in a chunkwise manner, I have a class called UserHandler that is able to load and process users in oder to return to the ProcessOrchestrator.
UsersHandler will cache/store some information that are important only while the ProcessOrchestrator.process() is executing (as the list of users to be processed). When ProcessOrchestrator.process() is done, it should be disposed and if ProcessOrchestrator.process() is called again, a new object should be instantiated.
Due to the nature of the logic, UsersHandler should be statefull (it should store things that are going to live longer than just one method call). But I know that I should not inject statefull object into stateless objects because once the stateless object is reused by the application you can't predict the state of the statefull one.
I could get to something like this
@Stateless
public class ProcessOrchestrator {
@Resource
private SessionContext ctx;
@Asynchronous
public void process(){
UsersHandler usersHandler = ctx.lookup(...)
while(usersHandler.hasNext()){
List<User> users = usersHandler.next();
List<UserDoc> documents = convertToDocument(users);
storeInSolr(documents);
}
}
}
This creates a new statefull bean every method call and the state will be hold only during the process() method lifetime. But then this class is not testable anymore (I can't mock the UsersHandler).
My goals are:
-
Be able to store state in the UsersHandler
-
Have one users handler per process() invocation
-
Be able to test the process() method by mocking/stubbing UsersHandler without reflection
Is there any way I can make it work?
Aucun commentaire:
Enregistrer un commentaire