Is there a way to detect when a war file is successfully loaded by Wildfly and cause some code to execute?
Asked
Active
Viewed 491 times
2 Answers
4
You have a few options.
If you're leveraging CDI, you can add an observer method for @Observes @Initialized(ApplicationScoped.class) Object o
If you're leveraging EJBs, you can have a @javax.ejb.Singleton @javax.ejb.Startup
with a @PostConstruct
method that does initialization. Here are two example implementations.
// using a CDI object
@ApplicationScoped
public class SomeStartupBean {
public void initOnStartup(@Observes @Initialized(ApplicationScoped.class) Object obj) {
// do your start up logic here
}
}
or
// using an EJB
@Singleton
@Startup
public class SomeStartupSingleton {
@PostConstruct
public void initOnStartup() {
// do your start up logic here
}
}

John Ament
- 11,595
- 1
- 36
- 45
-
In this case, I have a RESTeasy servlet. I'd like to trigger some logic to dump the current environment to the log file when the servlet is available. I'm looking for something like Spring's ApplicationListener for a ContextRefreshedEvent. Ideally I could make this a standalone class that can be included in any war file. – Patrick May 01 '15 at 13:48
-
I'm not trying to be obtuse, but I'm not sure exactly how to accomplish this. What I'd like to do is create a class EnvironmentLogger that can simply be included in the war file. Where do the @Observes @Initialized(ServletName) annotations go in such a class? Thanks again. – Patrick May 01 '15 at 17:18
-
I updated my answer with concrete implementations you can try. – John Ament May 01 '15 at 20:29
-
Its also worth noting that you can still use things like servlet context listeners. Nothing wrong with them, however when working with rest api's that would be your only dependency on a web tier. This gives slightly better isolation. If you're dealing with a webapp, the `Object` in the initialize is a servlet context. – John Ament May 01 '15 at 20:57
-
@JohnAment Would it be safe to use `@PostConstruct` on the `@ApplicationScoped` bean method `initOnStartup` on top of `@Observes @Initialized`? The reason I'm asking: I have two such CDI beans with that eager startup technique where one is injected into the other. This leads to a race condition because sometimes the injected bean has not been created/initialized by the container yet when the other bean tries to call a method on it. – Hein Blöd Jul 01 '16 at 12:47
-
If placing `@PostConstruct` additionally eliminates the race condition _and_ placing that annotation on the `initOnStartup` method guarantees a single execution (as the initialization logic should only run once), I'd be fine. Otherwise, I'd need a flag indicating successful initialization and have an extra method annotated with `@PostConstruct`. – Hein Blöd Jul 01 '16 at 12:50
-
1No, that would be bad. I would recommend using two different methods. `@PostConstruct` specifically should not take parameters. – John Ament Jul 01 '16 at 13:15
-
Thanks for your quick reply - actually I was just going to report my findings after doing some test deploys: In fact, placing `@PostConstruct` on `initOnStartup` doesn't prevent the race condition. Using some debug output, I found that `initOnStartup` is sometimes _still not_ called before the method used by my other bean. It looks as if the two (sets of) annotations are in conflict and `@PostConstruct` loses out. This is on GF 4.1, for reference. Or it's because of the parameter, as you said. So I will follow your recommendation and use two different methods. – Hein Blöd Jul 01 '16 at 13:20
-
`@PostConstruct` will always be called before any bean methods. If you're not seeing that, sounds like a bug in GlassFish. But honestly, you may need to look at Payara for bug fixes. – John Ament Jul 01 '16 at 13:29
-
Yes - we will move on to Payara for sure. I've kept postponing that "just until the next release is done" for some time already, but now it's near the top of my todo list. ;) – Hein Blöd Jul 01 '16 at 13:52
0
You could use an @Startup
EJB. That would execute when the application has successfully been deployed.

James R. Perkins
- 16,800
- 44
- 60