调用对方接口方法之Hessian

调用对方接口方法之Hessian,第1张

调用对方接口方法之Hessian

两个系统交互,需要调用对方接口,我知道的有两种方法,一种是webservice,另一种是Hessian。
这里讲Hessian,Hessian比较简单。
在自己系统上,建立一个Hessian客户端,我取名HessianClient

import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;

public class HessianClient  {
	private static ClassService inter;
	
	private HessianClient() {
	}
	
	public static ClassService getInstance() throws MalformedURLException {
		String url = "http://ip地址 :port/xxx/method";    // 设置url
		HessianProxyFactory factory = new HessianProxyFactory();
		inter = (ClassService) factory.create(ClassService.class, url);  // ClassService必须是与接口对面的类同名,同时自己这边也要有以ClassService为名的接口
		if (inter == null) {
		inter = (ClassService) new HessianClient();
		}
		return inter;
	}
}
	
然后再建立名为ClassService的接口类
public interface ClassService{
	public String xxxMethod();      // 对面一定要有此接口方法
}

最后为此ClassService接口写实现方法
public class ClassServiceImpl implements ClassService{
	public String xxxMethod(){
		ClassService client = HessianClient.getInstance();
		return client.xxxMethod();
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5677891.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存