<document>
<localtoc/> <s1>

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.

</s1>

- Resin remoting for a full description.

<deftable>
File Description
Interface for the hello service.
The main service implementation.
Configures the environment
Client JSP
Client PHP
</deftable>

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.

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.

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

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

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

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.

<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> <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> <web-app xmlns="http://caucho.com/ns/resin"> <bean class="example.HelloServiceImpl" name="vm"/> </web-app>

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.

<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>

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

<%@ 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> <results> From Hessian: hello, world From REST: hello, world From SOAP: hello, world From VM: hello, world </results>

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

<?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> <results> From Hessian: hello, world From REST: hello, world From SOAP: hello, world From VM: hello, world </results>
</document>