-1

I know that using ServletContextListener I can be aware about servlet context startup and shut down. But I am wondering how to listen to those events if my application does not uses the servelt context. Are there alternatives to be informed about wildfly container life cycle? Or am I forced to depends on jboss-servlet-api?

If my question is a "non-problem" please explain why.

Marco Stramezzi
  • 2,143
  • 4
  • 17
  • 37
  • 1
    *"if my application does not uses the servelt context"* huh? *"Or am I forced to depends on jboss-servlet-api?"* huh?? All due respect, this all indicates that you've some fundamental misunderstandings. As least, perhaps this is helpful? http://stackoverflow.com/questions/3468150/using-special-auto-start-servlet-to-initialize-on-startup-and-share-application – BalusC Mar 14 '16 at 22:02

2 Answers2

1

It's not quite clear whether you're interested in the servlet context lifecycle or the Java EE application lifecycle in general. It doesn't make sense to deal with servlet contexts without depending on the servlet API.

However, if you're really talking about the application lifecycle, you can use a CDI observer:

@Dependent
public class InitListener {

    public void onInit(@Observes @Initialized(ApplicationScoped.class) Object event) {
        System.out.println("*** init");
    }
}
Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63
  • Yes I am talking about appliaction lifecycle... So the observer listen for the initialization of the annotation or of a class annotated `@ApplicationScoped`? – Marco Stramezzi Mar 15 '16 at 08:08
0

If you are using EJB. Then you can use

@Startup
@Singleton
public class Init{
   @PostConstruct
   public void initialize(){
   }

  @PreDestroy
  public void destroy(){
  }
}

It will be called when the EE container is initialized

Shettyh
  • 1,188
  • 14
  • 27