Skip to main content

Embedded GlassFish : Programatically running asadmin commands from a servlet.

Posted by bhavanishankar on September 11, 2011 at 10:03 PM PDT

Recently there has been lot of requests in GlassFish community asking
for programmatic interface to run asadmin commands from within a
servlet. This is very easy when the servlet is deployed on Embedded

When you embed GlassFish, you just need to bind the embeddable command
runner in JNDI, like this:

import org.glassfish.embeddable.*;
import javax.naming.InitialContext;

public class GlassFishEmbedded {

public static void main(String... args) {
   GlassFishProperties props = new GlassFishProperties();
   props.setPort("http-listener", 8080);
   GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(props);

   // Bind the command runner in JNDI tree with your own mapped-name
   CommandRunner commandRunner = glassfish.getCommandRunner();
   new InitialContext().bind("org.glassfish.embeddable.CommandRunner", commandRunner);

   // Deploy your web-app containg RunAdminCommandsServlet using glassfish.getDeployer().deploy(...) method

Thats it, now from your RunAdminCommandsServlet you can inject the
command runner like this:

import org.glassfish.embeddable.*;
import javax.annotation.Resource;

@WebServlet(name = "RunAdminCommandsServlet",
urlPatterns = "/RunAdminCommandsServlet")
public class RunAdminCommandsServlet extends HttpServlet {

@Resource(mappedName = "org.glassfish.embeddable.CommandRunner")
CommandRunner commandRunner;

protected void doGet(HttpServletRequest httpServletRequest,
   HttpServletResponse httpServletResponse) throws ServletException, IOException {
     if (commandRunner != null) {
       CommandResult result ="create-jdbc-connection-pool",

Related Topics >>


Thank you very much for this explanation.

Thank you very much for this explanation.