Coding services

First, generate your service bean model, best using a design and code generation framework. A XSD example from org.resoa.testing:

Testmodel

Compiling this schema by JAXB generates your object model. Main type is org.resoa.test.xsd.Student, linking to org.resoa.test.xsd.Course, etc.

Next start coding services for the type Student:


package org.resoa.test.xsd.service;
public class StudentService {
	/**
	* Store a student
	*/
	public void store (Student s, ResoaResponse response, ResoaGateway gateway){
		try {
			gateway.getPersistence().store(s);
			Message msg = new Message();
			msg.setMessage("Student " + s.getId() + " stored on node " + gateway.getGatewayID());
			msg.setId("OK");
			response.setValue(msg, ResponseAction.SUCCESS);
		} catch (Exception e) {
			gateway.getLogger().error("Error during Student#store: " + e.getMessage());
		}
	}

	/**
	* Read a student
	*/
	public void read (Student s, ResoaResponse response, ResoaGateway gateway){
		try {
			Student existing = gateway.getPersistence().read(Student.class, s.getId());
			if (existing != null)
				response.setValue(existing, ResponseAction.SUCCESS);
			else{
				Message msg = new Message();
				msg.setMessage("Student " + s.getId() + " does not exist on node " + gateway.getGatewayID());
				msg.setId("NOTEXIST");
				response.setValue(msg, ResponseAction.SUCCESS);
			}
		} catch (Exception e) {
			gateway.getLogger().error("Error during Student#get: " + e.getMessage());
		}
	}
}

Characteristic of Resoa Services

  • The package name of service implementation classes extend the package name from the service object model by .service.
  • The classname of a single service object implementation extends the service type by ‘Service‘. I.E. implementing services for the type org.resoa.test.xsd.Student means creating a class named org.resoa.text.xsd.service.StudentService
  • A service for a type is a single method. Required arguments of a service method: First parameter is the service object, second the response handle, third (optional) the ResoaGateway instance.
  • If you set a response object, the gateway will parse this to a HTTPResponse in JSON format.

There are no more requirements, the ResoaServiceDomainExplorer will identify classes following this convention  as a service implementation class.

Initialization, Local and Cron Services

ResoaGateway will look for a method init(ResoaGateway gateway) during deployment. Add your service specific initialization code here.

There exist two interfaces for providing additional local container functionality:

org.resoa.node.ResoaGateway.LocalService

You might define LocalServices for central tasks like mail services etc. Local service do not have the fixed argument structure, you get a handle by resoaGateway.getLocalService(String serviceName).

org.resoa.node.ResoaGateway.CronService

CronServices are periodically invoked by the ResoaGateway by comparing the nextExecutionTime with the current system time.