您的位置:首页 > 其它

学习Axis2笔记之四--创建pojo服务和客户端

2007-01-07 16:10 513 查看
首先确定已经安装好axis2 并把相应的war包放到servlet容器下,我用的是 tomcat.
容器自动部署war包,可以看到下面的目录结构:



我们做的服务放在services目录下就可以了。

第一步:创建服务 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+"!";


}


}



这个服务是一个POJO,不再解释。

第二步:创建服务描述 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>



在服务描述 services.xml 中定义了几个属性值。

第三步:组装成axis2服务的文件目录格式:


StockQuoteService


- META-INF


- services.xml


- samples


- quickstart


- service


- pojo


- StockQuoteService.class

文件夹名字为StockQuoteService,下面有两个子文件夹META-INF,samples分别存放服务描述XML和服务的CLASS。

第四步:将StockQuoteService文件夹拷到%TOMCAT_HOME%/webapps/axis2/WEB-INF/services下并启动TOMCAT,
这时访问:http://localhost:8080/axis2/rest/StockQuoteService/echo?name=World 看看是不是得到:

<ns:echoResponse>
<ns:return>Hello,World!</ns:return>
</ns:echoResponse>

yes! 说明服务已经配置成功了,接下来创建一个客户端访问服务:

客户端需要两个jar文件,可以在axis2安装目录的lib下找到,这两个是: 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();


}


}




}



运行一下,看看结果是不是:
Hello,Bene.Wu!
done
Current price of WSO: 128.42

哈,bingo! axis2一共有五种创建服务的方式和四种创建客户端的方式,我觉得只要选择自己适合的方式就可以了,没有必要纠缠在技术细节上。接下来我打算继续学习和研究axis2的一些实现细节以及OSGi插件在axis2中的应用。

axis2和OSGi一样 带来的不仅仅是服务实现技术,而是对软件架构的冲击。
我觉得SOA时代已经到来,你准备好了吗?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐