The toolkit provides a super class BTTService as an interface that defines the major methods of the service objects. The toolkit also provides the BTTServiceImpl abstract class to provide the basic implementation of the major methods. You can create your own service objects based on these two classes to address your business needs.
For example, suppose you already have a class called MyClass. This class adds two integers and throws a Java(TM) event if the result is greater than some predefined value. The signature of the method that performs the addition is int addition(int,int). The predefined value is set through the method setMyMaximum(int), and the Java event MaximumValueExceeded is thrown when the result of the addition is greater than the value set in setMyMaximum(int). Any class willing to receive this event implements the interface MaximumValueExceededListener, which defines the method handleMaximumValueExceededEvent(MaximumValueExceeded event).
The following example describes how to access the MyClass methods from an application using a service:
In this example, assume that there is an instance variable myInst of type MyClass:
public Object initializeFrom(com.ibm.dse.base.Tag aTag) { myInst = new MyClass(); myInst.addMaximumValueExceededListener(this); for (int i=0;i<aTag.getAttrList().size();i++) { TagAttribute attribute = (TagAttribute)aTag.getAttrList().elementAt(i); if (attribute.getName().equals("maxValue")) { myInst.setMyMaximum(Integer.parseInt((String)(attribute.getValue()))); } } return this; }
public String toString() { String s="<"+getTagName()+" "; s=JavaExtensions.addAttribute(s,"maxValue", String.valueOf(getMaxValue())); s=s+">"; return s; }
public void handleMaximumValueExceededEvent(MaximumValueExceeded event) { try { signalEvent(new ValueExceeded((String)event.getSource(), this)); }catch(DSEInvalidArgumentException e){} }
public int add(int a, int b) { return myInst.addition(a,b); }
Now, you have a service that deals with MyClass. Note that MyClass is unchanged. Before using it from an application, you must update the configuration file and the XML services and contexts files.
<field id=myService value=com.ibm.dse.service.sample.MyService>
If the service definition needed subtags (additional tags inside the service tag), the line above would also contain the attribute description=compound. In this example, however, there are no subtags.
<myService id=newService maxValue=10>
<refService refId=newService alias=serv type=srv>
Now, you can use the service from an operation as in the following example (not from the sample application):
public void execute() throws Exception { try { handleEvent("allEvents","serv",getContext()); ((MyService)getService("serv")).add(int1,int2); stopHandlingEvent("allEvents","serv",getContext()); } catch (Exception e) { stopHandlingEvent("allEvents","serv",getContext()); throw e; } }
You will get the ValueExceeded event through the Handler dispatchEvent(DSEEventObject anEvent) method of your operation.