10

When developing an Java EE Application, I often came across the 'problem' to do stuff when the application is started, stopped etc. Now for Weblogic for example, there is a mechanism for that (the application life-cycle listener). But if you want to keep your application free from stuff that is app. server specific, you have to find a different solution. Some recommend using a servlet that is loaded on start-up, and "abuse" the init()/destroy().

Others say use a ServletContextListener. To me, the last one sounds best (according to the java doc for ServletContextListener. Unfortunately, today I tried JBoss 7, where it seems that jax-ws webservices are initialized before any other Servlet, thus before the ServletContextListener gets a notification.

Long story short - am I just facing some app server specific issues here - or is there any "more appropriate", standardized Java EE way to register things, do stuff, before any webservice, servlet, whatsoever is initialized?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
DXTR66
  • 563
  • 5
  • 17
  • To me it looks like an app server issue. I'm not an experienced JBoss user, but all app servers I've seen so far initialize the servlet context listener(s) first, then the servlets. Are your Web Services located in a different WAR file? – home Jul 22 '11 at 08:46
  • 3
    BTW: Sine JEE6 a Startup EJB annotation is available as well... in the case you use EJBs: http://download.oracle.com/javaee/6/api/javax/ejb/Startup.html – home Jul 22 '11 at 08:47
  • Nope, the servlet (which would do the startup process) and the webservices are in the same war. And I am also think that this might be a JB AS7 related issue, never the less I was interessted if there is maybe a better, standardized way, of handling startup stuff :). – DXTR66 Jul 22 '11 at 08:49
  • No, I think ServletContextListener is the right way to do it. I used it several times in the past and never found a better solution. – home Jul 22 '11 at 08:54
  • Not your JBoss version and GateIn related, but maybe there is a difference when it comes to hot deployment (e.g. in eclipse environment):http://community.jboss.org/thread/157804 – home Jul 22 '11 at 08:56

2 Answers2

1

If your webservices are annotated like this

@javax.jws.WebService(...)
public interface YourServiceEndpoint

they are no real servlets yet, but JBoss (Jax-WS) will turn them into a startup.

I am using jboss-4.2.3 and I am also getting these messages before my ServletContextListner is called.

[org.jboss.wsf.framework.management.DefaultEndpointRegistry] register: jboss.ws:context=crm,endpoint=YourService

But I wonder, if this webservice is available before the complete application has started because nearly at the end of deployment I get following messages

[org.jboss.wsf.stack.jbws.WSDLFilePublisher]  WSDL published to: ... YourServlet(..).wsdl

So I would guess, that this is a jboss related issue. Maybe we should test on another app server to proof so.

powerMicha
  • 2,753
  • 1
  • 24
  • 31
0

I did it with the following bean on WebLogic 14 with Java EE 8 API

@Singleton
@Startup
public class MyApplicationLifecycleListener {

    @PostConstruct
    public void init(){}

    @PreDestroy
    public void destroy(){}

}

I had the same problem as the OP with my runtime, and I was looking for decouple my code from vendor-specific libraries.

Following the comments, and with the help of javax:javaee-api which can be found on Maven I superseded WebLogic's ApplicationLifecycleListener thus removing the dependency from the POM and all the rest of the problems it takes along.

I would expect this to work on JBoss too.

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305