axis2 webservice学习笔记一

axis2 webservice


axis  包括以下几部分
消息子系统
传输框架子系统
数据编码子系统


下载axis2
地址如下:
http://axis.apache.org/axis2/java/core/download.cgi




环境搭建
1 eclipse 新建工程axis2webservice
2 axis2-1.6.2-bin\axis2-1.6.2\webapp\axis2-web整个文件夹放到webcontent目录下
3 添加servlet解析所用jar包
    javax.servlet.jsp.jar
    servlet-api-2.5.jar
4 WEB-INF下新建service文件夹
5 repository\modules目录放到WEB-INF目录下




service服务端


示例代码helloworld
1
/**
 * axis2webservice的service端
 * 
 * @time 上午11:36:53
 * @author retacn yue
 * @Email [email protected]
 */
public class HelloWorld {
public OMElement sayHello(OMElement element) {
element.build();
element.detach();


return element;
}
}


2 在service目录中新建与工程同名文件夹HelloWorld 
  并在该目录下添加service.xml
  示例代码如下:
  <service name="HelloWorld" >
    <parameter name="ServiceClass" 


locked="xsd:false">cn.yue.axis2service</parameter>
    <operation name="sayHello"> 
        <actionMapping>urn:sayHello</actionMapping>
        <messageReceiver 


class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
    </operation>
</service>




3修改web.xml文件
    <servlet>
        <servlet-name>AxisServlet</servlet-name>
        <servlet-class>
            org.apache.axis2.transport.http.AxisServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet-mapping>
         <servlet-name>AxisServlet</servlet-name>
         <url-pattern>/servlet/AxisServlet</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
       <servlet-name>AxisServlet</servlet-name>
       <url-pattern>*.jws</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
    
    <servlet>
        <servlet-name>AxisAdminServlet</servlet-name>
        <servlet-class>org.apache.axis2.transport.http.AxisAdminServlet</servlet-


class>
        <load-on-startup>100</load-on-startup>
   </servlet>
   <servlet-mapping>
       <servlet-name>AxisAdminServlet</servlet-name>
       <url-pattern>/axis2-admin/*</url-pattern>
   </servlet-mapping>
    
注:
 org.apache.axis2.transport.http.AxisAdminServlet找不到类
 直接导原码




4将工程部署到tomcat
 http://localhost:8089/axis2-web/


客户端
四种不同类型客户端
1 block 阻塞式客户端
  /**
 * 传统客户端(阻塞式)
 * 
 * @time 下午1:49:52
 * @author retacn yue
 * @Email [email protected]
 */
public class HelloWorldClient {
private static EndpointReference endpointReference = new EndpointReference


("http://localhost:8089/services/HelloWorld");


public void sayHello() {
Options options = new Options();
options.setAction("urn:sayHello");
options.setTo(endpointReference);


ServiceClient sender = null;


try {
sender = new ServiceClient();
sender.setOptions(options);
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namespace = factory.createOMNamespace


("http://axis2service.yue.cn", "");


OMElement method = factory.createOMElement("sayHello", 


namespace);
OMElement name = factory.createOMElement("name", namespace);
name.setText("yue");
method.addChild(name);
method.build();


OMElement response = sender.sendReceive(method);
System.out.println(response);
OMElement element = response.getFirstChildWithName(new 


QName("http://axis2service.yue.cn", "name"));
System.out.println(element.getText());
} catch (AxisFault e) {
System.out.println("error");
e.printStackTrace();
}


}


/**
* @param args
*/
public static void main(String[] args) {
HelloWorldClient client = new HelloWorldClient();
client.sayHello();
}


}
2 non block 非阻塞式客户端
/**
 * 
 * @time 下午3:27:21
 * @author retacn yue
 * @Email [email protected]
 */
public class HelloWorldNonBlockCB implements AxisCallback {
private boolean complete = false;


@Override
public void onMessage(MessageContext msgContext) {
System.out.println(msgContext.getEnvelope().getBody());
OMElement element = msgContext.getEnvelope().getBody


().getFirstElement();
OMElement result = element.getFirstChildWithName(new QName


("http://axis2service.yue.cn", "name"));
System.out.println(result.getText());
synchronized (this) {
this.notify();
}
}


@Override
public void onFault(MessageContext msgContext) {
System.out.println(msgContext.getEnvelope().getBody().getFault


().toString());
synchronized (this) {
this.notify();
}
}


@Override
public void onError(Exception e) {
e.printStackTrace();
synchronized (this) {
this.notify();
}
}


@Override
public void onComplete() {
this.complete = true;
synchronized (this) {
this.notify();
}
}


public boolean isComplete() {
return complete;
}


}






/**
 * 非阻塞式
 * 
 * @time 下午3:37:31
 * @author retacn yue
 * @Email [email protected]
 */
public class HelloWorldClient2 {
private static EndpointReference endpointReference = new EndpointReference


("http://localhost:8089/services/HelloWorld");


public void sayHello() {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namespace = factory.createOMNamespace


("http://axis2service.yue.cn", "");
OMElement method = factory.createOMElement("sayHello", namespace);
OMElement name = factory.createOMElement("name", namespace);
name.setText("zhenhua");
method.addChild(name);
method.build();


Options options = new Options();
options.setTo(endpointReference);
ServiceClient sender = null;


try {
HelloWorldNonBlockCB blockCB = new HelloWorldNonBlockCB();
sender = new ServiceClient();
sender.setOptions(options);
sender.sendReceiveNonBlocking(method, blockCB);
synchronized (blockCB) {
try {
blockCB.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (AxisFault e) {
e.printStackTrace();
} finally {
if (null != sender) {
try {
sender.cleanup();
} catch (AxisFault e) {
e.printStackTrace();
}
}
}
}


public static void main(String[] args) {
HelloWorldClient2 client2 = new HelloWorldClient2();
client2.sayHello();
}


}


 
  


4 双工非阻塞
  在客户端打开一个监听
  设置客户端为双工全开
  寻址
  engageModule(Constants.MODULE_ADDRESSING);
/**
 * 非阻塞全双工
 * 
 * @time 下午4:04:55
 * @author retacn yue
 * @Email [email protected]
 */
public class HelloWorldDualNonBlock {
private static EndpointReference endpointReference = new EndpointReference


("http://localhost:8089/services/HelloWorld");


public void sayHello() {
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namespace = factory.createOMNamespace


("http://axis2service.yue.cn", "");
OMElement method = factory.createOMElement("sayHello", namespace);
OMElement name = factory.createOMElement("name", namespace);
name.setText("yuezhenhua");
method.addChild(name);
method.build();


Options options = new Options();
options.setTo(endpointReference);
//设置客户端为双工全开
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
//在客户端打开一个监听
options.setUseSeparateListener(true);
options.setAction("urn:sayHello");


ServiceClient sender = null;
HelloWorldNonBlockCB blockCB = new HelloWorldNonBlockCB();


try {
sender = new ServiceClient();
//寻址
sender.engageModule(Constants.MODULE_ADDRESSING);
sender.setOptions(options);
sender.sendReceiveNonBlocking(method, blockCB);
synchronized (blockCB) {
try {
blockCB.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (AxisFault e) {
e.printStackTrace();
} finally {
try {
if (null != sender) {
sender.cleanup();
}
} catch (AxisFault e) {
e.printStackTrace();
}


}
}


public static void main(String[] args) {
HelloWorldDualNonBlock client = new HelloWorldDualNonBlock();
client.sayHello();
}


}


axis2-1.6.2-bin\axis2-1.6.2\conf目录下的axis2.xml文件放到service端的WEB-INF目录下

你可能感兴趣的:(axis2 webservice学习笔记一)