清单 3. 远程服务
public class RemoteServiceImpl implements IService
{
/* get the service outputs */
/* outputs are from the web service running in the location */
/* mentioned in the serviceURI */
public ….. getFunction()
{
adapter.examples.Function service = null;
String serviceURI = "http://……../Function/";
String wsdlURI = "http://localhost:8080/Function/";
Try
{
// init the lookup
WebServiceLookup lookup = (WebServiceLookup)
……….
// get the instance of the Web services interface
// from the lookup
service = (adapter.examples.Function)
lookup.lookup(wsdlURI,
adapter.examples.Function.class,
serviceURI);
}
catch (Exception e)
{
e.printStackTrace();
}
// now, call the methods on your Web services interface
return service.getFunction();
}
结束语
显然,使用 Web 服务的静态存根与前期绑定是 Web 服务调用的最简单的形式。但是简单化也带来了明显的缺点。与紧耦合方法不同,使用本文提出的适配器方法将给您留下可高度重用和可配置的 Web 服务代码。由于 Web 服务 调用全部通过部署了服务描述符的公共适配器来引导,因此您能够动态地决定调用什么服务——在部署代码时或在运行时。
同样,那些可能寻找商业产品作为 Web 服务松耦合调用定制解决方案的组织应该研究企业服务总线(Enterprise Service Bus,ESB)技术,该技术提供了类似于上面所引述的功能的选项来连接企业内部及跨企业的可作为 Web 服务的应用程序,并具备一套功能来管理并监视已连接应用之间的交互。
