he world of enterprise Java has an overwhelming array of choices for remoting: RMI, XML/SOAP, REST/JSON, etc. Each comes with its own industry-acknowledged strengths and weaknesses, such as complex setup (RMI) or performance overhead (XML/SOAP). A lesser known but very powerful and easy-to-use option is the Hessian binary web service protocol. The binary aspect of Hessian delivers excellent performance, and its native integration within Java allows you to expose remote services purely via a regular Java interface.
Hessian's most rewarding feature, though, is the ability to pass full Java POJO object graphs from one process to another easily without the overhead of XML marshalling, an unfortunate side effect of SOAP web services. Also, Hessian is implemented in a variety of different languages, so you theoretically can execute efficient binary remoting between Java and Python or Java and C# as well.
This article walks through using the Hessian protocol as an alternative to the more widely accepted RMI, XML/SOAP, and REST/JSON approaches for remote communication. It also explains how to integrate Spring and Hessian and then create Hessian components as simple Java objects.
The basic example from the Hessian web site illustrates this setup as follows:
public interface BasicAPI {
public String hello();
}
public class BasicService extends HessianServlet implements BasicAPI {
private String _greeting = "Hello, world";
public void setGreeting(String greeting)
{
_greeting = greeting;
}
public String hello()
{
return _greeting;
}
}
String url = "http://hessian.caucho.com/test/test";
HessianProxyFactory factory = new HessianProxyFactory();
BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url);
System.out.println("hello(): " + basic.hello());
If you have done the wsdl2java and java2wsdl Ant dance when exposing services via XML/SOAP, you surely will appreciate this straightforward path to implementing a Hessian-based web service. It's just 100% pure Java with some magic behind the scenes to tie it all together.
To demonstrate Spring's Hessian integration, this section walks you through setting up some common interfaces. First, use the following two classes to create a service that will allow saving/updating/deleting a simple Person POJO:
public class Person implements Serializable {
private Integer id;
private String firstName;
private String lastName;
private Calendar birthDate;
public Person() {
super();
}
public Person(String firstName, String lastName, Calendar birthDate) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
...getters and setters...
}
}
public interface IPersonService {
Person get(Integer id);
Person add(Person person);
void delete(Integer personId);
void update(Person person);
}
Both these classes are part of a common library shared on both endpoints.
@Service("personService")
public class PersonService implements IPersonService {
//map used for simulating data storage
private static Map<Integer,Person> people = new ConcurrentHashMap<Integer, Person>();
private static AtomicInteger keyGenerator = new AtomicInteger(0);
@Override
public Person add(Person person) {
if (person.getId() == null) {
Integer id = keyGenerator.addAndGet(1);
person.setId(id);
people.put(id,person);
return person;
} else {
throw new RuntimeException("Person already exists!");
}
}
@Override
public void update(Person person) {
if (person.getId() != null) {
people.put(person.getId(), person);
} else {
throw new RuntimeException("Person must be saved first!");
}
}
@Override
public void delete(Integer personId) {
people.remove(personId);
}
@Override
public Person get(Integer id) {
return people.get(id);
}
}
As you can see, this component does not need to extend the previously mentioned Hessian servlet. Also, the @Service Spring stereotype annotation eliminates the need to explicitly define the bean in an XML configuration. However, exposing the component via Spring does involve a series of steps.
First, in web.xml, add the Spring remoting servlet and map it to a URI:
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
The Spring remoting servlet will expect a new Spring application context file, which is usually called remoting-servlet.xml (separate from the usual beans.xml used by the rest of the application). In the context file, you have to expose your service via a HessianServiceExporter class:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.devx.hessian.server"/>
<bean name="/PersonService"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="personService" />
<property name="serviceInterface" value="com.dexv.hessian.IPersonService"/>
</bean>
</beans>
The context elements activate Spring's new annotation-based context support (to avoid unnecessary XML configuration) and tell it which package to use as the base for searching for components. The PersonService implementation is then injected into the HessianServiceExporter class instance.
If you run the server app (from the code sample attached to this article) and access the /remoting/PersonService URI via a regular browser, you will get a notice that Hessian is listening, although unable to process a typical browser request:
HTTP ERROR: 405
HessianServiceExporter only supports POST requests
RequestURI=/remoting/PersonService
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.devx.hessian.client" />
<bean id="personService"
class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://0.0.0.0:8080/remoting/PersonService" />
<property name="serviceInterface" value="com.dexv.hessian.IPersonService" />
</bean>
</beans>
When your service URL is defined, you can just inject the HessianProxyFactoryBean instance into your client application using regular dependency injection:
@Component("mainClient")
public class MainClient {
private IPersonService service = null;
@Autowired
public void setPersonService(@Qualifier("personService") IPersonService service) {
this.service = service;
}
}
Fortunately, this has been corrected in the latest milestone builds of Spring 3.0. The code samples attached to this article use the latest Hessian 4.0.1 and Spring 3.0 M4 builds.
| DevX is a division of Internet.com. © Copyright 2010 Internet.com. All Rights Reserved. Legal Notices |