At the moment I am struggling with a bug in Sun's class java.net.Authenticator
. It seems that the Authenticator
class has a system wide static Authenticator
. This results in the following problem in my multi-threaded application.
At this point, the system will get the messages for User 2 instead of the messages for User 1.
I have tried searching for a solution. Many suggested trying the following code:
AuthCacheValue.setAuthCache(new AuthCacheImpl());
Authenticator.setDefault(exchangeAuthenticator);
However, this does not work for me since my application is multi-threaded (exchangeAuthenticator
will always be set to the authenticator initialized in the latest thread).
If anyone has any idea, even a hack would do at the moment, I would really appreciate that, since at the moment the only 'neat' solution is to place a synchronized
on the main execution method with a dramatic effect on performance.
In my experience, your best bet is to ditch the builtin http support and get Apache's HttpClient.
If that's not an option, create a single object extending Authenticator, but store the username and password in ThreadLocal instances. That way each thread can retrieve it's own values.