WebService客户端编写及调用

WebService客户端主要用来调用WebService服务端提供的接口

WebService客户端编写如下:

一.用MyEclipse提供的WebService客户端生成工具生成(根据WebService服务的WSDL文件生成)

 

  首先新建Web工程,再在工程中新建Web Service Client

 

 

 

  注:此处需要添加WebServiceClient插件才能添加完成(需要WSDL文件)

 

二.用XFire生成工具生成

 1.生成结构如下

 

 

 

smsServieClient.java客户端测试连接WebService是否成功     public static void main(String[] args) { smsServiePortType sms = new smsServieClient().getsmsServieHttpPort(); // sms=new // smsServieClient().getsmsServieHttpPort("http://192.168.0.30:8088/SMSProxy/services/smsServie?wsdl"); sms.sendMsg("sf", null, "13500000000"); }

 

  2.将src下的包全部拷贝到需要调用WebService服务端的工程(TestWeb)或者打包(*.jar)(如WebServiceClient.jar)

 

  3.TestWeb调用客户端

    TestWeb调用客户端,而客户端调用服务端,TestWeb需要增加2个类(客户端代理类,代理工厂)和WebService客户端生成的类WebServiceClient.jar

 

    SMSProxyService.java(客户端代理池,通过调用该代理,调用客户端中的方法)

    package com.ipi.wap.portal.proxy; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.pool.impl.GenericObjectPool; import com.sms.service.smsServieClient; import com.sms.service.smsServiePortType; public class SMSProxyService { private Log logger = LogFactory.getLog(SMSProxyService.class); private String serviceURL; private GenericObjectPool pool;// HttpClient对象池 /** * 将对象归还给对象池 * */ public void returnObjToPool(smsServieClient client) { try { if (client != null) pool.returnObject(client); } catch (Exception e) { logger.info("将短信接口模块客户端还回池时发生错误!"); e.printStackTrace(); } } public String sendMsg(String smsContent, String tmp, String destArrList) { logger.info("开始调用发送短信接口操作: "); String result = null; smsServieClient client = null; try { client = (smsServieClient) pool.borrowObject(); smsServiePortType service = client.getsmsServieHttpPort(serviceURL); result = service.sendMsg(smsContent, tmp, destArrList); service = null; } catch (Exception e) { logger.info("发送短信时发生异常,原因:" + e.getMessage()); e.printStackTrace(); } finally { returnObjToPool(client); } return result; } /** * @param pool the pool to set */ public void setPool(GenericObjectPool pool) { this.pool = pool; } /** * @param serviceURL the serviceURL to set */ public void setServiceURL(String serviceURL) { this.serviceURL = serviceURL; } }

 

 

SMSProxyServicePoolFactory.java

package com.ipi.wap.portal.proxy; import org.apache.commons.pool.PoolableObjectFactory; import com.sms.service.smsServieClient; /** * 短信发送接口模块客户端连接池工厂 * * @author lhj * */ public class SMSProxyServicePoolFactory implements PoolableObjectFactory { /* * (non-Javadoc) * * @see * org.apache.commons.pool.PoolableObjectFactory#activateObject(java.lang * .Object) */ public void activateObject(Object arg0) throws Exception { } /* * (non-Javadoc) * * @see * org.apache.commons.pool.PoolableObjectFactory#destroyObject(java.lang * .Object) */ public void destroyObject(Object arg0) throws Exception { } /* * (non-Javadoc) * * @see org.apache.commons.pool.PoolableObjectFactory#makeObject() */ public Object makeObject() throws Exception { smsServieClient client = new smsServieClient(); return client; } /* * (non-Javadoc) * * @see * org.apache.commons.pool.PoolableObjectFactory#passivateObject(java.lang * .Object) */ public void passivateObject(Object arg0) throws Exception { } /* * (non-Javadoc) * * @see * org.apache.commons.pool.PoolableObjectFactory#validateObject(java.lang * .Object) */ public boolean validateObject(Object arg0) { return false; } }

 

 

applicationContext.xml文件配置

/WEB-INF/config.properties

  

config.properties

WebService服务端地址 SMSProxyServiceURL=http://192.168.0.30:8088/SMSProxy/services/smsServie

 

 

调用方法RegisterUserAction.java(TestWeb逻辑处理)

 

public class RegisterUserAction implements Action { private static Log logger = LogFactory.getLog(ContentAction.class); public void execute(ActionForward forward, HttpServletRequest request, HttpServletResponse response) throws Exception { SMSProxyService smsProxyService=(SMSProxyService)context.getBean("smsProxyService"); String str = smsProxyService.sendMsg(smsCOntext,tmp,phone); }

你可能感兴趣的:(Java)