Strongly Typed Camel BeanBindings

The Apache Camel framework offers powerful mechanisms to determine which method of your beans to invoke. These are described here in the Camel documentation: http://camel.apache.org/bean-binding.html.

 

In practice, you have your beans defined with spring and declare their invocation in the Camel route definition either with „beanRef“:

  1. .beanRef("orderService", "doSomething")
or with „to“:
  1. .to("bean:orderService?method=doSomething")
This has some disadvantages, for which we want to suggest a utility classes as a solution:

  • Your IDE cannot find the usages of the method invocation of method „doSomething“
  • Your IDE cannot assist you during refactorings, e.g. when renaming the method or changing the signature
  • Neither your IDE nor the compiler can complain about typos in method names. You will find typos during runtime, not during compile-time.

 

Replacing the calls with process() is no solution, because you still want the Camel-comfort of the powerful bean-binding.

 

Using class BeanRef

To use strongly typed invocations in your camel route definitions, that avoid the disadvantages, you can use the utility class BeanRef, with a little more verbose syntax:

 

  1. // ...
  2. from("activemq:orders").to(doSomething()).to("file:confirm.txt")
  3. // ...
  4. private String doSomething() {
  5. return new BeanRef<OrderService>(OrderService.class) {
  6. protected void call() {
  7. bean.doSomething(null);
  8. }
  9. }.uri();
  10. }
This results in a single URI:
  1. from("activemq:orders").to("bean:orderService?method=doSomething").to("file:confirm.txt")
 

It does not matter, which parameter values you are using to invoke „doSomething“, but you need to pass the parameters of the required time, so that the compiler is satisfied. The real method of your bean will not be invoked with this parameter!

 

When your route calls more myOrderBookService than one method on the service bean, you can call the methods inside call() in their adequate sequence:

  1. from("activemq:orders").to(new BeanRef<OrderService>(OrderService.class) {
  2. protected void call() {
  3. bean.validate(bean.parse(null));
  4. bean.doSomething(null);
  5. }
  6. }.uris()).to(„file:confirm.txt)
This results in an array of URIs:
  1. from("activemq:orders").to(
  2. "bean:orderService?method=parse",
  3. "bean:orderService?method=validate",
  4. "bean:orderService?method=doSomething").to("file:confirm.txt")
When your service bean has a non-standard bean-name, you can use the second constructor of BeanRef to tell the bean-name you are using:
  1. private String doSomething() {
  2. return new BeanRef<SmartWebService>(
  3. SmartWebService.class, "myOrderBookService"){
  4. protected void call() {
  5. bean.doSomething (null);
  6. }
  7. }.uri();
  8. }
 

This produces the URI:

  1. "bean:myOrderBookService?method=doSomething"

How does it work?

During definition of the camel route, the call() method of the BeanRef-instance will be invoked with a proxy, that implements the interface of your service bean. The proxy just collects the method invocations and the uri()/uris() methods return the URI-strings of all method invocations.

 

This is he whole source of class BeanRef:

  1. package de.viaboxx.camel;
  2. import java.lang.reflect.*;
  3. import java.util.*;
  4. public abstract class BeanRef {
  5. private final Class beanType;
  6. private final String beanName;
  7. protected T bean;
  8. public BeanRef(Class beanType) {
  9. this.beanType = beanType;
  10. this.beanName = uncapitalize(beanType.getSimpleName());
  11. }
  12. public BeanRef(Class beanType, String beanName) {
  13. this.beanType = beanType;
  14. this.beanName = beanName;
  15. }
  16. public static String uncapitalize(String str) {
  17. int strLen;
  18. if (str == null || (strLen = str.length()) == 0) {
  19. return str;
  20. }
  21. int i = 0;
  22. while (i < str.length() && Character.isUpperCase(str.charAt(i))) {
  23. i++;
  24. }
  25. if (i == 0) {
  26. return str;
  27. } else {
  28. if (i > 1) i--;
  29. return new StringBuilder(strLen)
  30. .append(str.substring(0, i).toLowerCase())
  31. .append(str.substring(i))
  32. .toString();
  33. }
  34. }
  35. protected abstract void call() throws Throwable;
  36. public String[] uris() {
  37. Collector collector = evaluate();
  38. return toUris(collector);
  39. }
  40. private String[] toUris(Collector collector) {
  41. String[] uris = new String[collector.calls.size()];
  42. for (int i = 0; i < collector.calls.size(); i++) {
  43. uris[i] = toUri(collector, i);
  44. }
  45. return uris;
  46. }
  47. public String uri() {
  48. Collector collector = evaluate();
  49. if (collector.calls.size() != 1) {
  50. throw new RuntimeException(
  51. "uri() requires a single call, use uris() instead for " + Arrays.toString(toUris(collector)));
  52. }
  53. return toUri(collector, 0);
  54. }
  55. public String method() {
  56. Collector collector = evaluate();
  57. if (collector.calls.size() != 1) {
  58. throw new RuntimeException(
  59. "method() requires a single call, at " + Arrays.toString(toUris(collector)));
  60. }
  61. return collector.calls.get(0);
  62. }
  63. public String[] methods() {
  64. Collector collector = evaluate();
  65. return collector.calls.toArray(new String[collector.calls.size()]);
  66. }
  67. private Collector evaluate() {
  68. Collector collector = new Collector();
  69. //noinspection unchecked
  70. bean = (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
  71. new Class[]{beanType},
  72. collector);
  73. try {
  74. call();
  75. } catch (Throwable throwable) {
  76. throw new RuntimeException("Unexpected exception during configuration", throwable);
  77. }
  78. bean = null;
  79. return collector;
  80. }
  81. public String getBeanName() {
  82. return beanName;
  83. }
  84. private String toUri(Collector collector, int i) {
  85. return "bean:" + getBeanName() + "?method=" + collector.calls.get(i);
  86. }
  87. private static class Collector implements InvocationHandler {
  88. List calls = new LinkedList();
  89. public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
  90. calls.add(method.getName());
  91. return null;
  92. }
  93. }
  94. }
If you are using Groovy instead of Java, you can easily think of a variant of BeanRef using Closures, that avoid most of the syntax overhead of Java, if you want to invoke your beans that way.

Kommentare sind deaktiviert