The API for embedded GlassFish Server provides a method for stopping an embedded server. Using this method enables your application to stop the server in an orderly fashion by performing any necessary cleanup steps before stopping the server, for example:
Undeploying deployed applications
Releasing any resources that your application uses
This example shows code for prompting the user to press the Enter key to stop an embedded GlassFish Server. When a user presses Enter, the application undeploys any deployed applications before stopping the server. For more information about undeploying applications, see Undeploying an Application. Code for creating a Server object is not shown in this example. For an example of code for creating a Server object, see Example 1.
... import java.io.BufferedReader; ... import org.glassfish.api.embedded.*; ... EmbeddedDeployer deployer = server.getDeployer(); ... System.out.println("Press Enter to stop server"); // wait for Enter new BufferedReader(new java.io.InputStreamReader(System.in)).readLine(); deployer.undeployAll(); server.stop(); ...