JBoss Embedded AS | API Guide

    Introduction

    The Embedded APIs to JBoss Application Server provide a code-based analouge for tasks ordinarily accomplished through traditional command-line and filesystem operations.  This guide will provide a few of the most often-used examples.

     

    Examples

    Creating a Server (assuming you have all dependencies on your ClassPath) and pointing it to JBOSS_HOME

    JBossASEmbeddedServer server = JBossASEmbeddedServerFactory.createServer();
    server.getConfiguration().jbossHome("/path/to/jbossHome");
    

     

    Creating a Server (when you only have EmbeddedAS APIs on the ClassPath)

    // Get JBOSS_HOME
    final URL jbossHome = this.getJBossHome();
    
    // Get additional binaries which need CL visibility (ie. jboss-embedded-core,
    // which is placed under "target/deps" by the build).  These
    // binaries are not presently available under $JBOSS_HOME
    final URL[] additionalUrls = null; // Assume we have these
    
    // Make the new ClassLoader
    final ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
    final ClassLoader jbossHomeClassLoader = JBossHomeClassLoader.newInstance(jbossHome, additionalUrls
          .toArray(new URL[]{}), oldCl);
    
    // Make Server
    server = JBossASEmbeddedServerFactory.createServer(jbossHomeClassLoader);
    
    // Set TCCL
    Thread.currentThread().setContextClassLoader(jbossHomeClassLoader);
    
    try
    {
       // Start the Server
       server.start();
    
       /*
        * Run your tests
        */
    
       // Shutdown if started
       if (server != null && server.getState().equals(LifecycleState.STARTED))
       {
          // Shutdown
          log.info("Shutting down server: " + server);
          server.shutdown();
       }
    }
    finally
    {
       // Reset the TCCL 
       Thread.currentThread().setContextClassLoader(oldCl);
    }
    

     

    Configuring the Server Startup Properties (may only be done before the server has been initialized/started)

    server.getConfiguration().bindAddress("localhost").serverName("all").partitionName("PARTNAME").udpGroup("UDPGROUP");
    

     

    Starting/Stopping the Server

    server.start();
    // Blocks ...
    server.stop();

     

    Registering an Event Handler to be Fired Upon Server Startup

    server.registerEventHandler(LifecycleState.STARTED, new LifecycleEventHandler()
    {
       @Override
       public void handleEvent(final LifecycleState state) throws LifecycleEventException
       {
          System.out.println("I've been started!");
       }
    });
    

     

    Deploying Files or URLs

    File[] files = null; // Assume you've got a bunch
    URL[] urls = null; // Keep assuming
    server.deploy(files);
    server.deploy(urls);
    

     

    Deploying a ShrinkWrap Archive

    String name = "slsb.jar";
    JavaArchive archive = Archives.create(name, JavaArchive.class).addClasses(OutputBean.class,
          OutputLocalBusiness.class);
    log.info(archive.toString(true));
    // Deploy
    server.deploy(archive);
    

     

    Making Your Own Deployable Type

    // If you can represent it as a URL, you can deploy it in EmbeddedAS
    final Deployable deployable =new Deployable()
    {
       @Override
       public URL getTarget()
       {
          return someUrlIHave;
       }
     };
    server.deploy(deployable);