5

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.

  1. Thread 1 - Authenticates for User 1
  2. Thread 2 - Authenticates for User 2
  3. Thread 1 - Executes the code to download messages for User 1

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.

palacsint
  • 28,416
  • 10
  • 82
  • 109
Kros
  • 848
  • 1
  • 10
  • 24

1 Answers1

7

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.

Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71