Terry.Li-彬

虚其心,可解天下之问;专其心,可治天下之学;静其心,可悟天下之理;恒其心,可成天下之业。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  143 随笔 :: 344 文章 :: 130 评论 :: 0 Trackbacks
首先确定已经安装好axis2 并把相应的war包放到servlet容器下,我用的是 tomcat.
    容器自动部署war包,可以看到下面的目录结构:
axis2

     我们做的服务放在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时代已经到来,你准备好了吗?
posted on 2007-11-21 18:02 礼物 阅读(1232) 评论(0)  编辑  收藏 所属分类: web service