Hessian with Dependency Injection
Resin 3.1

Documentation
Examples
Changes

Quercus
Database
Amber
EJB
SOA/ESB
IoC
JMS
Servlet
JMX
Hessian
Security

Hessian Serialization
Hessian Addition
Service Addition
Hessian with DI
Burlap Addition
custom-protocol
Service Addition
Hessian
Burlap Addition

Using Hessian with Dependency Injection pattern creates services which are simpler, protocol-independent and more easily tested. The Dependency Injection pattern (aka inversion of control) gives Resin the responsibility of configuring and assembling the service, protocol, and client.

Demo

Files in this tutorial

WEB-INF/classes/example/GreetingAPI.javaInterface for the greeting service.
WEB-INF/classes/example/GreetingImpl.javaThe service implementation as a Java object
WEB-INF/resin-web.xmlConfigures the environment
WEB-INF/classes/example/GreetingClientServlet.javaClient Servlet

Service Implementation

GreetingAPI.java
package example;

public interface GreetingAPI {
  public String hello();
}

Service Implementation

The Greeting implementation is a plain Java class that implements the MatchService API. Making the service a plain class offers a number of advantages:

  • Simplicity: It can concentrate on its business logic because it doesn't need to implement any protocol-dependent code.
  • Independence: It can be reused more easily because it isn't tied to a distributed framework (e.g. in contrast to EJB).
  • Testability: It is more easily tested since the test harness doesn't need to implement the protocol or its stubs. The harness can just test the service as a plain old Java object.
GreetingImpl.java
package example;

public class GreetingImpl implements GreetingAPI {
  private String _greeting = "Hello, world";

  public void setGreeting(String greeting)
  {
    _greeting = greeting;
  }

  public String greeting()
  {
    return _greeting;
  }
}

Configuration using Dependency Injection

resin-web.xml
<resource jndi-name="service/greeting"
          type="example.GreetingImpl">
  <init>
    <greeting>Hello, web.xml</greeting>
  </init>
</resource>

<servlet url-pattern="/hessian/greeting"
         servlet-class="com.caucho.hessian.server.HessianServlet">
  <init>
    <home>\${jndi("service/greeting")}</home>
    <home-api>example.GreetingAPI</home-api>
  </init>
</servlet>

Client

Configuring the client servlet with Dependency Injection allows for a simpler and more general client. The following client can use any proxy/stub which implements the GreetingAPI without change, for example:

  • Hessian proxy
  • Burlap proxy
  • EJB local object stub
  • JMX proxy
  • Java bean instance

Using the Dependency Injection patter, the servlet doesn't care how the proxy is implemented, or how the greeting service is discovered.

GreetingClientServlet.java
public class GreetingClientServlet extends GenericServlet {
  private String _name = "generic";
  private GreetingAPI _greeting;

  public void setName(String name)
  {
    _name = name;
  }

  public void setGreeting(GreetingAPI greeting)
  {
    _greeting = greeting;
  }

  public void service(ServletRequest req, ServletResponse res)
    throws IOException, ServletException
  {
    PrintWriter out = res.getWriter();

    out.println(_name + ": " + _greeting.greeting());
  }
}

Hessian Client using Dependency Injection

The following example stores a <reference> to the Hessian proxy in JNDI for the service. The servlet configuration looks up the Hessian proxy in JNDI.

resin-web.xml
<reference jndi-name="client/greeting"
           factory="com.caucho.hessian.client.HessianProxyFactory">
  <init-param url="\${app.getURL()}/hessian/greeting"/>
  <init-param type="example.GreetingAPI"/>
</reference>

<servlet-mapping url-pattern="/client/greeting"
                 servlet-class="example.GreetingClientServlet">
  <init>
    <name>Hessian Client</name>
    <greeting>\${jndi("client/greeting")}</greeting>
  </init>
</servlet-mapping>

JMX Configuration

The following JMX configuration shows the flexibility of the Dependency Injection pattern. With no changes to either the Greeting service or its client, the Greeting service can change to a JMX bean used by and the client to use a JMX proxy.

resin-web.xml
<resource mbean-name="type=Greeting"
          mbean-interface="example.GreetingAPI"
          type="example.GreetingImpl">
  <init>
    <greeting>Hello, web.xml</greeting>
  </init>
</resource>

<servlet-mapping url-pattern="/client/greeting"
                 servlet-class="example.GreetingClientServlet">
  <init>
    <name>JMX Client</name>
    <greeting>\${jndi("mbean:type=Greeting")}</greeting>
  </init>
</servlet-mapping>

Demo


Service Addition
Hessian
Burlap Addition
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin ® is a registered trademark, and Quercustm, Ambertm, and Hessiantm are trademarks of Caucho Technology.