|
楼主 |
发表于 2011-5-21 09:58
|
显示全部楼层
(四):HelloWorld二改进版(AXIOM)
1:类文件修改成为
- package samples.quickstart.service.axiom;
- import javax.xml.stream.XMLStreamException;
- import org.apache.axiom.om.OMAbstractFactory;
- import org.apache.axiom.om.OMElement;
- import org.apache.axiom.om.OMFactory;
- import org.apache.axiom.om.OMNamespace;
- import java.util.HashMap;
- public class StockQuoteService {
- private HashMap map = new HashMap();
-
- public OMElement getPrice(OMElement element) throws XMLStreamException {
- //构建节点本身
- element.build();
- //从父节点删除本节点
- element.detach();
- //获取节点的第一个子节点
- OMElement symbolElement = element.getFirstElement();
- //获取这个子节点的文本值
- String symbol = symbolElement.getText();
- //准备返回的值
- String returnText = "42";
- Double price = (Double) map.get(symbol);
- if (price != null) {
- returnText = "" + price.doubleValue();
- }
- //组装需要返回的xml,值也包含在这个xml当中
- //<getPriceResponse>
- // <price>
- // returnText
- // </price>
- //</getPriceResponse>
- OMFactory fac = OMAbstractFactory.getOMFactory();
- OMNamespace omNs = fac.createOMNamespace(
- "ht tp://axiom.service.quickstart.samples/xsd", "tns");
- OMElement method = fac.createOMElement("getPriceResponse", omNs);
- OMElement value = fac.createOMElement("price", omNs);
- value.addChild(fac.createOMText(value, returnText));
- method.addChild(value);
- return method;
- }
- public void update(OMElement element) throws XMLStreamException {
- //构建节点本身
- element.build();
- //从父节点删除本节点
- element.detach();
- //获取节点的第一个子节点——symbol
- OMElement symbolElement = element.getFirstElement();
- //获取这个子节点的文本值
- String symbol = symbolElement.getText();
- //获取节点的第二个字节点——price
- OMElement priceElement = (OMElement) symbolElement.getNextOMSibling();
- //获取price的值
- String price = priceElement.getText();
- //把值放置到缓存中
- map.put(symbol, new Double(price));
- }
- }
复制代码 2:service.xml如下:
- <service name="StockQuoteService" scope="application">
- <description>
- Stock Quote Service
- </description>
- <operation name="getPrice">
- <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
- </operation>
- <operation name="update">
- <messageReceiver class="org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver"/>
- </operation>
- <parameter name="ServiceClass">samples.quickstart.service.axiom.StockQuoteService</parameter>
- </service>
复制代码 3:重新打包,然后拷贝到webapps\axis2\WEB-INF\services下面,覆盖ht.aar
4:客户端:
- package samples.quickstart.service.axiom;
- import org.apache.axiom.om.OMAbstractFactory;
- import org.apache.axiom.om.OMElement;
- import org.apache.axiom.om.OMFactory;
- import org.apache.axiom.om.OMNamespace;
- import org.apache.axis2.AxisFault;
- import org.apache.axis2.Constants;
- import org.apache.axis2.addressing.EndpointReference;
- import org.apache.axis2.clustering.MessageSender;
- import org.apache.axis2.client.Options;
- import org.apache.axis2.client.ServiceClient;
- public class Client {
- //指定打包的Service在容器中的物理位置
- private static EndpointReference targetEPR =
- new EndpointReference("ht tp://localhost:8080/axis2/services/StockQuoteService");
- /**
- * 准备访问getPrice方法时需要传递的xml节点
- * <getPrice>
- * <symbol>
- * value
- * </symbol>
- * </getPrice>
- * @param symbol
- * @return
- */
- public static OMElement getPricePayload(String symbol) {
- OMFactory fac = OMAbstractFactory.getOMFactory();
- OMNamespace omNs = fac.createOMNamespace("ht tp://axiom.service.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;
- }
- /**
- * 准备访问update方法时需要传递的xml节点
- * <update>
- * <symbol>
- * value
- * </symbol>
- * <price>
- * price value
- * </price>
- * </update>
- * @param symbol
- * @return
- */
- public static OMElement updatePayload(String symbol, double price) {
- OMFactory fac = OMAbstractFactory.getOMFactory();
- OMNamespace omNs = fac.createOMNamespace("ht tp://axiom.service.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 {
- //获或得先从服务上查找WSO的值的节点
- OMElement getPricePayload = getPricePayload("WSO");
- //获得修改WSO的值的节点
- OMElement updatePayload = updatePayload("WSO", 123.42);
-
- //准备options,设置要访问的web服务在什么地方
- Options options = new Options();
- options.setTo(targetEPR);
- options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
- ServiceClient sender = new ServiceClient();
- sender.setOptions(options);
-
- //直接调用In-Only的服务
- sender.fireAndForget(updatePayload);
-
- System.err.println("price updated");
- //直接调用In-Out的服务
- OMElement result = sender.sendReceive(getPricePayload);
- //获取到的返回值也是xml
- String response = result.getFirstElement().getText();
- System.err.println("Current price of WSO: " + response);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
复制代码 【——还有……】
|
|