Creating a New JOnAS Service

1.1. Target Audience and Rationale
1.2. Introducing a new Service
1.2.1. Defining the Service class
1.2.2. Modifying the jonas.properties file
1.2.3. Using the New Service
1.2.4. Adding the class of the new service to JOnAS
1.3. Advanced Understanding
1.3.1. JOnAS built-in services
1.3.2. The ServiceException
1.3.3. The ServiceManager

1.1. Target Audience and Rationale

This chapter is intended for advanced JOnAS users who require that some "external" services run along with the JOnAS server. A service is something that may be initialized, started, and stopped. JOnAS itself already defines a set of services, some of which are cornerstones of the JONAS Server. The JOnAS pre-defined services are listed in Configuring JOnAS services .

J2EE application developers may need to access other services, for example another Web container or a Versant container, for their components. Thus, it is important that such services be able to run along with the application server. To achieve this, it is possible to define them as JOnAS services.

This chapter describes how to define a new JOnAS service and how to specify which service should be started with the JOnAS server.

1.2. Introducing a new Service

The customary way to define a new JOnAS service is to encapsulate it in a class whose interface is known by JOnAS. More precisely, such a class provides a way to initialize, start, and stop the service. Then, the jonas.properties file must be modified to make JOnAS aware of this service.

1.2.1. Defining the Service class

A JOnAS service is represented by a class that implements the interface org.objectweb.jonas.service.Service , and, thus should implement the following methods:

  • public void init(Context ctx) throws ServiceException;

  • public void start() throws ServiceException;

  • public void stop() throws ServiceException;

  • public boolean isStarted();

  • public String getName();

  • public void setName(String name);

It should also define a public constructor with no argument.

These methods will be called by JOnAS for initializing, starting, and stopping the service. Configuration parameters are provided to the initialization method through a naming context. This naming context is built from properties defined in the jonas.properties file as explained in Section 1.2.2, “Modifying the jonas.properties file”.

The Service class should look like the following:

package a.b;
import javax.naming.Context;
import javax.naming.NamingException;
import org.objectweb.jonas.service.Service;
import org.objectweb.jonas.service.ServiceException;
.....
public class MyService implements Service {
    private String name = null;
    private boolean started = false;
    .....    
public void init(Context ctx) throws ServiceException {
        try {
            String p1 = (String) ctx.lookup("jonas.service.serv1.p1");
            .....
        } catch (NamingException e) {
            throw new ServiceException("....", e);
        }
        .....
    }
    public void start() throws ServiceException {
        .....
        this.started = true;
    }
    public void stop() throws ServiceException {
        if (this.started) {
            this.started = false;
            .....
        }
    }
    public boolean isStarted() {
        return this.started;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;    }
}
      

1.2.2. Modifying the jonas.properties file

The service is defined and its initialization parameters specified in the jonas.properties file. First, choose a name for the service (e.g. "serv1"), then do the following:

  • add this name to the jonas.services property; this property defines the set of services (comma-separated) that will be started with JOnAS, in the order of this list .

  • add a jonas.service.serv1.class property specifying the service class.

  • add as many jonas.service.serv1.XXX properties specifying the service initialization parameters, as will be made available to the service class via the Context argument of the init method.

This is illustrated as follows:

jonas.services                   .......,serv1  
jonas.service.serv1.class        a.b.MyService  
jonas.service.serv1.p1           value      

1.2.3. Using the New Service

The new service has been given a name in jonas.properties . With this name, it is possible to get a reference on the service implementation class by using the ServiceManager method: getService(name) . The following is an example of accessing a Service:

import org.objectweb.jonas.service.ServiceException;
import org.objectweb.jonas.service.ServiceManager;
    MyService sv = null;        // Get a reference on MyService.
        try {
            sv = (MyService) ServiceManager.getInstance().getService("serv1");
        } catch (ServiceException e) {
            Trace.errln("Cannot find MyService:"+e);
        }      

1.2.4. Adding the class of the new service to JOnAS

Package the class of the service into a .jar file and add the jar in the JONAS_ROOT/lib/ext directory. All the libraries required by the service can also be placed in this directory.

1.3. Advanced Understanding

Refer to the JOnAS sources for more details about the classes mentioned in this section.

1.3.1. JOnAS built-in services

The existing JOnAS services are the following:

Service name Service class
registry RegistryServiceImpl
ejb EJBServiceImpl
web CatalinaJWebContainerServiceImpl/JettyJWebContainerServiceImpl
ear EarServiceImpl
dbm DataBaseServiceImpl
jms JmsServiceImpl
jmx JmxServiceImpl
jtm TransactionServiceImpl
mail MailServiceImpl
resource ResourceServiceImpl
security JonasSecurityServiceImpl
ws AxisWSService

If all of these services are required, they will be launched in the following order: registry, jmx, security, jtm, dbm, mail, jms, resource, ejb, ws, web, ear, jmx, security, dbm, mail.

registry must be launched first. (Note that for reasons of compatability with previous versions of JOnAS, if registry is unintentionally not set as the first service to launch, JOnAS will automatically launch the registry service.)

Note that dbm , jms , resource , and ejb depend on jtm . Note that ear depends on ejb and web (that provide the ejb and web containers), thus these services must be launched before the ear service. Note that ear and web depends on ws, thus the ws service must be launched before the ear and web services. It is possible to launch a stand-alone Transaction Manager with only the registry and jtm services.

A jonas.properties file looks like the following:

...
jonas.services    registry,jmx,jtm,db,dbm,security,wm,wc,resource,ejb,ejb3,ws,web,ear,depmonitor
jonas.service.registry.class org.ow2.jonas.registry.carol.CarolRegistryService
jonas.service.registry.mode    collocated
jonas.service.jmx.class                         org.ow2.jonas.jmx.internal.JOnASJMXService
jonas.service.jmx.secured                       false
jonas.service.jmx.authentication.method         jmx.remote.x.password.file
jonas.service.jmx.authentication.parameter      conf/jmx.passwords
jonas.service.jmx.authorization.method          jmx.remote.x.access.file
jonas.service.jmx.authorization.parameter       conf/jmx.access
jonas.service.wc.class    org.ow2.jonas.workcleaner.internal.JOnASWorkCleanerService
jonas.service.wc.period   300
...

1.3.2. The ServiceException

The org.objectweb.jonas.service.ServiceException exception is defined for Services. Its type is java.lang.RuntimeException. and it can encapsulate any java.lang.Throwable .

1.3.3. The ServiceManager

The org.objectweb.jonas.service.ServiceManager class is responsible for creating, initializing, and launching the services. It can also return a service from its name and list all the services.