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});
 

 

 

你可能感兴趣的:(.net,ide,Exchange)