I componenti con i moduli possono utilizzare i componenti su qualunque nodo di un cluster WebSphere Process Server.
ServiceManager serviceManager = new ServiceManager();
InputStream myReferences = new FileInputStream(“MyReferences.references”); ServiceManager serviceManager = new ServiceManager(myReferences);
StockQuote stockQuote = (StockQuote)serviceManager.locateService(“stockQuote");
Service stockQuote = (Service)serviceManager.locateService(“stockQuote");
public class MyValueImpl implements MyValue { public float myValue throws MyValueException { ServiceManager serviceManager = new ServiceManager(); // variables Customer customer = null; float quote = 0; float value = 0; // invoke CustomerInfo cInfo = (CustomerInfo)serviceManager.locateService("customerInfo"); customer = cInfo.getCustomerInfo(customerID); if (customer.getErrorMsg().equals("")) { // invoke StockQuoteAsync sQuote = (StockQuoteAsync)serviceManager.locateService("stockQuote"); Ticket ticket = sQuote.getQuoteAsync(customer.getSymbol()); // … do something else … quote = sQuote.getQuoteResponse(ticket, Service.WAIT); // assign value = quote * customer.getNumShares(); } else { // throw throw new MyValueException(customer.getErrorMsg()); } // reply return value; } }
Quando un modulo richiama un componente che dispone di un'interfaccia del tipo d porta WSDL (Web Service Descriptor Language), il modulo deve richiamare il componente in modo dinamico utilizzando il metodo invoke().
Service service = (Service)serviceManager.locateService("multiParamInf"); Reference reference = service.getReference(); OperationType methodMultiType = reference.getOperationType("methodWithMultiParameter"); Type t = methodMultiType.getInputType(); BOFactory boFactory = (BOFactory)serviceManager.locateService ("com/ibm/websphere/bo/BOFactory"); DataObject paramObject = boFactory.createbyType(t); paramObject.set(0,"input1") paramObject.set(1,"input2") paramObject.set(2,"input3") service.invoke("methodMultiParamater",paramObject);
L'esempio di seguito riportato utilizza il metodo invoke con un'interfaccia del tipo di porta WSDL come destinazione.
Service serviceOne = (Service)serviceManager.locateService("multiParamInfWSDL"); DataObject dob = factory.create("http://MultiCallWSServerOne/bos", "SameBO"); dob.setString("attribute1", stringArg); DataObject wrapBo = factory.createByElement ("http://MultiCallWSServerOne/wsdl/ServerOneInf", "methodOne"); wrapBo.set("input1", dob); //wrapBo encapsulates all the parameters of methodOne wrapBo.set("input2", "XXXX"); wrapBo.set("input3", "yyyy"); DataObject resBo= (DataObject)serviceOne.invoke("methodOne", wrapBo);