This chapter is intended for advanced JOnAS users who require that some "external" services run along with the EJB 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 EJB Server. The JOnAS pre-defined services are listed in Section 3.5 Configuring JOnAS Services.
EJB 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 EJB 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 EJB server.
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.
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 24.1.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; } } |
The service is defined and its initialization parameters specified in the jonas.properties file. First, choose a name for the service (for example, "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 |
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); } |