二 兩種方式自訂Web Service
在IDE裡建立一個Web Service工程是很容易的,只要找到【File】-【New】-【Web Service】菜單就可以了,點擊這個菜單將會彈出下面的對話方塊:
這裡的Web service type有兩種選擇:一種是Bottom up、另外一種是Top down。這兩種方式分別對應了自下而上和自上而下兩種建立Web Service的模式。如果你已經有了實現的功能類,現在想要把它封裝成Web Service,那麼你應當選擇Bottom up模式;如果你首先定義了Web Service的描述檔案(wsdl),現在想要實現這個wsdl的功能,那麼你應當選擇Top down模式。
三 實現Web Service的功能
不管通過Bottom up還是Top down方式,當你把Web Service的架構搭起來以後,剩下的工作就是如何去實現Web Service的功能了。下面讓我們通過講解一個網路分析Web Service的實現,來瞭解自訂Web Service的運作。
這個網路分析的Web Service需要的功能是預先定義好的,其中有比如上下遊追溯、連通性分析等等,這裡只挑連通性分析這個小功能進行說明。
首先,我們通過給定的wsdl建立了一個Web Service工程,然後找到實作類別,在這個類中已經根據wsdl自動產生了對應各個功能的方法,這裡就連通性分析功能進行講解,對應的方法是connectivity,下面是這個方法的實現:
public
class
TestGISAnalysisSoapBindingImpl implements
TestGISAnalysisPortType
{
private
GeoNetworkAnalysis geoNetAnalysis;
public
TestGISAnalysisSoapBindingImpl()
{
geoNetAnalysis = new
GeoNetworkAnalysis();
}
public
java.lang.String connectivity(String sourceDeviceType
, String sourceDeviceID
, String targetDeviceType
, String targetDeviceID,
String type) throws
java.rmi.RemoteException
{
String result = "";
try
{
result = geoNetAnalysis.findPath(Integer.parseInt
(sourceDeviceType)
, Integer.parseInt
(sourceDeviceID)
, Integer.parseInt
(targetDeviceType)
, Integer.parseInt
(targetDeviceID)
, type);
}
catch
(Exception ex)
{
ex.printStackTrace();
}
return
result;
}
}
怎麼這麼簡單?當然,因為這裡對功能做了封裝,真正的分析功能在geoNetAnalysis這個對象中,在這個對象中進行的都是直接對AO的操作,我們通過一些程式碼片段來看一下這個功能的實現:
public
class
GeoNetworkAnalysis
{
public
GeoNetworkAnalysis()
{
try
{
EngineInitializer.initializeEngine();
new
AoInitialize().initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);
ResourceBundle bundle = ResourceBundle.getBundle("geometrynetwork");
traceFlowSolver = new
TraceFlowSolver();
INetSolver netSolver = (INetSolver) traceFlowSolver;
geometricNetwork = openNetwork(bundle.getString("workspacePath"),
bundle.getString("datasetName"),bundle.getString("networkName"));
pNetElements = new
INetElementsProxy(geometricNetwork.getNetwork());
netSolver.setSourceNetworkByRef(geometricNetwork.getNetwork());
...
}
catch
(Exception e)
{
e.printStackTrace();
}
}
/**
* 連通性分析
*
* @param
sourceLyrID
* 起點的圖層ID
* @param
sourceFeatID
* 起點的要素ID
* @param
targetLyrID
* 終點的圖層ID
* @param
targetFeatID
* 終點的要素ID
* @param
type
* @return
*/
public
String findPath(int
sourceLyrID
, int
sourceFeatID
, int
targetLyrID
, int
targetFeatID
, String type) throws
Exception
{
this
.putTraceOrigins(getOriginPoints(sourceLyrID
,sourceFeatID
,targetLyrID
,targetFeatID));
IEnumNetEID[] junctionEIDS = new
IEnumNetEID[1];
IEnumNetEID[] edgeEIDs = new
IEnumNetEID[1];
Object[][] costArray = new
Object[1][1];
traceFlowSolver.findPath(esriFlowMethod.esriFMConnected,
esriShortestPathObjFn.esriSPObjFnMinSum
, junctionEIDS
, edgeEIDs
, 1
, costArray);
...
Set junctions = this
.getSolverResultFeature(junctionEIDS[0]);
Set edges = this
.getSolverResultFeature(edgeEIDs[0]);
return
FeatureUtil.networkResultToJSON(junctions, edges);
}
}
注意,上面只是程式碼片段,整個功能的實現代碼太長了,和我們要瞭解的Web Service其實又沒有什麼關係,所以,就看個大概吧。
做完這些,在很多場合都可以調用這個自訂的Web Service了,比如上面這個功能我們是在Flex中調用的:
showBusyCursor="true"
concurrency="last"
result="onWsAnalysisResult(event)"
fault="onWsAnalysisFault(event)"/>
試想一下,是不是不同的業務系統之間互動也可以做了呢?