Using remote services

**Accessing and publishing RMI services
**Using Hessian and Burlap services
**Using Spring's HTTP invoker
**Using Spring to develop web services

 

Imagine us being stranded on a desolate island, it sounds like a dream come true. After all, who doesn't want to be alone on the beach, blissfully ignoring the hustle and bustle of the outside world? But on a desert island, we can't always enjoy pina coladas and sunbathes, and even if we could enjoy such a peaceful seclusion, it wouldn't take long before we were hungry, bored, and alone. In times like these, we can only live on coconuts and fish caught with a fork. We still need food, new clothes, and other supplies after all. And if we can't get in touch with other people, soon we'll be talking to volleyball! Many of the apps we develop are like deserted islands. On the surface, they may appear to be self-sufficient, but in reality, they may also need to cooperate with other systems, both inside and outside the organization. For example, a purchasing system needs to communicate with a manufacturer's supply chain system; a company's human resources system may need to integrate a payroll system; or a payroll system needs to communicate with external systems such as printing and mailing payroll. In either case, our application needs to interact with other systems and access their services remotely. As a Java developer, we have a variety of remote invocation techniques at our disposal, including:

Remote Method Invocation (RMI);
Caucho's Hessian and Burlap;
Spring's HTTP-based remote services;
Web Services using JAX-RPC and JAX-WS.

Regardless of which remote invocation technology we choose, Spring provides extensive support for accessing and creating remote services using these different technologies. In this chapter, we will learn how Spring simplifies and improves these remote invocation services. But first, let's take a brief look at how remoting works in Spring

 

1.1 Overview of Spring remote invocation

 

 

No matter which remote invocation model you choose, we'll find that Spring provides support for a consistent style. This means that once we understand how to configure Spring to use one of these models, we will have a very low learning curve if we decide to use the other. In all models, services are configured into our applications as Spring-managed beans. This is achieved through a proxy factory bean that can assemble remote services into properties of other beans as if they were local objects. Figure 15.2 shows how this works.

 

1.2 Publishing remote services using Hessian and Burlap

  Hessian and Burlap are two lightweight HTTP-based remote service solutions provided by Caucho Technology. They all aim to simplify web services with the simplest possible APIs and communication protocols.

You might be wondering why Caucho has two solutions to the same problem. Hessian and Burlap are two sides of the same coin, but each solution serves a slightly different purpose.

  Hessian, like RMI, uses binary messages for client-server interaction. But unlike other binary remote invocation technologies (such as RMI), its binary messages can be ported to other non-Java languages, including PHP, Python, C++, and C#. Burlap is an XML-based remote invocation technology, which makes it portable to any language capable of parsing XML. Because it is based on XML, Burlap is more readable than Hessian's binary format. But unlike other XML-based remote technologies (such as SOAP or XML-RPC), Burlap's message structure is as simple as possible and does not require additional external definition languages ​​(such as WSDL or IDL).

  You might be wondering how to choose between Hessian and Burlap. For the most part, they are the same. The only difference is that Hessian's messages are binary, while Burlap's messages are XML. Since Hessian's messages are binary, it has more bandwidth advantages. But if we care more about readability (eg for debugging purposes) or if our application needs to interact with a language that doesn't have a Hessian implementation, then Burlap's XML messages would be a better choice.

 

Ability to export beans using Hessian and Burlap

We just need to write a class that inherits com.caucho.hessian.server.HessianServlet and make sure that all service methods are public (in Hessian, all public methods are considered service methods). Because the Hessian service is easy to implement, Spring doesn't do much to simplify the Hessian model. But when used with Spring, Hessian services can take advantage of the Spring framework in various ways, which pure Hessian services do not have. Including the use of Spring's AOP to provide system-level services for Hessian services, such as declarative transactions.

HessianServiceExporter performs the same function for Hessian services as RmiServiceExporter performs for RMI services: it publishes POJO's public methods as methods of Hessian services. However, as shown in Figure 15.6, its implementation is different from RmiServiceExporter publishing POJOs as RMI services.

 

HessianServiceExporter(稍后会有更详细的介绍)是一个Spring MVC控制器,它接收Hessian请求,并将这些请求转换成对被导出POJO的方法调用。在如下Spring的声明中,HessianServiceExporter会把spitterService bean导出为Hessian服务

 

 

 

 正如RmiServiceExporter一样,service属性的值被设置为实现了这个服务的bean引用。在这里,它引用的是spitterServicebean。serviceInterface属性用来标识这个服务实现了SpitterService接口。与RmiServiceExporter不同的是,我们不需要设置serviceName属性。在RMI中,serviceName属性用来在RMI注册表中注册一个服务。而Hessian没有注册表,因此也就没必要为Hessian服务进行命名。

 

配置Hessian控制器

 

RmiServiceExporter和HessianServiceExporter另外一个主要区别就是,由于Hessian是基于HTTP的,所以HessianSeriviceExporter实现为一个Spring MVC控制器。这意味着为了使用导出的Hessian服务,我们需要执行两个额外的配置步骤:

在web.xml中配置Spring的DispatcherServlet,并把我们的应用部署为 Web应用;

在Spring的配置文件中配置一个URL处理器,把Hessian服务的URL分发给对应的Hessian服务bean。

 

我们在第5章学习了如何配置Spring的DispatcherServlet和URL处理器,所以这些步骤看起来有些熟悉。首先,我们需要一个DispatcherServlet。还好,这个我们已经在Spittr应用的web.xml文件中配置了。但是为了处理Hessian服务,DispatcherServlet还需要配置一个Servlet映射来拦截后缀为“*.service”的URL

这样配置后,任何以“.service”结束的URL请求都将由DispatcherServlet处理,它会把请求传递给匹配这个URL的控制器。因此“/spitter.service”的请求最终将被hessianSpitterServicebean所处理(它实际上仅仅是一个SpitterServiceImpl的代理)。

那我们是如何知道这个请求会转给hessianSpitterSevice处理呢?我们还需要配置一个URL映射来确保DispatcherServlet把请求转给hessianSpitterService。如下的SimpleUrlHandlerMappingbean可以做到这一点

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325376225&siteId=291194637