SourceForge.net Logo

create services

This page explains how to create a BERSERK service.

service's class

 
After you have in mind what your service will do, you should create a Java class that will implement the idealized logic. The created class should implement BERSERK's IService interface:
public MyService implements IService
{
	//my methods here
}									
									
Obviously the previous services does absolutelly nothing and its completelly useless.
To create some usefull service, the service's method must have at least one method. By the release of BERSERK V1.0 the called method is the run() method. However, the code is ready for extension and soon the services definitions will include the method name.
 
For now lets add a run method to our new service. Suppose that we want the service to multiply two numbers that are it's parameters:
public class MyService implements IService
{
	Integer run(Integer a, Integer b)
	{
		return new Integer(a.intValue()*b.intValue());
	}
}									
									
By this time, this look like an ordinary class with one method. As a matter of fact, it is.
Our service is done and ready to be configured in BERSERK framework.
 

service's configuration

 
The differences between using OJB and XML storage are explained in the Tutorials section of this website. Thus, in this page we will use XML storage and make no reference to how things would be if we were using OJB.
 
We will also associate a filter chain with this service. The filter chain creation is described in the "how to create filter chains" page.
 
The service configuration is very intuitive. A XML file must be created and copied to the client application's classpath. The default name is sd.xml. In fact, any filename can be used if it is correctly configured in the berserk.properties file.
 
The file should look like this:
<serviceDefinitions>
		<service>
		<name>Multiplicator</name> 
		<implementationClass>Multiplier</implementationClass> 
		<description>Multiplies two numbers returning the	resulting value</description>
		<isTransactional>false</isTransactional> <filterChains>
			<chain id="1"/>
		</filterChains>
	</service>
</serviceDefinitions>
 
This service assumes the class is in the application's default package.
 
The name and the implementationClass fields are different to remind that there is no relation between them: the name is the string we must provide to the service invocator, and the implementing class is the full qualified name to the class where the service's logic resides.
 
As stated before we had associated the chain with the id=1. This chain is defined in the "how to create filter chains" page.
 

calling the service

 
To call our new service, we need a caller method. To keep this example as simple as possible, we will directly call the service from the main class:
public class HowTo
{ 
	public static void main() throws Exception
	{ 

		IServiceManager manager = ServiceManager.getInstance();
		Object[] args =	{new Integer (2), new Integer(4)};
		Integer result = manager.execute("Guest","Multiplicator",args);
	}
}

 
We are not worried about the exceptions, so we declare that main throws an Exception.
 
The requester is also irrelevant to this case, and we can use any object, as a string for instance.
 
The arguments are a vector containing the numbers we want to multiply.
 
The result variable will hold the result of the arguments product.