PageRenderTime 26ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/components/camel-kura/src/main/docs/kura.adoc

https://gitlab.com/matticala/apache-camel
AsciiDoc | 320 lines | 250 code | 70 blank | 0 comment | 0 complexity | 6785b1ee81e38483bacfa6f3b5cbdd52 MD5 | raw file
  1. [[Kura-EclipseKuracomponent]]
  2. Eclipse Kura component
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. *Available as of Camel 2.15*
  5. This documentation page covers the integration options of Camel with the
  6. https://eclipse.org/kura/[Eclipse Kura] M2M gateway. The common reason
  7. to deploy Camel routes into the Eclipse Kura is to provide enterprise
  8. integration patterns and Camel components to the messaging M2M gateway.
  9. For example you might want to install Kura on Raspberry PI, then read
  10. temperature from the sensor attached to that Raspberry PI using Kura
  11. services and finally forward the current temperature value to your data
  12. center service using Camel EIP and components.
  13. [[Kura-KuraRouteractivator]]
  14. KuraRouter activator
  15. ^^^^^^^^^^^^^^^^^^^^
  16. Bundles deployed to the Eclipse Kura are usually
  17. http://eclipse.github.io/kura/doc/hello-example.html#create-java-class[developed
  18. as bundle activators]. So the easiest way to deploy Apache Camel routes
  19. into the Kura is to create an OSGi bundle containing the class extending
  20. `org.apache.camel.kura.KuraRouter` class:
  21. [source,java]
  22. -------------------------------------------------------
  23. public class MyKuraRouter extends KuraRouter {
  24. @Override
  25. public void configure() throws Exception {
  26. from("timer:trigger").
  27. to("netty-http:http://app.mydatacenter.com/api");
  28. }
  29. }
  30. -------------------------------------------------------
  31. Keep in mind that `KuraRouter` implements
  32. the `org.osgi.framework.BundleActivator` interface, so you need to
  33. register its `start` and `stop` lifecycle methods
  34. while http://eclipse.github.io/kura/doc/hello-example.html#create-component-class[creating
  35. Kura bundle component class].
  36. Kura router starts its own OSGi-aware `CamelContext`. It means that for
  37. every class extending `KuraRouter`, there will be a dedicated
  38. `CamelContext` instance. Ideally we recommend to deploy one `KuraRouter`
  39. per OSGi bundle.
  40. [[Kura-DeployingKuraRouter]]
  41. Deploying KuraRouter
  42. ^^^^^^^^^^^^^^^^^^^^
  43. Bundle containing your Kura router class should import the following
  44. packages in the OSGi manifest:
  45. [source,xml]
  46. --------------------------------------------------------------------------------------------------------------------
  47. Import-Package: org.osgi.framework;version="1.3.0",
  48. org.slf4j;version="1.6.4",
  49. org.apache.camel,org.apache.camel.impl,org.apache.camel.core.osgi,org.apache.camel.builder,org.apache.camel.model,
  50. org.apache.camel.component.kura
  51. --------------------------------------------------------------------------------------------------------------------
  52. Keep in mind that you don't have to import every Camel component bundle
  53. you plan to use in your routes, as Camel components are resolved as the
  54. services on the runtime level.
  55. Before you deploy your router bundle, be sure that you have deployed
  56. (and started) the following Camel core bundles (using Kura GoGo
  57. shell)...
  58. [source,xml]
  59. -----------------------------------------------------------------------------------------------------------
  60. install file:///home/user/.m2/repository/org/apache/camel/camel-core/2.15.0/camel-core-2.15.0.jar
  61. start <camel-core-bundle-id>
  62. install file:///home/user/.m2/repository/org/apache/camel/camel-core-osgi/2.15.0/camel-core-osgi-2.15.0.jar
  63. start <camel-core-osgi-bundle-id>
  64. install file:///home/user/.m2/repository/org/apache/camel/camel-kura/2.15.0/camel-kura-2.15.0.jar
  65. start <camel-kura-bundle-id>
  66. -----------------------------------------------------------------------------------------------------------
  67. ...and all the components you plan to use in your routes:
  68. [source,xml]
  69. -----------------------------------------------------------------------------------------------------
  70. install file:///home/user/.m2/repository/org/apache/camel/camel-stream/2.15.0/camel-stream-2.15.0.jar
  71. start <camel-stream-bundle-id>
  72. -----------------------------------------------------------------------------------------------------
  73. Then finally deploy your router bundle:
  74. [source,xml]
  75. ----------------------------------------------------------------------------------
  76. install file:///home/user/.m2/repository/com/example/myrouter/1.0/myrouter-1.0.jar
  77. start <your-bundle-id>
  78. ----------------------------------------------------------------------------------
  79. [[Kura-KuraRouterutilities]]
  80. KuraRouter utilities 
  81. ^^^^^^^^^^^^^^^^^^^^^
  82.  Kura router base class provides many useful utilities. This section
  83. explores each of them.
  84. [[Kura-SLF4Jlogger]]
  85. SLF4J logger
  86. ++++++++++++
  87. Kura uses SLF4J facade for logging purposes. Protected member `log`
  88. returns SLF4J logger instance associated with the given Kura router.
  89. [source,java]
  90. ----------------------------------------------
  91. public class MyKuraRouter extends KuraRouter {
  92. @Override
  93. public void configure() throws Exception {
  94. log.info("Configuring Camel routes!");
  95. ...
  96. }
  97. }
  98. ----------------------------------------------
  99. [[Kura-BundleContext]]
  100. BundleContext
  101. +++++++++++++
  102. Protected member `bundleContext` returns bundle context associated with
  103. the given Kura router.
  104. [source,java]
  105. ---------------------------------------------------------------------------------------------------------------
  106. public class MyKuraRouter extends KuraRouter {
  107. @Override
  108. public void configure() throws Exception {
  109. ServiceReference<MyService> serviceRef = bundleContext.getServiceReference(LogService.class.getName());
  110. MyService myService = bundleContext.getService(serviceRef);
  111. ...
  112. }
  113. }
  114. ---------------------------------------------------------------------------------------------------------------
  115. [[Kura-CamelContext]]
  116. CamelContext
  117. ++++++++++++
  118. Protected member `camelContext` is the `CamelContext` associated with
  119. the given Kura router.
  120. [source,java]
  121. ----------------------------------------------
  122. public class MyKuraRouter extends KuraRouter {
  123. @Override
  124. public void configure() throws Exception {
  125. camelContext.getStatus();
  126. ...
  127. }
  128. }
  129. ----------------------------------------------
  130. [[Kura-ProducerTemplate]]
  131. ProducerTemplate
  132. ++++++++++++++++
  133. Protected member `producerTemplate` is the `ProducerTemplate` instance
  134. associated with the given Camel context.
  135. [source,java]
  136. -----------------------------------------------------------
  137. public class MyKuraRouter extends KuraRouter {
  138. @Override
  139. public void configure() throws Exception {
  140. producerTemplate.sendBody("jms:temperature", 22.0);
  141. ...
  142. }
  143. }
  144. -----------------------------------------------------------
  145. [[Kura-ConsumerTemplate]]
  146. ConsumerTemplate
  147. ++++++++++++++++
  148. Protected member `consumerTemplate` is the `ConsumerTemplate` instance
  149. associated with the given Camel context.
  150. [source,java]
  151. --------------------------------------------------------------------------------------------------
  152. public class MyKuraRouter extends KuraRouter {
  153. @Override
  154. public void configure() throws Exception {
  155. double currentTemperature = producerTemplate.receiveBody("jms:temperature", Double.class);
  156. ...
  157. }
  158. }
  159. --------------------------------------------------------------------------------------------------
  160. [[Kura-OSGiserviceresolver]]
  161. OSGi service resolver
  162. +++++++++++++++++++++
  163. OSGi service resolver (`service(Class<T> serviceType)`) can be used to
  164. easily retrieve service by type from the OSGi bundle context.
  165. [source,java]
  166. -------------------------------------------------------
  167. public class MyKuraRouter extends KuraRouter {
  168. @Override
  169. public void configure() throws Exception {
  170. MyService myService = service(MyService.class);
  171. ...
  172. }
  173. }
  174. -------------------------------------------------------
  175. If service is not found, a `null` value is returned. If you want your
  176. application to fail if the service is not available, use
  177. `requiredService(Class)` method instead. The `requiredService` throws
  178. `IllegalStateException` if a service cannot be found.
  179. [source,java]
  180. ---------------------------------------------------------------
  181. public class MyKuraRouter extends KuraRouter {
  182. @Override
  183. public void configure() throws Exception {
  184. MyService myService = requiredService(MyService.class);
  185. ...
  186. }
  187. }
  188. ---------------------------------------------------------------
  189. [[Kura-KuraRouteractivatorcallbacks]]
  190. KuraRouter activator callbacks
  191. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  192. Kura router comes with the lifecycle callbacks that can be used to
  193. customize the way the Camel router works. For example to configure the
  194. `CamelContext` instance associated with the router just before the
  195. former is started, override `beforeStart` method of the `KuraRouter`
  196. class:
  197. [source,java]
  198. --------------------------------------------------------------------------
  199. public class MyKuraRouter extends KuraRouter {
  200.  
  201. ...
  202. protected void beforeStart(CamelContext camelContext) {
  203. OsgiDefaultCamelContext osgiContext = (OsgiCamelContext) camelContext;
  204. osgiContext.setName("NameOfTheRouter");
  205. }
  206. }
  207. --------------------------------------------------------------------------
  208. [[Kura-LoadingXMLroutesfromConfigurationAdmin]]
  209. Loading XML routes from ConfigurationAdmin
  210. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  211. Sometimes it is desired to read the XML definition of the routes from
  212. the server configuration. This a common scenario for IoT gateways where
  213. over-the-air redeployment cost may be significant. To address this
  214. requirement each `KuraRouter` looks for the
  215. `kura.camel.BUNDLE-SYMBOLIC-NAME.route` property from the `kura.camel`
  216. PID using the OSGi ConfigurationAdmin. This approach allows you to
  217. define Camel XML routes file per deployed `KuraRouter`. In order to
  218. update a route, just edit an appropriate configuration property and
  219. restart a bundle associated with it. The content of
  220. the `kura.camel.BUNDLE-SYMBOLIC-NAME.route` property is expected to be
  221. Camel XML route file, for example:
  222. [source,java]
  223. ------------------------------------------------------
  224. <routes xmlns="http://camel.apache.org/schema/spring">
  225. <route id="loaded">
  226. <from uri="direct:bar"/>
  227. <to uri="mock:bar"/>
  228. </route>
  229. </routes>
  230. ------------------------------------------------------
  231.  
  232. [[Kura-DeployingKurarouterasadeclarativeOSGiservice]]
  233. Deploying Kura router as a declarative OSGi service
  234. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  235. If you would like to deploy your Kura router as a declarative OSGi
  236. service, you can use `activate` and `deactivate` methods provided by
  237. `KuraRouter`.
  238. [source,java]
  239. ----------------------------------------------------------------------------------------------------------------------------------------------
  240. <scr:component name="org.eclipse.kura.example.camel.MyKuraRouter" activate="activate" deactivate="deactivate" enabled="true" immediate="true">
  241. <implementation class="org.eclipse.kura.example.camel.MyKuraRouter"/>
  242. </scr:component>
  243. ----------------------------------------------------------------------------------------------------------------------------------------------
  244. [[Kura-SeeAlso]]
  245. See Also
  246. ^^^^^^^^
  247. * Configuring Camel
  248. * Component
  249. * Endpoint
  250. * Getting Started