RPC 适配器库概述

Web-remoting 是一种模式,它支持 JavaScriptTM 或客户机端代码直接调用服务器端的逻辑。此模式能够调用 JavaScript 中的 JavaTM 方法。它是通过执行 JSON-RPC 调用来调用的。最常见的用法是通过 XmlHttpRequest 来异步调用。数据以 JSON 格式在服务器与客户机之间传输。因此,此模式实质上是一项 JSON Web Service。

RPC 适配器简介

IBM® 实现的 Web remoting 称为 IBM 的 RPC 适配器。RPC 适配器用来帮助开发者轻松快捷地创建基于命令的服务,这种创建服务的方式是对 Ajax 应用程序和其他轻量级客户机的编程样式的补充。RPC 适配器是作为通用 Servlet 实现的,它为已注册的 JavaBean 提供了一个 HTTP 接口。

RPC 适配器
RPC 适配器目前支持两种 RPC 协议:

HTTP RPC

使用 HTTP RPC 协议时,使用带有查询参数或表单参数的 URL 来进行调用。RPC 适配器将拦截 URL 并将它反序列化,以获得服务名称、方法名称和输入参数。通过使用这些细腻下,RPC 适配器将调用相匹配的 JavaBean 的相应方法。

例如,客户机可以在下列 JavaBean 上调用 test.Example.getEcho("Hello world")

package test; public class Example { public String getEcho(String message) { return(message); } } 按如下所示通过 HTTP RPC 使用 HTTP GET 来调用 test.Example.getEcho("Hello world")

GET /contextRoot/RPCAdapter/httprpc/example/echo?p0=Hello%20world HTTP/1.1

必须在已经正确地将 RPC 适配器配置为与您的应用程序配合使用之后才能执行所有这些调用。为了实现这种正确配置,需要将 RPCAdapter Java 归档 (JAR) 文件添加至 WAR 文件的 WEB-INF/lib 目录,并在 web.xml 文件中生成下列条目。 <servlet> <display-name>RPCAdapter</display-name> <servlet-name>RPCAdapter</servlet-name> <servlet-class>com.ibm.websphere.rpcadapter.RPCAdapter</servlet-class> </servlet> <servlet-mapping> <servlet-name>RPCAdapter</servlet-name> <url-pattern>/RPCAdapter</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>RPCAdapter</servlet-name> <url-pattern>/RPCAdapter/*</url-pattern> </servlet-mapping> 指定 WEB-INF 目录中的 RpcAdapterConfig.xml 文件。此文件用来配置已导出的服务。在这种情况下,样本 RpcAdapterConfig.xml 文件类似于以下示例: <?xml version="1.0" encoding="UTF-8"?> <rpcAdapter> <default-format>xml</default-format> <services> <pojo> <name>样本</name> <implementation>test.pojo.Sample</implementation> </pojo> </services> </rpcAdapter> 派生的 HTTP RPC API 将第一个自变量的名称指定为 p0。此缺省行为是由于 Java 反射 API 中当前存在的局限性而造成的。 可以使用 Bean 描述符信息或者通过在 RpcAdapterConfig.xml 文件中指定参数名称来指定有意义的参数名称。例如,查看此 Bean 中的 getEcho() package test; import com.ibm.websphere.rpcadapter.SelfBeanInfo; public class Example2 implements SelfBeanInfo { public String getEcho(String message) { return(message); } public static String[][] getBeanDescriptorInfo() { String [][] mds = { {"method", "getEcho", "Echoes the input String.", "GET", "message", "String to be echoed."}, }; return(mds); } } 可以按如下所示来调用以上示例: GET /contextRoot/httprpc/example/echo?message=Hello%20world HTTP/1.1 package test; public class Example2 { public String getEcho(String message) { return(message); } } 可以使用以下 RpcAdapterConfig.xml 按如下所示来调用以上示例: GET /contextRoot/httprpc/example/echo?message=Hello%20world HTTP/1.1

<?xml version="1.0" encoding="UTF-8"?> <rpcAdapter> <default-format>xml</default-format> <services> <pojo> <name>example</name> <implementation>test.pojo.Example2</implementation> <methods filter="whitelisting"> <method> <name>echo</name> <description>echos the message</description> <parameters> <parameter> <name>message</name> <description>Contains the message to be echo'd.</description> <!--optional--> </parameter> </parameters> </method> </methods> </pojo> </services> </rpcAdapter>

配置文件中的 default-format 元素指定在请求中未指定格式的情况下要使用的缺省格式。此格式可以为 XML 或 JSON。

JSON-RPC

使用 JSON-RPC 协议时,使用 JSON 对象来进行方法调用。生成的响应也是一个 JSON 对象。可以通过 Dojo JSON-RPC API 来访问已注册的 JavaBean: var example = new dojo.rpc.JsonService("/contextRoot/RPCAdapter/jsonrpc/example"); example.getEcho('Hello world').addCallback(exampleCallback); function exampleCallback(result) { // response is available as result.getEchoReturn.String }

指定 Bean 描述符信息

在 RPC 适配器中,使用 Bean 描述符信息来指定有关可用的 JavaBean 的信息。如果未指定 Bean 描述符,那么将通过反射来获得此信息。可以采用两种方式来指定 Bean 描述符信息。请考虑以下所示的名为 Sample 的 JavaBeans。 package test.pojo; import java.util.List; import java.util.Vector; /** * Example showing custom Bean access specified with a separate BeanInfo class. * This sample is instructive for cases where the Bean class cannot be modified. * * @see test.pojo.SampleBeanInfo * @see test.pojo.SampleNoBeanInfo * @see test.pojo.SampleSelfBeanInfo */ public class Sample { /** * Echoes the input String. This stateless operation works correctly * regardless of the setting of the bean descriptor <code>oneInstancePerUser</code>. * * @param message String to be echoed. */ public String echo(String message) { return(message); } public String echoNoArg() { return("Hello World"); } public boolean echoBoolean(boolean b) { return(b); } public boolean echoIntegerBean(boolean b) { return(b); } List _values = new Vector(); /** * Adds a value to a list that can be retrieve with the <code>getValues</code> method. * This stateful scenario requires that the bean descriptor <code>oneInstancePerUser</code> * be set to true. * * @param value String added to the list. */ public void addValue(String value) { _values.add(value); } /** * Returns the cumulative list of values added with the <code>addValue</code> method. * This stateful scenario requires that the bean descriptor <code>oneInstancePerUser</code> * be set to true. * * @return List of Strings previously added to the list. */ public List getValues() { return(_values); } } 可以创建称为 SampleBeanInfo 的 BeanInfo 对象来描述 Sample JavaBeans。请注意 BeanInfo 类是如何将 SimplerBeanInfo 作为它的父代的。还应注意,您需要在构造函数中调用 setBeanClass 并且将 Sample.class 作为参数。 package test.pojo; import com.ibm.websphere.rpcadapter.SimplerBeanInfo; /** * Example BeanInfo class showing declarative-type descriptor information * supported by the RPC adapter. * * @see test.pojo.Sample */ public class SampleBeanInfo extends SimplerBeanInfo { /** * SimplerBeanInfo uses Java Reflection; must set the Bean class first. */ public SampleBeanInfo() { setBeanClass(Sample.class); } public String[][] getBeanDescriptorInfo() { String [][] mds = { {"bean", "oneInstancePerUser", "true"}, {"method", "echo", "Echoes the input String.", "GET", "message", "String to be echoed."}, {"method", "echoNoArg", "Echoes Hello World.", "GET" }, {"method", "addValue", "Adds a value to a list.", "GET", "value", "value to add"}, {"method", "getValues", "Returns the list.", "GET"}, {"method", "echoBoolean", "Echoes a boolean value.", "GET", "b", "Boolean to be echoed."}, }; return(mds); } } 还可以使用另一种方法来指定 Bean 描述符,即,将 Bean 描述符信息包括在 Bean 类中。注意,Bean 类将实现 SelfBeanInfo 接口。它应该还具有一个静态方法 getBeanDescriptorInfo(),用于返回有关 JavaBeans 的信息。 package test.pojo; import java.util.List; import java.util.Vector; import com.ibm.websphere.rpcadapter.SelfBeanInfo; /** * Example showing custom Bean access specified with embedded descriptive * information. This sample is instructive for cases where the Bean can be * modified. * * @see test.pojo.SampleNoBeanInfo * @see test.pojo.Sample */ public class SampleSelfBeanInfo implements SelfBeanInfo { /** * Echoes the input String. This stateless operation works correctly * regardless of the setting of the bean descriptor <code>oneInstancePerUser</code>. * * @param message String to be echoed. */ public String echo(String message) { return(message); } List _values = new Vector(); /** * Adds a value to a list that can be retrieve with the <code>getValues</code> method. * This stateful scenario requires that the bean descriptor <code>oneInstancePerUser</code> * be set to true. * * @param value String added to the list. */ public void addValue(String value) { _values.add(value); } /** * Returns the cumulative list of values added with the <code>addValue</code> method. * This stateful scenario requires that the bean descriptor <code>oneInstancePerUser</code> * be set to true. * * @return List of Strings previously added to the list. */ public List getValues() { return(_values); } public static String[][] getBeanDescriptorInfo() { String [][] mds = { {"bean", "oneInstancePerUser", "true"}, {"method", "echo", "Echoes the input String.", "GET", "message", "String to be echoed."}, {"method", "addValue", "Adds a value to a list.", "GET", "value", "value to add"}, {"method", "getValues", "Returns the list.", "GET"}, }; return(mds); } } 还有一个重要事项要注意,如果您不想使用 BeanInfo,那么类路径中就不应包含 BeanInfo 类。这是因为 RPC 适配器通过将 BeanInfo 添加至 POJO 类,然后尝试首先装入该信息来检查 BeanInfo 类。无论您是否使用 Bean 描述符,都还需要注册您需要在 RPCAdapterConfig.xml 中呈示的对象。

软件包结构

   此库在独立版本中分发:
 
   独立软件包

       使用以下方式组织此软件包: 对于 WebSphere® Application Server V8.5
<install_root>/optionalLibraries/web2mobile/rpcAdapter/RPCAdapter.jar
打包为 Java 归档 (JAR) 格式、用于实现 API 的 Java 类文件。



使用条款 | 反馈