Pregunta

In my servlet I start a background thread onContextInitialized() and consequently I need to stop it onContextDestroyed(). So my servlet needs a private instance variable (let's call this daemon) pointing to this daemon thread. The daemon runs fine, but the reference is lost as soon as I exit contextInitialized()! For example, if I implement doGet() to show the status of the thread, it throws a NPE

So the answer is, who can access my servlet private instance variable and set it to null? I remark that the thread runs just fine, just the reference is broken

¿Fue útil?

Solución

Just set the reference in Application Context or set it to some static field of some class[less preferable]

Not sure what you want to achieve but this will do

Otros consejos

Make it static so all instances can see it:

private static Thread daemon;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top