加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 01:26:11 所属栏目:安全 来源:网络整理
导读:下面是客户端调用类的完整代码。执行了excuteTask方法。 服务端代码apache官方网站很详细完整。 如有web工程中用spring发布,只要在web-inf目录下建立services目录,下面建相应的仍webservice服务目录,目录下建META-INF目录,下面是services.xml.这个是 web
下面是客户端调用类的完整代码。执行了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,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,returnTypes)[0]; 如果要整个返回值是你要的数组,则应当刚返回值类型如new Class[] {int.class,int.class})刚可这般取值。 return serviceClient.invokeBlocking(new QName(namespaceURI,returnTypes);这样更适合返回多个不同类型的结果组成的对象数组。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读