cancel
Showing results for 
Search instead for 
Did you mean: 

Statefulness of IdentityService

bmarkmann
Champ in-the-making
Champ in-the-making
It seems like the services provided by the ProcessEngine are all stateless, except the IdentityService…  ie. in most cases, you use a service to retrieve some bean and then work with that bean.  Based on the way that it's defined in the Spring configuration, it looks like the IdentityService (and the others) are singletons.  However, to start a process as a particular user you do:

  identityService.setAuthenticatedUserId("userx");

…and then call to the RuntimeService to start the process (without the user, just implicitly set by calling that method on the IndentityService.  Is this thread-safe?  In other words, if I have a lot of threads in my application simultaneously kicking off processes, is the identity and invocation of the runtime service local to my individual thread?  I'm wondering about this design decision and why the identity isn't just passed into the call to the runtime service instead of being split between the two calls.  I'm working through the engine source to understand its design and this approach jumped out at me.

Thanks! - Bill
2 REPLIES 2

bmarkmann
Champ in-the-making
Champ in-the-making
So, for anyone wondering…  I built a little test app that spun off 20 threads, and each created a process instance.  In between setting the authenticated user and kicking off the process instance, I put a randomized timer so that there was overlap between the threads setting different users and kicking off process instances.  Turned out each thread started the its process as the correct user, even with the overlapping calls.  After some more digging through the source, I found the reason – the identity set by the call to the identity service and used by the runtime service is stored in a thread-local string:

https://github.com/Activiti/Activiti/blob/master/modules/activiti-engine/src/main/java/org/activiti/...

Anyway, this was just a random question I had that wasn't entirely clear from the user guide… after building this little test I then stumbled across this in the JavaDocs: "void setAuthenticatedUserId(String authenticatedUserId): Passes the authenticated user id for this particular thread. All service method (from any service) invocations done by the same thread will have access to this authenticatedUserId."  Mystery solved. 🙂

frederikherema1
Star Contributor
Star Contributor
Thanks for coming back on this. Indeed, authentication mechanism uses thread-local storage of the current authenticated used, and is cleaned up afterwards as well, to make sure thread-pools (which reuse threads) don't get authentication mixed up…