XFire构建client的几种方式

XFire构建客户端也有几种方式。

一,如果能够知道并得到service的类,那么可以在客户端中通过xfier的代理工厂生成service类。然后调用相应的方法。

package test.client;
 
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
 
import cn.cjw.services.exchange.ExchangeService;
 
public class ExchangeServiceClient {
	
	public double RMB2Dollar(double RMB)
	{
		Service serviceModel = new ObjectServiceFactory().create(ExchangeService.class);
		String serviceURL = "http://[ip]:[port]/[yourProjectName]/services/ExchangeService";//相应需要修改
		ExchangeService service = null;
		try{
			service = (ExchangeService)new XFireProxyFactory().create(serviceModel, serviceURL);			
		}catch(Exception e){
			throw new RuntimeException(e);
		}
		return service.RMB2Dollar(RMB);
	}
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ExchangeServiceClient client = new ExchangeServiceClient();
		System.out.println("invoking service : ExchangeService results="+client.RMB2Dollar(100.0));
	}
 
}

 二,通过xfire的IDE插件,xfire提供好几个集成到IDE的自动生成客户端的插件。可以到官网下载。然后通过图形化界面生成client。

三,由于service类在很多情况下并不是只有自己开发的。这时候很有可能你没有办法得到service类,但是service发布的wsdl文件是可 以得到,xfire可以通过wsdl生成client。这里又包含两种方式,第一,把wsdl下载下来到本地的classpath。通过读取wsdl生 成。或者,可以直接通过url来生成。这里说明第二种情况。

package test.client;
 
import java.net.HttpURLConnection;
import java.net.URL;
 
import org.codehaus.xfire.client.Client;
 
public class ExchangeServiceClient {
	
	public double RMB2Dollar(double RMB)
	{
		try{
			String wsdl = "http://[ip]:[port]/[yourProjectName]/services/ExchangeService?wsdl";
			URL url = new URL(wsdl);
			HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
			httpConnection.connect();
			Client client = new Client(httpConnection.getInputStream(),null);			
			Object []results = client.invoke("RMB2Dollar", new Object[]{RMB});
			return (Double)results[0];
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ExchangeServiceClient client = new ExchangeServiceClient();
		System.out.println("invoking service : ExchangeService results="+client.RMB2Dollar(100.0));
	}
 
}
 

 

貌似这种调用更加简单哦!

 

Client client = new Client(new URL(URL));
Object[] results = client.invoke("say", new Object[]{passPort});
 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值