Sample code for the acs-callbacks support package
Class DemoRequester wants to invoke method revertString(), which demands a String, a CBstring, and a DescIn as its arguments. The latter two can/should be created using the RequesterUtil class. For CBstring to be created, a ResponseReceiver is necessary. A simple but in many cases sufficient example of an ResponseReceiver instance is shown below. When the Responder implementation calls the client back, the specified ResponseReceiver will be executed.
import java.util.logging.Logger; import alma.acs.callbacks.RequesterUtil; import alma.acs.callbacks.ResponseReceiver; import alma.acs.commandcenter.meta.Firestarter; import alma.acs.container.ContainerServicesBase; import alma.mysubsystem.Responder; public class DemoRequester { void go () throws Exception { // --- connect to component Firestarter.configure(null, "te1.hq.eso.org", "3500"); Firestarter.prepare(); ContainerServicesBase cs = Firestarter.giveContainerServices(); Logger logger = cs.getLogger(); Responder responder = ResponderHelper.narrow(cs.getComponent("RESPONDER")); // --- activate callback object ResponseReceiver rere = new ResponseReceiver() { public void incomingResponse(Object x) { System.out.println("Incoming Response: "+x); } public void incomingException(Exception x) { System.out.println("Responding failed: "+x);} }; responder.revertString( "Hallo", RequesterUtil.giveCBString(cs, rere), RequesterUtil.giveDescIn() ); // --- do something else until response comes in System.out.println("--> press enter to exit <--"); while (System.in.read() != 13) ; // --- clean up and quit Firestarter.godown(); } }
Class EasyResponderImpl implements the revertString() method: after calculating the return value, it uses ResponderUtil to call the client back.
import alma.ACS.CBDescIn; import alma.ACS.CBstring; import alma.acs.component.ComponentImplBase; import alma.acs.callbacks.ResponderUtil; import alma.mysubsystem.ResponderOperations; public class EasyResponderImpl extends ComponentImplBase implements ResponderOperations { public void revertString (String text, CBstring cb, CBDescIn descIn) { try { // --- calculate returnvalue int length = text.length(); char[] res = new char[length]; for (int i=0; i<length; res[i] = text.charAt(length-1-i++)); String returnValue = new String(res); // --- invoke callback ResponderUtil.respond (returnValue, cb, descIn); } catch (Exception exc) { ResponderUtil.respond (exc, cb, descIn); } } }