3

Tomcat で Wicket Web アプリケーションを実行しています。アプリケーションは、(org.springframework.web.context.ContextLoaderListener を介して) Spring を使用してアプリケーションを初期化します。これは起動には問題ありませんが、生成されたスレッドをシャットダウンできるように、コンテキストが破棄されているという何らかの通知を受け取ることも望んでいます。そのような通知を受け取る方法はありますか? 私の質問の理解を助けるために、私のアプリケーションからの抜粋を含めました.

web.xml 抽出

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/mysite/web/spring/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

Spring applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
    "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
    <bean id="MyWebService" class="com.mysite.web.MyWebApp">
    </bean>
</beans>

MyWebApp.java 抽出

public class MyWebApp extends org.apache.wicket.protocol.http.WebApplication {
    private MyWebServiceServiceAPI webservice =
       MyWebServiceAppImpl.getMyWebService();

    public MyWebServiceWebApp() {
    }

    @Override
    public void init() {
        super.init();
        webservice.start();
    }
}

MyWebServiceAppImpl.java 抽出

    public class MyWebServiceAppImpl  extends ServiceImpl
        implements MyWebServiceServiceAPI  {
        // The ServiceImpl implements the Callable<T> interface

    private static MyWebServiceServiceAPI instance;
    private List<Future<ServiceImpl>> results =
        new ArrayList<Future<ServiceImpl>>();
    private ExecutorService pool = Executors.newCachedThreadPool();


    private MyWebServiceAppImpl() {
        super(.....);
    }

    public synchronized static MyWebServiceServiceAPI getMyWebService() {
        if (instance == null) {
            instance = new MyWebServiceAppImpl();
            instance.start();
        }
        return instance;
    }

@Override
public synchronized void start() {

    if (!started()) {
        pool.submit(this);
        super.start();
    }
}
4

1 に答える 1

2

はい、独自のContextListenerを実装できます。

package myapp;

public class MyContextListener implements ServletContextListener {



    public void contextInitialized(ServletContextEvent arg0) {
        //called when context is started
    }


    public void contextDestroyed(ServletContextEvent arg0) {
      //called context destroyed
    }   
}

そのリスナーを web.xml に追加する必要があります

 <listener>
    <listener-class>myapp.MyContextListener</listener-class>
 </listener>
于 2010-02-21T13:03:09.420 に答える