1

I'm trying to run a java web service on Glassfish. There is some initialization code that sets a few variables and retrieves some information from the Glassfish environment itself. I have that code in a static initializer inside the @WebService class, however this code appears to be called too early, it gets run as soon as the WebService endpoint is deployed, whereas I need it to run once the whole web service is successfully deployed.

I tried moving the code into the constructor of the WebService class, however then that code was only run when I went into the Tester web page and sent some data to make the web methods run.

Is there any way to set up some initialization code to be run as soon as the whole web service deployment is completed?

Garis M Suero
  • 7,974
  • 7
  • 45
  • 68
Jainathan Leung
  • 1,147
  • 2
  • 15
  • 27

2 Answers2

6

Option 1: In Glassfish you have the Lifecycle modules

Option 2: You also have the ability to code a ServletContextListener to be triggered when the context is loaded:

public class MyServlet implements ServletContextListener {

  public void contextInitialized(ServletContextEvent e) {
         // implementation code
  }

  public void contextDestroyed(ServletContextEvent e) {
         // implementation code
  }
}

Reference:

Garis M Suero
  • 7,974
  • 7
  • 45
  • 68
0

Yes @Jaynathan Leung, with soapUI can you deployment the web services and testing your programming with input and output data. I hope help you. :)

hekomobile
  • 1,388
  • 1
  • 14
  • 35
  • i'm sorry, I'm not trying to test the code, I'm trying to have code run on deployment, automatically. A static initializer did work when I was running the code from Netbeans, but when i deploy the .WAR on glassfish via ./asadmin deploy WebService.war, it seems to run the code too early. – Jainathan Leung May 15 '12 at 15:15
  • With soapUI can you **deploy WAR file** this serve for code run, I hope understand you. :). Try using soapUI and tell me if you want if will help you. – hekomobile May 15 '12 at 15:28