天天看點

Ecplses+axis+BPEL2.0實作:BPEL調用加減法的Web服務,進行加減運算

  今天早上終于調通了!為了更好地交流,給大家分享一下。

  首先,說明一下我的開發環境:

         開發平台IDE:ecplise3.4-SDK JDK6.0

             Web服務: axis2 加Axis2 Wizards(Axis2 Code Generator,Axis2 Service Archiver)

             BPEL:BPEL2.0,   Apache ODE

 操作過程及代碼:

    (1)利用Axis2 Code Generator将AddService.java、SubAddService轉化為AddService.wsdl、SubService.wsdl,并将其儲存在生成BPEL項目檔案下

  public class AddService {

public int addOption(int a, int b) {

return a + b;

}

}

   AddService.java

public class SubService {

public int SubOption(int a, int b) {

return a - b;

}

}

 SubService.java

  (2)利用Axis2 Service Archiver将兩個JAVA類(AddService.java,SubService.java)分别封裝成AddService.aar,SubService.aar,并将這兩個檔案存儲在/%apache-tomcat Home%/ /webapps/axis2/WEB-INF/services目錄下。

  (3)File-New-Other-BPEL Project建立一個BPEL項目CquBPEL,通過File-New-Other-New BPEL Process File再建立BPEL檔案MathServiceProcess.bpel

  (4)在MathServiceProcessArtifacts.wsdl中建立一個MathServiceBinding(從PortType下拉框中選擇MathServiceProcess,Protocl 設為SOAP), 建立一個MathService(Binding:MathServiceBinding、Address:http://localhost:8080/ode/processes/MathServiceProcess、Protocol:SOAP),

    并設定MathServiceProcessRequest的XML Schem(type:Stirng a:int b:int) MathServiceProcessResponse的XML Schem(MathResult:int)

将步驟一中的AddService.wsdl、SubService.wsdl通過Import的方式加入到MathServiceProcessArtifacts.wsdl,其代碼如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:p="http://www.w3.org/2001/XMLSchema" xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.bpel.cqu" xmlns:vprop="http://docs.oasis-open.org/wsbpel/2.0/varprop" name="MathServiceProcess" targetNamespace="http://ws.bpel.cqu">

<!--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

TYPE DEFINITION - List of types participating in this BPEL process The

BPEL Designer will generate default request and response types but you

can define or import any XML Schema type and use them as part of the

message types.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-->

<plnk:partnerLinkType name="AddService">

<plnk:role name="AddServiceProvider" portType="tns:AddServicePortType"/>

</plnk:partnerLinkType>

<plnk:partnerLinkType name="SubService">

<plnk:role name="SubServiceProvider" portType="tns:SubServicePortType"/>

</plnk:partnerLinkType>

<import location="AddService.wsdl" namespace="http://ws.bpel.cqu"/>

<import location="SubService.wsdl" namespace="http://ws.bpel.cqu"/>

<types>

<schema xmlns="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://ws.bpel.cqu">

<element name="MathServiceProcessRequest">

<complexType>

<sequence>

<element name="type" type="string"/>

<element name="a" type="int"/>

<element name="b" type="int"/>

</sequence>

</complexType>

</element>

<element name="MathServiceProcessResponse">

<complexType>

<sequence>

<element name="MathResult" type="int"/>

</sequence>

</complexType>

</element>

<element name="NewOperation">

<complexType>

<sequence>

<element name="in" type="string"/>

</sequence>

</complexType>

</element>

<element name="NewOperationResponse">

<complexType>

<sequence>

<element name="out" type="string"/>

</sequence>

</complexType>

</element>

</schema>

</types>

<!--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MESSAGE TYPE DEFINITION - Definition of the message types used as part

of the port type defintions

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-->

<message name="MathServiceProcessRequestMessage">

<part element="tns:MathServiceProcessRequest" name="payload"/>

</message>

<message name="MathServiceProcessResponseMessage">

<part element="tns:MathServiceProcessResponse" name="payload"/>

</message>

<!--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

PORT TYPE DEFINITION - A port type groups a set of operations into a

logical service unit.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-->

<!-- portType implemented by the MathServiceProcess BPEL process -->

<portType name="MathServiceProcess">

<operation name="process">

<input message="tns:MathServiceProcessRequestMessage"/>

<output message="tns:MathServiceProcessResponseMessage"/>

</operation>

</portType>

<!--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

PARTNER LINK TYPE DEFINITION

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-->

<plnk:partnerLinkType name="MathServiceProcess">

<plnk:role name="MathServiceProcessProvider" portType="tns:MathServiceProcess"/>

</plnk:partnerLinkType>

<binding name="MathServiceBinding" type="tns:MathServiceProcess">

<soap:binding style="document" mce_style="document" transport="http://schemas.xmlsoap.org/soap/http">

</soap:binding>

<operation name="process">

<soap:operation soapAction="http://ws.bpel.cqu/process">

</soap:operation>

<input>

<soap:body use="literal"/>

</input>

<output>

<soap:body use="literal"/>

</output>

</operation>

</binding>

<service name="MathService">

<port binding="tns:MathServiceBinding" name="MathServicePort">

<soap:address location="http://localhost:8080/ode/processes/MathServiceProcess"/>

</port>

</service>

</definitions>

主要實作的功能是,當使用者輸入運算類型Type是‘add’時,則設用AddService,将a與b進行相加,或者調用SubService進行減法操作

 (5)定義BPEL中Partner Links(client、addService、subService)

   client:

        Partner Link Type:MathServiceProcess;

        MyRole:MathServiceProcessProvider;

    addService:

        Partner Link Type:AddService;(步驟一中對應的AddService.wsdl的AddService服務)

        Partner Role:AddServiceProvider(這個需要按提示步驟進行自定義設定);

addService:

        Partner Link Type:AddService;(步驟一中對應的SubService.wsdl的AddService服務)

        Partner Role:AddServiceProvider(這個需要按提示步驟進行自定義設定);

  (6)定義BPEL中Variables(input、output、inputForAdd、inputForSub、outputForAdd、outputForSub)

 input:

                  MessageType:MathServiceProcessRequestMessage,Structure:ComplexType(type:Stirng a:int  b:int)

 output:

                  MessageType:MathServiceProcessResponseMessage,Structure:ComplexType(MathResult:int)

 inputForAdd:

                  MessageType:addOptionRequest,Structure:ComplexType(a:int  b:int)

 inputForSub:

                  MessageType:subOptionRequest,Structure:ComplexType(a:int  b:int)

 outputForAdd:

                  MessageType:addOptionRequest,Structure:ComplexType(return:int)

 outputForSub:

                  MessageType:subOptionRequest,Structure:ComplexType(return:int)

   (7)利用BPEL2.0檔案進行業務描述,可視化界面如下圖:(不知CSDN怎麼上傳圖檔哎),是以隻好上傳代碼了

  <!--

MathServiceProcess BPEL Process [Generated by the Eclipse BPEL

Designer]

-->

<bpel:process name="MathServiceProcess" targetNamespace="http://ws.bpel.cqu"

suppressJoinFailure="yes" xmlns:tns="http://ws.bpel.cqu"

xmlns:bpel="http://docs.oasis-open.org/wsbpel/2.0/process/executable"

xmlns:ns1="http://ws.bpel.cqu" xmlns:xsd="http://ws.bpel.cqu">

<!-- Import the client WSDL -->

<bpel:import namespace="http://ws.bpel.cqu" location="SubService.wsdl"

importType="http://schemas.xmlsoap.org/wsdl/"></bpel:import>

<bpel:import namespace="http://ws.bpel.cqu" location="AddService.wsdl"

importType="http://schemas.xmlsoap.org/wsdl/"></bpel:import>

<bpel:import location="MathServiceProcessArtifacts.wsdl"

namespace="http://ws.bpel.cqu" importType="http://schemas.xmlsoap.org/wsdl/" />

<!--

=================================================================

-->

<!--

PARTNERLINKS

-->

<!--

List of services participating in this BPEL process

-->

<!--

=================================================================

-->

<bpel:partnerLinks>

<!-- The 'client' role represents the requester of this service. -->

<bpel:partnerLink name="client" partnerLinkType="tns:MathServiceProcess"

myRole="MathServiceProcessProvider" />

<bpel:partnerLink name="addService" partnerLinkType="tns:AddService" partnerRole="AddServiceProvider"></bpel:partnerLink>

<bpel:partnerLink name="subService" partnerLinkType="tns:SubService" partnerRole="SubServiceProvider"></bpel:partnerLink>

</bpel:partnerLinks>

<!--

=================================================================

-->

<!--

VARIABLES

-->

<!--

List of messages and XML documents used within this BPEL process

-->

<!--

=================================================================

-->

<bpel:variables>

<!-- Reference to the message passed as input during initiation -->

<bpel:variable name="input"

messageType="tns:MathServiceProcessRequestMessage" />

<!--

Reference to the message that will be returned to the requester

-->

<bpel:variable name="output"

messageType="tns:MathServiceProcessResponseMessage" />

<bpel:variable name="inputForAdd" messageType="ns1:addOptionRequest"></bpel:variable>

<bpel:variable name="inputForSub" messageType="ns1:subOptionRequest"></bpel:variable>

<bpel:variable name="outputForAdd" messageType="ns1:addOptionResponse"></bpel:variable>

<bpel:variable name="outputForSub" messageType="ns1:subOptionResponse"></bpel:variable>

</bpel:variables>

<!--

=================================================================

-->

<!--

ORCHESTRATION LOGIC

-->

<!--

Set of activities coordinating the flow of messages across the

-->

<!--

services integrated within this business process

-->

<!--

=================================================================

-->

<bpel:sequence name="main">

<!--

Receive input from requester. Note: This maps to operation defined in

MathServiceProcess.wsdl

-->

<bpel:receive name="receiveInput" partnerLink="client"

createInstance="yes" operation="process" portType="tns:MathServiceProcess" variable="input"/>

<!-- Generate reply to synchronous request -->

<bpel:if name="If">

<bpel:condition><!--[CDATA[$input.payload/tns:type='add']]></bpel:condition>

<bpel:sequence name="SequenceAdd">

<bpel:assign validate="no" name="AssignIn">

<bpel:copy>

<bpel:from>

<bpel:literal xml:space="preserve"><tns:addOption xmlns:tns="http://ws.bpel.cqu" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<tns:a></tns:a>

<tns:b></tns:b>

</tns:addOption></bpel:literal>

</bpel:from><bpel:to part="parameters" variable="inputForAdd"></bpel:to>

</bpel:copy>

<bpel:copy>

<bpel:from part="payload" variable="input">

<bpel:query queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0"><![CDATA[tns:a]]></bpel:query>

</bpel:from>

<bpel:to part="parameters" variable="inputForAdd">

<bpel:query queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0"><![CDATA[tns:a]]></bpel:query>

</bpel:to>

</bpel:copy>

<bpel:copy>

<bpel:from part="payload" variable="input">

<bpel:query queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0"><![CDATA[tns:b]]></bpel:query>

</bpel:from>

<bpel:to part="parameters" variable="inputForAdd">

<bpel:query queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0"><![CDATA[tns:b]]></bpel:query>

</bpel:to>

</bpel:copy>

</bpel:assign>

<bpel:invoke name="InvokeAdd" partnerLink="addService" operation="addOption" portType="tns:AddServicePortType" inputVariable="inputForAdd" outputVariable="outputForAdd"></bpel:invoke>

<bpel:assign validate="no" name="AssignOut">

<bpel:copy>

<bpel:from>

<bpel:literal xml:space="preserve"><tns:MathServiceProcessResponse xmlns:tns="http://ws.bpel.cqu" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<tns:MathResult></tns:MathResult>

</tns:MathServiceProcessResponse>

</bpel:literal>

</bpel:from>

<bpel:to variable="output" part="payload"></bpel:to>

</bpel:copy>

<bpel:copy>

<bpel:from>

<![CDATA[$outputForAdd.parameters/tns:return]]>

</bpel:from>

<bpel:to part="payload" variable="output">

<bpel:query queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0"><![CDATA[tns:MathResult]]></bpel:query>

</bpel:to>

</bpel:copy>

</bpel:assign>

</bpel:sequence>

<bpel:elseif>

<bpel:sequence name="SequenceSub">

<bpel:assign validate="no" name="AssignIn">

<bpel:copy>

<bpel:from>

<bpel:literal xml:space="preserve"><tns:subOption xmlns:tns="http://ws.bpel.cqu" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<tns:a></tns:a>

<tns:b></tns:b>

</tns:subOption></bpel:literal>

</bpel:from><bpel:to part="parameters" variable="inputForSub"></bpel:to>

</bpel:copy>

<bpel:copy>

<bpel:from part="payload" variable="input">

<bpel:query queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0">

<![CDATA[tns:a]]>

</bpel:query>

</bpel:from><bpel:to part="parameters" variable="inputForSub">

<bpel:query queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0"><![CDATA[tns:a]]></bpel:query>

</bpel:to>

</bpel:copy>

<bpel:copy>

<bpel:from part="payload" variable="input">

<bpel:query queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0">

<![CDATA[tns:b]]>

</bpel:query>

</bpel:from><bpel:to part="parameters" variable="inputForSub">

<bpel:query queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0"><![CDATA[tns:b]]></bpel:query>

</bpel:to>

</bpel:copy>

</bpel:assign>

<bpel:invoke name="InvokeSub" partnerLink="subService" operation="subOption" portType="tns:SubServicePortType" inputVariable="inputForSub" outputVariable="outputForSub"></bpel:invoke>

<bpel:assign validate="no" name="AssignOut">

<bpel:copy>

<bpel:from>

<bpel:literal xml:space="preserve"><tns:MathServiceProcessResponse xmlns:tns="http://ws.bpel.cqu" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<tns:MathResult></tns:MathResult>

</tns:MathServiceProcessResponse>

</bpel:literal>

</bpel:from><bpel:to variable="output" part="payload"></bpel:to>

</bpel:copy>

<bpel:copy>

<bpel:from>

<![CDATA[$outputForSub.parameters/tns:return]]>

</bpel:from><bpel:to part="payload" variable="output">

<bpel:query queryLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0"><![CDATA[tns:MathResult]]--></bpel:query>

</bpel:to>

</bpel:copy>

</bpel:assign>

</bpel:sequence>

</bpel:elseif></bpel:if>

<bpel:reply name="replyOutput"

partnerLink="client"

portType="tns:MathServiceProcess"

operation="process"

variable="output"

/>

</bpel:sequence>

</bpel:process>

 (8)通過new-file-other-bpel2.0-Apache ODE Deployment Descriptor,生成deploy.xml

 (9)啟動一下Ode V1.x Server at localhost.

 (10)最後用Web Service Explorer進行測試,經測試完全通過!

繼續閱讀