axis2服务发布与客户端代码示例

下面是客户端调用类的完整代码。执行了excuteTask方法。

服务端代码apache官方网站很详细完整。
如有web工程中用spring发布,只要在web-inf目录下建立services目录,下面建相应的仍webservice服务目录,目录下建META-INF目录,下面是services.xml.这个是
webservice的发布配置文件。
如:[img][/img]
--services.xml一般配置如下
<serviceGroup>
<service name="tesTaskExcuteService">
<description>
TES Service
</description>
<parameter name="ServiceClass" locked="false">stat2.common.task.tes.webservice.ITesTaskExcuteService</parameter>
<parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter>
<parameter name="SpringBeanName" >tesTaskExcuteService</parameter>
<!--
上面是与spring结合的配置。这是不与spring结合的配置
<parameter name="ServiceClass" locked="false">
stat2.common.task.tes.webservice.TesTaskExcuteServiceImpl
</parameter>
-->

<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>
</service>
</serviceGroup>


在web.xml做相应配置
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>
org.apache.axis2.transport.http.AxisServlet
</servlet-class>
<load-on-startup>4</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>




import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class TestTaskExcuteServiceClient {
protected String wsURL;
protected String namespaceURI;
protected long timeOut;//--超时时间,默认为30秒
protected Object invokeWs(String method, Object[] parm, Class[] returnTypes)
throws AxisFault {

RPCServiceClient serviceClient;
serviceClient = new RPCServiceClient();
//这三行代码一般不加也没问题。但大批量操作的时候不加有时好象会出问题,
//我这里就出现过,加上好象就没事了。
MessageContext msgContext = new MessageContext();
msgContext.setServiceContext(serviceClient.getServiceContext());
MessageContext.setCurrentMessageContext(msgContext);
Options options = new Options();
options.setTimeOutInMilliSeconds(this.timeOut);//不设则默认为30秒
options.setTo(new EndpointReference(wsURL));
options.setAction("urn:" + method);
serviceClient.setOptions(options);
//这里可以返回数组刚取数组,返回对象则取返回的第一个元素
return serviceClient.invokeBlocking(new QName(namespaceURI, method), parm, returnTypes)[0];
}

/**
* webservice客户端访问程序,不带返回值的方法调用
*
* @param method
* @param parm
* @throws AxisFault
*/
protected void invokeNoReturn(String method, Object[] parm) throws AxisFault {
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = new Options();
options.setTimeOutInMilliSeconds(this.timeOut);
options.setTo(new EndpointReference(address));
options.setAction("urn:" + method);
serviceClient.setOptions(options);
serviceClient.invokeRobust(new QName(namespaceURI, method), parm);
}


public static void main(String[] args) throws AxisFault {
TestTaskExcuteServiceClient TClient=new TestTaskExcuteServiceClient();
TClient.setWsURL("http://localhost:8080/webApp/services/tesTaskExcuteService");
TClient.setNamespaceURI("http://webservice.asyntask.common.stat2");
String methodName="excuteTask";
TClient.setTimeout(5000L);
Boolean result=(Boolean)TClient.invokeWs(methodName,new Object[]{new Long(123456)},new Class[] {boolean.class});
System.out.println("result :"+result.toString());
}

public long getTimeOut() {
return timeOut;
}

public void setTimeOut(long timeOut) {
this.timeOut = timeOut;
}

public String getWsURL() {
return wsURL;
}

public void setWsURL(String wsURL) {
this.wsURL = wsURL;
}

public String getNamespaceURI() {
return namespaceURI;
}

public void setNamespaceURI(String namespaceURI) {
this.namespaceURI = namespaceURI;
}

}

服务端接口代码如下:
public interface ITesTaskExcuteService {
public boolean excuteTask(Long taskId);


关于返回值类型的一些说明:
1.如果是jdk1.5 以下,则要注意int,float等类型问题。axis2在返回一个int型时会封装成Integer,所以需转换成 Integer型,在参数类型里则用int.class或Integer.class都一样,其他如float,boolean等相同。
2.如果要返回数组类型。jdk1.4也一样。刚返回值类型如new Class[] {int[].class});将返回一个int[]数组,但它只是返回值数组的第一个值,仍如这样取值:return serviceClient.invokeBlocking(new QName(namespaceURI, method), parm, returnTypes)[0];
如果要整个返回值是你要的数组,则应当刚返回值类型如new Class[] {int.class,int.class})刚可这般取值。
return serviceClient.invokeBlocking(new QName(namespaceURI, method), parm, returnTypes);这样更适合返回多个不同类型的结果组成的对象数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值