Java EE Enterprise Application: perform some actio

2019-01-09 10:33发布

I would like to perform some action as soon as my application (Enterprise Application with Business Logic, EJB, and a Client, Web) is deployed. For example I would like to make some entity in a persistent state, or otherwise create a file. How can I do that?

Thanks.

3条回答
仙女界的扛把子
2楼-- · 2019-01-09 10:58

The "default" way is to have a servlet with an init() method. Then in the servlet-descriptor you mark this servlet as load-on-startup 1:

Example:

<servlet-name>Seam Resource Servlet</servlet-name>
   <servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>

As soon as the servlet is deployed (which happens after the EJBs are deployed), that init() method is called and you can execute the task you want.

查看更多
\"骚年 ilove
3楼-- · 2019-01-09 11:00

With present web application in your ear, the easiest and simplest would be to use ServletContextListener, otherwise in EJB 3.1 you could use automatic timers or startup singleton session beans.

查看更多
beautiful°
4楼-- · 2019-01-09 11:05

Configure SerlvetContextListener and override contextInitilized()

in your web application description , web.xml

<web-app ...>
    <listener>
        <listener-class>com.someCompany.AppNameServletContextListener</listener-class>
    </listener>
</web-app

package com.someCompany;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class AppNameServletContextListener implements ServletContextListener{

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("ServletContextListener destroyed");
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("ServletContextListener started");   
                // do the things here 
    }
}
查看更多
登录 后发表回答