Total Pageviews

2017/05/03

[webMethods] 如何將 JSON String 轉換成 ESB 中的 Document 物件

我有一個 Service Class,可以透過 buildTestJson() 取得測試用的 JSON String,或者是呼叫 buildHeartbeatMsgJson() 並傳入一個 HBMessage 物件取得 JSON String
import java.io.Serializable;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OpcHeartbeatService {
    
    private final static Logger logger = LoggerFactory.getLogger(OpcHeartbeatService.class);
    
    public static void main(String[] args) {
        OpcHeartbeatService test = new OpcHeartbeatService();
        logger.info("buildTestJson = " + test.buildTestJson());
    }
    
    public String buildTestJson() {
        HBMessage hb_msg = new HBMessage("test", "123445", "HBT_MSG");
        return buildHeartbeatMsgJson(hb_msg);
    }
    
    public String buildHeartbeatMsgJson(HBMessage hb_msg) {
        JSONObject dataset = new JSONObject();
        
        JSONObject hbObject = new JSONObject();
        hbObject.put("serviceId", hb_msg.getServiceId());
        hbObject.put("token", hb_msg.getToken());
        hbObject.put("type", hb_msg.getType());
        
        dataset.accumulate("heartbeatMsg", hbObject);
        
        return dataset.toString();
    }

    public static class HBMessage implements Serializable{
        private static final long serialVersionUID = 1L;
        
        private String serviceId;
        private String token;
        private String type;
        
        public HBMessage() {
            super();
        }

        public HBMessage(String serviceId, String token, String type) {
            super();
            this.serviceId = serviceId;
            this.token = token;
            this.type = type;
        }

        public String getServiceId() {
            return serviceId;
        }
        
        public void setServiceId(String serviceId) {
            this.serviceId = serviceId;
        }
        
        public String getToken() {
            return token;
        }
        
        public void setToken(String token) {
            this.token = token;
        }
        
        public String getType() {
            return type;
        }
        
        public void setType(String type) {
            this.type = type;
        }
        
        @Override
        public String toString() {
            return "HBMessage [serviceId=" + serviceId + ", token=" + token + ", type=" + type
                    + "]";
        }
    }
}



在 Design 中建立一個 Java Service,名為 OPC_Heartbeat




在 OPC_Heartbeat 中定義一個 output 參數 (i.e. heartbeatJson),用來接收 buildTestJson() 所得到的 JSON String




在 OPC_Heartbeat 中,呼叫 OpcHeartbeatService 的 buildTestJson() method,取得測試的 JSON String ,並將 JSON String 傳給 output parameter
public final class OPC_Heartbeat_SVC

{

    /** 
     * The primary method for the Java service
     *
     * @param pipeline
     *            The IData pipeline
     * @throws ServiceException
     */
    public static final void OPC_Heartbeat(IData pipeline) throws ServiceException {
        OpcHeartbeatService hbService = new OpcHeartbeatService();
        String hbJson = hbService.buildTestJson();
        
        IDataCursor pipelineCursor = pipeline.getCursor();
        IDataUtil.put(pipelineCursor, "heartbeatJson", hbJson);
        pipelineCursor.destroy();
    }
    
    // ignore......
}    



OPC_Heartbeat 執行結果




將 OPC_Heartbeat 拉近 Flow Service,並利用 pub.json:jsonStringToDocument 此現成套件協助我們將 JSON String 轉成 ESB 中的 Document 物件




No comments: