How to call a Java method while deploying a WAR [duplicate]
Asked Answered
V

4

0

How to call a Java method while deploying a WAR

Versicular answered 16/5, 2011 at 10:38 Comment(5)
Please clarify what you mean by "while deploying"? Do you want some code to run as soon as your webapp is launched?Ceil
a java method from a class within the WAR file being deployed? why?Farouche
Do you want to call some method on server startup?Disobedience
Yes, I want some code to run as soon as my web app is launchedVersicular
@William: then the answers you got provides good options. Just try them.Disobedience
I
4

It's not clear what you mean by "deploy". Is that the moment when the WAR file arrives on the app server? Maybe you gin something up with Ant. Is that when the app starts up? Maybe you can do it with a ServletContextListener.

There's no mechanism for doing so built into any Java EE app server I know of, so you're out of luck if Ant isn't suitable. You need something that does the deploying to do it for you.

What's your purpose for doing so? What does this method do?

Intestinal answered 16/5, 2011 at 10:39 Comment(5)
It's not clear == not a real questionApostolate
I mean when the WAR file arrives on the app server. This method saves some configuration data in the database, and I want this to happen when I deploy the WAR on the server. I don't want to do it with ant, because it shouldn't happen while building.Versicular
I don't see any reason of down vote here. +1Parietal
See my update. And there's no reason for a down vote here, unless you think that the only appropriate response is "not a real question." I think clarification isn't that far away.Intestinal
I didn't give you a down vote, I can't even vote :PVersicular
D
4

Multiple solutions are available to you:

  • A ServletContextListener can be used to listen to startup events and execute appropriate actions.
  • The same thing can be done using a Servlet, with the load-on-startup attribute. You don't have to map your servlet to a servlet-path and can use it only for startup actions.
  • If you are using Java EE 6, you can use an EJB anotated with @Startup, which instanciate the service during startup. A @PostConstruct annotation declares a method to execute after instantiation. Note this works only with EJB singletons.
Diphyodont answered 16/5, 2011 at 10:44 Comment(1)
But none of that is at deployment time; all happen at runtime.Intestinal
P
3

You can use ServletContextListener

Parietal answered 16/5, 2011 at 10:40 Comment(0)
C
1

If you want to put this functionality in you web app then right place is ServletContextListener

Implementations of the ServletContextListener interface receive notifications about changes to the servlet context of the Web application of which they are part. The following methods are defined in the ServletContextListener :

public void contextInitialized(ServletContextEvent sce)
public void contextDestroyed(ServletContextEvent sce)

The contextInitialized() method is invoked when the Web application is ready for service and the contextDestroyed() method is called when it is about to shut down. The following code shows how we can use these methods to log the application events:

public void contextInitialized(ServletContextEvent e) {
    e.getServletContext().log("Context initialized");
}

public void contextDestroyed(ServletContextEvent e) {
    e.getServletContext().log("Context destroyed");
}

see example, or you can extend you container/server to monitor, as same as monitor tools.

Charest answered 16/5, 2011 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.