📄 index.xtp
字号:
<document> <header> <product>resin</product> <title>A Simple Service for Resin Remoting</title> <description> <p>Writing a service for the Resin remoting as a plain-old Java object (POJO) eliminates protocol dependencies and simplifies service testing. </p> </description> <type>tutorial</type> <tutorial-startpage>demo.jsp</tutorial-startpage> </header> <body> <localtoc/> <s1><p>With the Resin remoting, services can be written as plain-old Java objects (POJOs) and made available to many different protocolsusing simple configuration changes.</p></s1><s1 title="See Also"><ul><li><a href="../../doc/resin-remoting.xtp">Resin remoting</a> for a fulldescription.</li></ul></s1><s1 title="Files in this tutorial"><deftable><tr> <th>File</th> <th>Description</th></tr><tr> <td><viewfile-link file="WEB-INF/classes/example/HelloService.java"/></td> <td>Interface for the hello service.</td></tr><tr> <td><viewfile-link file="WEB-INF/classes/example/HelloServiceImpl.java"/></td> <td>The main service implementation.</td></tr><!--<tr> <td><viewfile-link file="WEB-INF/classes/example/HelloResult.java"/></td> <td>The result object</td></tr>--><tr> <td><viewfile-link file="WEB-INF/resin-web.xml"/></td> <td>Configures the environment</td></tr><tr> <td><viewfile-link file="demo.jsp"/></td> <td>Client JSP</td></tr><tr> <td><viewfile-link file="demo.php"/></td> <td>Client PHP</td></tr></deftable></s1><s1 title="Introduction"><p></p><figure src="soa.png"/><p>The Resin remoting is an infrastructure thatallows a service to be exposed via many different service protocols.For example in this tutorial, there is a plain-old Java object (POJO) thatimplements a service and this service is made available using REST, SOAP,Hessian, and WebBeans. The service is implemented once and these protocolsare activated with a few simple changes to the configuration file.</p></s1><s1 title="Service Interface"><p>In this example, the service interface is for a simple Hello, World service.There is a single method, <code>hello()</code> that the service must implementand the client may invoke.</p><example title="HelloService.java">package example;public interface HelloService { /** * Returns "hello, world". */ public String hello();}</example></s1><s1 title="Service Implementation"><p>The HelloService implementation is just a Java class that implementsthe HelloService API. It can optionally use EJB annotations like @Remote,@Stateless or @TransactionAttribute.</p><example title="HelloServiceImpl.java">package example;public class HelloServiceImpl implements HelloService { /** * Returns "hello, world". */ public String hello() { return "hello, world"; }}</example></s1><s1 title="Service configuration"><p>Services for Resin remoting are configured with the <servlet>tag. The implementation class is given asthe <code>servlet-class</code> attribute.It is possible to allow access to the service within the same virtual machineby registering the service as a WebBeans singleton with <bean>.</p><p>To expose the service as a Hessian service, use the <hessian> tag.Hessian is one of the <em>protocols</em> available for web services.</p><!--<p>Here a REST interface is also exposed. In thisexample, the REST interface uses the defaultquery-based binding that interprets URLs of the form<url>http://www.foo.com/?method=myMethod&arg1=foo&arg2=bar</url>.to be a call to the method "myMethod" with two arguments,"foo" and "bar". Other custom bindings are possible, includingfor example a path-based binding that with URLs of the form<url>http://www.foo.com/myMethod/foo/bar</url>.The <url-pattern> child tag specifies the base URL for the REST interface.</p>--><p>Finally, a SOAP interface is available, using CXF or XFire.</p><example title="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></example><!--<example title="removing service for rest"><web-app xmlns="http://caucho.com/ns/resin"> <servlet-mapping url-pattern="/hello/hessian/*" servlet-class="example.HelloServiceImpl"> <protocol type="rest"/> </servlet-mapping></web-app></example>--><example title="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></example><example title="service as WebBeans singleton"><web-app xmlns="http://caucho.com/ns/resin"> <bean class="example.HelloServiceImpl" name="vm"/></web-app></example></s1><s1 title="Client configuration"><p>Resin also makes it easy to access services using the <remote-client> tag. This tag connects to a serviceusing a URI of the form <i><protocol>:url=<location></i>.The example below shows just such a URL. The interface of the serviceis required. The <remote-client> tagcreates a proxy client instance for the service and registers theproxy with WebBeans.</p><example title="<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></example></s1><s1 title="JSP Client Script"><p>The client can now connect to the HelloService using any supportedencoding simply by doing a WebBeans injection.</p><example title="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></example><results>From Hessian: hello, worldFrom REST: hello, worldFrom SOAP: hello, worldFrom VM: hello, world</results></s1><s1 title="PHP Client Script"><p>The client can now connect to the HelloServiceusing PHP with the <code>java_bean()</code> method.</p><example title="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></example><results>From Hessian: hello, worldFrom REST: hello, worldFrom SOAP: hello, worldFrom VM: hello, world</results></s1> </body></document>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -