All Products
Search
Document Center

Generic Invocation

Last Updated: Aug 17, 2020

In RPC, an app can call an RPC service based on the interface name and method name without depending on the JAR packages of the service provider.

Generic interface

  1. public interface GenericService {
  2. /**
  3. * For generic calls, the method arguments can only be basic data type,
  4. * or the method arguments exist in the current ClassLoader.
  5. *
  6. * @param methodName: the name of the calling method.
  7. * @param args: the arguments list for the call.
  8. * @return: the calling result.
  9. * @throws com.alipay.sofa.rpc.core.exception.GenericException: the exception for the call.
  10. */
  11. Object $invoke(String methodName, String[] argTypes, Object[] args) throws GenericException;
  12. /**
  13. * Supports generic calls when the argument type cannot be loaded in the ClassLoader. Non-JDK classes are serialized to the GenericObject type.
  14. *
  15. * @param methodName: the name of the calling method.
  16. * @param argTypes: the argument type.
  17. * @param args: arguments. The type can be GenericObject.
  18. * @return: the result. It is the GenericObject type.
  19. * @throws com.alipay.sofa.rpc.core.exception.GenericException
  20. */
  21. Object $genericInvoke(String methodName, String[] argTypes, Object[] args)
  22. throws GenericException;
  23. /**
  24. * Supports generic calls when the argument type cannot be loaded in the ClassLoader.
  25. *
  26. * @param methodName: the name of the calling method.
  27. * @param argTypes: the argument type
  28. * @param args: arguments. The type can be GenericObject.
  29. * @param context GenericContext
  30. * @return: the result. It is the GenericObject type.
  31. * @throws com.alipay.sofa.rpc.core.exception.GenericException
  32. */
  33. Object $genericInvoke(String methodName, String[] argTypes, Object[] args,
  34. GenericContext context) throws GenericException;
  35. /**
  36. * Supports generic calls when the argument type cannot be loaded in the ClassLoader, and returns T type.
  37. *
  38. * @param methodName: the name of the calling method.
  39. * @param argTypes: the argument type.
  40. * @param args: arguments. The type can be GenericObject.
  41. * @return: the result. It is the T type.
  42. * @throws com.alipay.sofa.rpc.core.exception.GenericException
  43. */
  44. <T> T $genericInvoke(String methodName, String[] argTypes, Object[] args, Class<T> clazz) throws GenericException;
  45. /**
  46. * Supports generic calls when the argument type cannot be loaded in the ClassLoader.
  47. *
  48. * @param methodName: the name of the calling method.
  49. * @param argTypes: the argument type
  50. * @param args: arguments. The type can be GenericObject.
  51. * @param clazz: the return type.
  52. * @param context GenericContext
  53. * @return: the result. It is the T type.
  54. * @throws com.alipay.sofa.rpc.core.exception.GenericException
  55. */
  56. <T> T $genericInvoke(String methodName, String[] argTypes, Object[] args, Class<T> clazz, GenericContext context) throws GenericException;
  57. }

Note:

  • $invoke applies when the argument type exists in the ClassLoader of the current app.
  • $genericInvoke applies when the argument type does not exist in the ClassLoader of the current app.

Examples

Spring XML configuration:

  1. <! -- Reference TR service -->
  2. <sofa:reference interface="com.alipay.sofa.rpc.api.GenericService" id="genericService">
  3. <sofa:binding.tr>
  4. <sofa:global-attrs generic-interface="com.alipay.test.SampleService"/>
  5. </sofa:binding.tr>
  6. </sofa:reference>

Java code:

  1. /**
  2. * Java Bean
  3. */
  4. public class People {
  5. private String name;
  6. private int age;
  7. // getters and setters
  8. }
  9. /**
  10. * The interface provided by the service provider.
  11. */
  12. interface SampleService {
  13. String hello(String arg);
  14. People hello(People people);
  15. }
  1. /**
  2. * The test class on the consumer side.
  3. */
  4. public class ConsumerClass {
  5. GenericService genericService;
  6. public void do() {
  7. // 1. $invoke applies when the argument type exists in the ClassLoader of the current app.
  8. genericService.$invoke("hello", new String[]{ String.class.getName() }, new Object[]{"I'm an arg"});
  9. // 2. $genericInvoke applies when the argument type does not exist in the ClassLoader of the current app.
  10. // 2.1 Construct an object.
  11. GenericObject genericObject = new GenericObject("com.alipay.sofa.rpc.test.generic.bean.People"); // Specify the full path class name in the constructor.
  12. genericObject.putField("name", "Lilei"); // Call putField to specify the field value.
  13. genericObject.putField("age", 15);
  14. // 2.2 Perform a call without specifying the return type. The return type is GenericObject.
  15. Object obj = genericService.$genericInvoke("hello", new String[]{"com.alipay.sofa.rpc.test.generic.bean.People"}, new Object[] { genericObject });
  16. Assert.assertTrue(obj.getClass() == GenericObject.class);
  17. // 2.3 Perform a call with a specified return type.
  18. People people = genericService.$genericInvoke("hello", new String[]{"com.alipay.sofa.rpc.test.generic.bean.People"}, new Object[] { genericObject }, People.class);
  19. // 3. Use generic calls in LDC architecture.
  20. // 3.1 Construct GenericContext object
  21. AlipayGenericContext genericContext = new AlipayGenericContext();
  22. genericContext.setUid("33");
  23. // 3.2 Perform invocation
  24. People people = genericService.$genericInvoke("hello", new String[] {"com.alipay.sofa.rpc.test.generic.bean.People"}, new Object[] { genericObject }, People.class, genericContext);
  25. }

Note

The invocation of $genericInvoke(String methodName, String[] argTypes, Object[] args) will serialize the classes excluded in the following packages to GenericObject.

  1. "com.sun",
  2. "java",
  3. "javax",
  4. "org.ietf",
  5. "org.ogm",
  6. "org.w3c",
  7. "org.xml",
  8. "sunw.io",
  9. "sunw.util"