开发者

Can some code run when we first deploy a WAR file?

Is there any method or API that I can use so that whenever I deploy a new WAR file, a part of code should execute or perhaps when To开发者_如何转开发mcat starts, the respective servlet should start or run some code continuously.


Reviving an old question since the only answer doesn't show any example.

In order to run a custom piece of code whenever a web application WAR is deployed/undeployed or Tomcat is started/stopped, you need to:

  1. Implement the ServletContextListener listener and its methods contextInitialized() and contextDestroyed().
  2. Let Tomcat know about your implementation. According to the documentation, you can either add the implementing class to the deployment descriptor, annotate it with WebListener, or register it via one of the addListener() methods defined on ServletContext.

Here is an example (based on this post):

package com.example;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyServletContextListener implements ServletContextListener {
    /** The servlet context with which we are associated. */
    private ServletContext context = null;

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        log("Context destroyed");
        this.context = null;
    }

    @Override
    public void contextInitialized(ServletContextEvent event) {
        this.context = event.getServletContext();
        log("Context initialized");
    }

    private void log(String message) {
        if (context != null) {
            context.log("MyServletContextListener: " + message);
        } else {
            System.out.println("MyServletContextListener: " + message);
        }
    }
}

And add the following to web.xml (or alternatively, use the WebListener annotation or addListener() method):

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    ...
    <listener>
        <listener-class>com.example.MyServletContextListener</listener-class>
    </listener>
</web-app>


You can include a "ContextListener" in web.xml. An instance of this class would be created when Webb application WAR is about to be deployed/started.

This code can start a thread that would keep running till app is deployed.

Example : http://www.javafaq.nu/java-example-code-233.html


i find a way to run some code only at application (*.war) deployment: it works at least for jersey servlets with java servlet using javax.ws.rs.core.Application .

The application in the file ApplicationConfig.java (see below) contains a method public getClasses which is called at application deployment. So adding code in getClasses result in having it executed at application deployment. The only caveit i noticed is that strangely this function is called twice, do not know why ,so i added a global variable in the ApplicationConfig class to know if my code has already run.

Here is my solution:

package eu.oca;

import java.util.Set;
import javax.ws.rs.core.Application;

/**                                                                                                                             
 *                                                                                                                              
 * @author mattei                                                                                                               
 */
@javax.ws.rs.ApplicationPath("jersey")
public class ApplicationConfig extends Application {

    private boolean alreadyRun = false;

    @Override
    public Set<Class<?>> getClasses() {

        System.out.println("Sidonie : ApplicationConfig : getClasses : alreadyRun = " + String.valueOf(alreadyRun));
        alreadyRun = true;
        Set<Class<?>> resources = new java.util.HashSet<>();
        addRestResourceClasses(resources);
        return resources;
    }

    /**                                                                                                                         
     * Do not modify addRestResourceClasses() method.                                                                           
     * It is automatically populated with                                                                                       
     * all resources defined in the project.                                                                                    
     * If required, comment out calling this method in getClasses().                                                            
     */
    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(eu.oca.ResultatGeneralAF.class);
        resources.add(eu.oca.ResultatGeneralF.class);
        resources.add(eu.oca.ResultatMesuresAF.class);
        resources.add(eu.oca.ResultatMesuresF.class);
        resources.add(eu.oca.SidonieAccueilD.class);
        resources.add(eu.oca.SidonieWelcomeR.class);
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜