非法访问:此Web应用程序实例已经停止

我有一个类,它有一个在xml中定义的init方法

 

我的课:

 public class myClass{ private Thread t; public void init() { t = new Thread() { @Override public void run() { while (true) try { doStuff(); Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } }; t.start(); } public void destroy() { t.interrupt(); } } 

当应用程序启动时,这些线程运行正常,一切正常,一段时间后我得到以下exception。

 INFO: Illegal access: this web application instance has been stopped already. Could not load com.sun.mail.imap.IMAPStore. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact. java.lang.IllegalStateException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1273) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233) at javax.mail.Session.getService(Session.java:755) at javax.mail.Session.getStore(Session.java:569) at javax.mail.Session.getStore(Session.java:531) at javax.mail.Session.getStore(Session.java:510) 

在doStuff方法中:

 public void doStuff(){ Session sessioned = Session.getDefaultInstance(System.getProperties(), null); Store store = sessioned.getStore("imap"); store.connect(hostName, userName, password); . . . } 

我不知道为什么,有什么想法吗?

重新启动tomcat和apache后问题解决了,tomcat正在缓存应用程序的旧版本。

我怀疑这是在尝试取消部署您的应用程序后发生的。 你有没有杀掉你在init()过程中初始化的那个线程 ? 我会在相应的destroy()方法中执行此操作。

简而言之:当您正在热部署Web应用程序时,可能会发生这种情况。 例如,您的ide +开发服务器再次热部署战争。 之前创建的线程仍在运行。 但同时他们的类加载器/上下文无效并且面临IllegalAccessException / IllegalStateException,因为它已经重新部署了它的orgininating webapp(以前的运行时环境)。

因此,作为此处的状态,重新启动不会永久解决此问题。 相反,最好找到/实现托管的线程池,s。 像这样适当地处理线程的终止。 在JavaEE中,您将使用这些ManagedThreadExeuctorServices。 这里有类似的意见和参考 。

这方面的例子是Apache Commons Pool的EvictorThread,它根据池的配置(最大空闲等)“清理”池化实例。