首先確定已經(jīng)安裝好axis2 并把相應(yīng)的war包放到servlet容器下,我用的是 tomcat.
容器自動(dòng)部署war包,可以看到下面的目錄結(jié)構(gòu):
我們做的服務(wù)放在services目錄下就可以了。
第一步:創(chuàng)建服務(wù) StockQuoteService.java
package samples.quickstart.service.pojo;

import java.util.HashMap;


public class StockQuoteService ...{
private HashMap map = new HashMap();


public double getPrice(String symbol) ...{
Double price = (Double) map.get(symbol);

if (price != null) ...{
return price.doubleValue();
}
return 42.00;
}


public void update(String symbol, double price) ...{
map.put(symbol, new Double(price));
}

public String echo(String name)...{
return "Hello,"+name+"!";
}
}

這個(gè)服務(wù)是一個(gè)POJO,不再解釋。
第二步:創(chuàng)建服務(wù)描述 services.xml
<service name="StockQuoteService" scope="application" targetNamespace="http://quickstart.samples/">
<description>
Stock Quote Service
</description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<schema schemaNamespace="http://quickstart.samples/xsd"/>
<parameter name="ServiceClass">samples.quickstart.service.pojo.StockQuoteService</parameter>
</service>

在服務(wù)描述 services.xml 中定義了幾個(gè)屬性值。
第三步:組裝成axis2服務(wù)的文件目錄格式:
StockQuoteService
- META-INF
- services.xml
- samples
- quickstart
- service
- pojo
- StockQuoteService.class
文件夾名字為
StockQuoteService,下面有兩個(gè)子文件夾META-INF,samples分別存放服務(wù)描述XML和服務(wù)的CLASS。
第四步:將StockQuoteService文件夾拷到%TOMCAT_HOME%\webapps\axis2\WEB-INF\services下并啟動(dòng)TOMCAT,
這時(shí)訪問:http://localhost:8080/axis2/rest/StockQuoteService/echo?name=World 看看是不是得到:
<ns:echoResponse>
<ns:return>Hello,World!</ns:return>
</ns:echoResponse>
yes! 說明服務(wù)已經(jīng)配置成功了,接下來創(chuàng)建一個(gè)客戶端訪問服務(wù):
客戶端需要兩個(gè)jar文件,可以在axis2安裝目錄的lib下找到,這兩個(gè)是: axiom-api-1.2.jar,axis2-kernel-1.1.jar
AXIOMClient.java
package samples.quickstart.clients;

//axiom-api-1.2.jar
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
//axis2-kernel-1.1.jar
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;


public class AXIOMClient ...{

private static EndpointReference targetEPR =
new EndpointReference(
"http://localhost:8080/axis2/services/StockQuoteService");


public static OMElement getPricePayload(String symbol) ...{
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://quickstart.samples/xsd", "tns");

OMElement method = fac.createOMElement("getPrice", omNs);
OMElement value = fac.createOMElement("symbol", omNs);
value.addChild(fac.createOMText(value, symbol));
method.addChild(value);
return method;
}


public static OMElement echoServie(String name)...{
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://quickstart.samples/xsd", "tns");
OMElement method = fac.createOMElement("echo", omNs);
OMElement value = fac.createOMElement("name", omNs);
value.addChild(fac.createOMText(value, name));
method.addChild(value);
return method;
}

public static OMElement updatePayload(String symbol, double price) ...{
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://quickstart.samples/xsd", "tns");

OMElement method = fac.createOMElement("update", omNs);

OMElement value1 = fac.createOMElement("symbol", omNs);
value1.addChild(fac.createOMText(value1, symbol));
method.addChild(value1);

OMElement value2 = fac.createOMElement("price", omNs);
value2.addChild(fac.createOMText(value2,
Double.toString(price)));
method.addChild(value2);
return method;
}


public static void main(String[] args) ...{

try ...{
OMElement echoServie = echoServie("Bene.Wu");
OMElement getPricePayload = getPricePayload("WSO");
OMElement updatePayload = updatePayload("WSO", 128.42);
Options options = new Options();
options.setTo(targetEPR);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);

ServiceClient sender = new ServiceClient();
sender.setOptions(options);

OMElement resultEcho = sender.sendReceive(echoServie);
String responseEcho = resultEcho.getFirstElement().getText();
System.err.println(responseEcho);
sender.fireAndForget(updatePayload);
System.err.println("done");
OMElement result = sender.sendReceive(getPricePayload);
String response = result.getFirstElement().getText();
System.err.println("Current price of WSO: " + response);


} catch (Exception e) ...{
e.printStackTrace();
}
}
}

運(yùn)行一下,看看結(jié)果是不是:
Hello,Bene.Wu!
done
Current price of WSO: 128.42
哈,bingo! axis2一共有五種創(chuàng)建服務(wù)的方式和四種創(chuàng)建客戶端的方式,我覺得只要選擇自己適合的方式就可以了,沒有必要糾纏在技術(shù)細(xì)節(jié)上。接下來我打算繼續(xù)學(xué)習(xí)和研究axis2的一些實(shí)現(xiàn)細(xì)節(jié)以及OSGi插件在axis2中的應(yīng)用。
axis2和OSGi一樣 帶來的不僅僅是服務(wù)實(shí)現(xiàn)技術(shù),而是對(duì)軟件架構(gòu)的沖擊。
我覺得SOA時(shí)代已經(jīng)到來,你準(zhǔn)備好了嗎?