假設我有兩個 Java Service,一個為 Test Java Service: input 參數為 name,執行時會出現 popup window 供輸入
另外一個為 HelloWorld,input parameters 有兩個,分別是 name 與 dateTime,由Test Java Service 傳送給 HellowWorld Java Service,welcomeMessage = name + "(" + dateTime + ")"
How-to
可以使用 Service.doInvoke 來呼叫其他 Service
以下是 Test Service 中的 code snippet:
public final class Test_SVC { /** * The primary method for the Java service * * @param pipeline * The IData pipeline * @throws ServiceException */ public static final void Test(IData pipeline) throws ServiceException { IDataCursor pipelineCursor = pipeline.getCursor(); // Invoke the pub.date:getCurrentDateString service to get current date time String currentDateString = ""; IDataUtil.put(pipelineCursor, "pattern", "yyyy/MM/dd HH:mm:ss"); try { IData data = Service.doInvoke("pub.date", "getCurrentDateString", pipeline); currentDateString = (String) IDataUtil.get(data.getCursor(), "value"); } catch (Exception e) { throw new ServiceException(e); } // input name String name = IDataUtil.getString(pipelineCursor, "name"); IDataUtil.put(pipelineCursor, "name", name); // get current date time via pub.date:getCurrentDateString service IDataUtil.put(pipelineCursor, "dateTime", currentDateString); // Invoke HelloWorld service try { Service.doInvoke("acme.albert.work", "HelloWorld", pipeline); } catch (Exception e) { throw new ServiceException(e); } pipelineCursor.destroy(); } }
以下是 HelloWorld Service 中的 code snippet:
public final class HelloWorld_SVC { /** * The primary method for the Java service * * @param pipeline * The IData pipeline * @throws ServiceException */ public static final void HelloWorld(IData pipeline) throws ServiceException { // pipeline IDataCursor pipelineCursor = pipeline.getCursor(); String name = IDataUtil.getString(pipelineCursor, "name"); String dateTime = IDataUtil.getString(pipelineCursor, "dateTime"); logger("dateTime = " + dateTime); pipelineCursor.destroy(); // pipeline IDataCursor pipelineCursor_1 = pipeline.getCursor(); String welcomeMsg = "Hello~" + name + " (" + dateTime + ")"; logger("welcome msg = " + welcomeMsg); IDataUtil.put( pipelineCursor_1, "welcomeMessage", welcomeMsg ); pipelineCursor_1.destroy(); } // --- <<IS-BEGIN-SHARED-SOURCE-AREA>> --- public static void logger(String message) throws ServiceException { IData input = IDataFactory.create(); IDataCursor inputCursor = input.getCursor(); IDataUtil.put(inputCursor, "message", message); IDataUtil.put(inputCursor, "function", "customLogger"); IDataUtil.put(inputCursor, "level", "INFO"); inputCursor.destroy(); try { Service.doInvoke("pub.flow", "debugLog", input); } catch (Exception e) { throw new ServiceException(e); } } // --- <<IS-END-SHARED-SOURCE-AREA>> --- }
執行結果:
[9987]2016-11-16 15:52:31 TST [ISP.0090.0004I] customLogger -- welcome msg = Hello~Albert (2016/11/16 15:52:31) [9986]2016-11-16 15:52:31 TST [ISP.0090.0004I] customLogger -- dateTime = 2016/11/16 15:52:31
上述的範例是採取 synchronous 的方式去呼叫另外一個 Java Service,若要採用 asynchronous 的方式呼叫,需要改寫如下:
Session session = Service.getSession(); IData input = IDataFactory.create(); IDataCursor inputCursor = input.getCursor(); IDataUtil.put(inputCursor, "input", "hello"); inputCursor.destroy(); Service.doThreadInvoke("acme.albert.work.OPC", "OPC_Heartbeat_Flow", session, input);
No comments:
Post a Comment