Caucho Technology
  • resin 4.0
  • a simple service for resin remoting


    Writing a service for the Resin remoting as a plain-old Java object (POJO) eliminates protocol dependencies and simplifies service testing.

    Demo

    With the Resin remoting, services can be written as plain-old Java objects (POJOs) and made available to many different protocols using simple configuration changes.

    See Also

    Files in this tutorial

    FILEDESCRIPTION
    WEB-INF/classes/example/HelloService.javaInterface for the hello service.
    WEB-INF/classes/example/HelloServiceImpl.javaThe main service implementation.
    WEB-INF/resin-web.xmlConfigures the environment
    demo.jspClient JSP
    demo.phpClient PHP

    Introduction

    The Resin remoting is an infrastructure that allows a service to be exposed via many different service protocols. For example in this tutorial, there is a plain-old Java object (POJO) that implements a service and this service is made available using REST, SOAP, Hessian, and WebBeans. The service is implemented once and these protocols are activated with a few simple changes to the configuration file.

    Service Interface

    In this example, the service interface is for a simple Hello, World service. There is a single method, hello() that the service must implement and the client may invoke.

    HelloService.java
    package example;
    
    public interface HelloService {
      /**
       * Returns "hello, world".
       */
      public String hello();
    }
    

    Service Implementation

    The HelloService implementation is just a Java class that implements the HelloService API. It can optionally use EJB annotations like @Remote, @Stateless or @TransactionAttribute.

    HelloServiceImpl.java
    package example;
    
    public class HelloServiceImpl implements HelloService {
      /**
       * Returns "hello, world".
       */
      public String hello()
      {
        return "hello, world";
      }
    }
    

    Service configuration

    Services for Resin remoting are configured with the <servlet> tag. The implementation class is given as the servlet-class attribute. It is possible to allow access to the service within the same virtual machine by registering the service as a WebBeans singleton with <bean>.

    To expose the service as a Hessian service, use the <hessian> tag. Hessian is one of the protocols available for web services.

    Finally, a SOAP interface is available, using CXF or XFire.

    remoting service for hessian
    <web-app xmlns="http://caucho.com/ns/resin">
    
      <servlet-mapping url-pattern="/hello/hessian/*"
                       servlet-class="example.HelloServiceImpl">
        <protocol uri="hessian:"/>
      </servlet-mapping>
    
    </web-app>
    
    remote service for CXF
    <web-app xmlns="http://caucho.com/ns/resin">
    
      <servlet-mapping url-pattern="/hello/hessian/*"
                       servlet-class="example.HelloServiceImpl">
        <protocol uri="cxf:"/>
      </servlet-mapping>
    
    </web-app>
    
    service as WebBeans singleton
    <web-app xmlns="http://caucho.com/ns/resin">
    
      <bean class="example.HelloServiceImpl" name="vm"/>
    
    </web-app>
    

    Client configuration

    Resin also makes it easy to access services using the <remote-client> tag. This tag connects to a service using a URI of the form <protocol>:url=<location>. The example below shows just such a URL. The interface of the service is required. The <remote-client> tag creates a proxy client instance for the service and registers the proxy with WebBeans.

    <remote-client>
    <web-app xmlns="http://caucho.com/ns/resin">
    
      <remote-client interface="example.HelloService" name="hessian">
        <uri>hessian:url=${webApp.url}/hello/hessian/</uri>
      </remote-client>  
    
      <remote-client interface="example.HelloService" name="rest">
        <uri>rest:url=${webApp.url}/hello/rest/</uri>
      </web-service-client>
    
      <remote-client interface="example.HelloService" name="soap">
          <uri>xfire:url=${webApp.url}/hello/soap/</url>
      </remote-client>
    
    </web-app>
    

    JSP Client Script

    The client can now connect to the HelloService using any supported encoding simply by doing a WebBeans injection.

    demo.jsp
    <%@ page import="javax.webbeans.Named" %>
    <%@ page import="example.HelloService" %>
    <%!
    @Named("hessian") HelloService _hessianHello;
    @Named("rest") HelloService _restHello;
    @Named("soap") HelloService _soapHello;
    @Named("vm") HelloService _vmHello;
    %>
    <pre>
    From Hessian: <%= _hessianHello.hello() %>
    From REST: <%= _restHello.hello() %>
    From SOAP: <%= _soapHello.hello() %>
    From VM: <%= _vmHello.hello() %>
    </pre>
    
    From Hessian: hello, world
    From REST: hello, world
    From SOAP: hello, world
    From VM: hello, world
    

    PHP Client Script

    The client can now connect to the HelloService using PHP with the java_bean() method.

    demo.php
    <?php
    $hessian = java_bean("hessian");
    $rest = java_bean("rest");
    $soap = java_bean("soap");
    $vm = java_bean("vm");
    ?>
    <pre>
    From Hessian: <?= $hessian->hello() ?>
    From REST: <?= $rest->hello() ?>
    From SOAP: <?= $soap->hello() ?>
    From VM: <?= $vm->hello() ?>
    </pre>
    
    From Hessian: hello, world
    From REST: hello, world
    From SOAP: hello, world
    From VM: hello, world
    

    Demo


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