Programmatically stop Java EE application during initializaton
Asked Answered
P

1

5

Is there any Java EE standard (application server cross-compatible) way how to stop the Java EE application during initialization i.e. during running of @PostConstruct anotated method of @Singleton @Startup class?

@Singleton
@Startup
public class Initializer {

    @PostConstruct
    public void checkConfiguration() {
        // stop application here
    }
}

I search for soft way to stop just the application, to whole application server, nothing like System#exit.

Pass answered 19/3, 2015 at 10:57 Comment(2)
Why don't you just throw an IllegalArgumentException from checkConfiguration? That should stop your deployment in it's tracks.Entrepreneur
Yes, it works. Actually any RuntimeException works. Would you like to post it as a regular answer so I can accept it?Pass
E
9

If you throw any kind of RuntimeException (such as IllegalArgumentException for configuration errors) from an @PostConstruct annotated method in an @Startup annotated @Singleton then the entire application will fail deployment.

From §4.8.1 "Singleton Session Bean Initialization" of the EJB 3.2 specification:

If the Startup annotation appears on the singleton session bean class or if the singleton session bean has been designated via the deployment descriptor as requiring eager initialization, the container must initialize the singleton session bean instance during the application startup sequence. The container must initialize all such startup-time singleton session beans before any external client requests (that is, client requests originating outside of the application) are delivered to any enterprise bean components in the application.

This can't be satisfied if initialisation fails.

Entrepreneur answered 24/3, 2015 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.