Camel
Purpose¶
Apache Camel is a powerful open source integration framework based on known Enterprise Integration Patterns with powerful Bean Integration.
Methods¶
Binding name: p6.camel
Method | Usage |
---|---|
CamelContext getCtx() |
Gets the Camel context currently started within Platform 6. |
Endpoint getEndpoint(String endpoint) |
Direct access to an endpoint within the context given it’s name. |
Map endpointCallWaitInput(String endpoint) |
Synchronous call to given endpoint. Exchange In message returned as Map |
Map endpointExchangeWaitInput(String endpoint, Map exchangeMap) |
Synchronous exchange with given endpoint. Exchange In message given as Map, Exchange In message returned as Map |
Map endpointExchangeWaitOutput(String endpoint, Map exchangeMap) |
Synchronous exchange with given endpoint. Exchange In message given as Map, Exchange Out message returned as Map |
Map endpointExchangeWaitBoth(String endpoint, Map exchangeMap) |
Synchronous exchange with given endpoint. Exchange In message given as Map, Exchange In and Out message returned as Map |
void registerBean(String beanId, Class beanClass) |
Registers a named bean in the Spring application context. |
void unregisterBean(String beanId) |
Removes a named bean from the Spring application context. |
boolean destroyRoute(String routeId) |
Removes the route from the current camel context. False returned in case of failure. |
void destroyAllRoutes() |
Removes all route from the current camel context. |
An Exchange represents an abstraction for an exchange of messages which involves a request message and its corresponding response or an exception message. This can be expressed as a map for use with this DSL:
// Example exchange map def exchange = [ body: "<xml>hello</xml>", headers: [hello : "world" ], attachments: [ myfile: "file:///Users/simont/Documents/temp/b2box-5.2.6-SNAPSHOT/b2run.sh"], properties: [ one : "one", two : "two" ] ]
Method: void processFile(Closure processor)
Should be called in a route definition just after using the Camel File2 component.
It will create a processing.log
file in the same folder as the input file and write a message of error or success depending on the execution of the processor.
Note
The output format inside the processing.log
file is:
20191025T14:38:12.265 GMT [dump.xml] OK: SUCCESS using script 'scriptName' 20191025T14:39:21.866 GMT [dump.xml] ERROR: <<exception message>> using script 'scriptName'
Details¶
When Platform 6 starts, a camel context is started via Spring using the file: $P6_HOME/conf/p6context.xml
.
It is possible to extend the context bean definition using this file; adding routes for example.
Because Spring starts the camel context, the bean registry used by this context will be Springs.
Therefore adding additional beans via the Spring context is also a possibility.
To do this the p6context.xml
file should be moved from $P6_HOME/conf
to $P6_DATA/conf
.
Alternatively, creating routes and registering beans can be performed in the scripts and routes services using this DSL.
JAR dependencies
The following Camel JAR files ship with Platform 6:
- camel-core
- camel-jms
- camel-spring
- camel-file2
- camel-quartz2
Camel/Spring Versions
The version of Camel shipped with Platform 6 is 2.18.1 which is compatible with the version of Spring used by the product: 4.3.4.RELEASE. Please do not attempt to update or use services and/or components that are not version compatible.
Camel has many other components and services that are distributed in other JAR files.
If a developer wishes to use other components and services the JARs (and dependencies) should be downloaded and placed in $P6_DATA/lib
.
Using @Grab
We have encountered issues when using @Grab to download JAR dependencies at runtime. These issues can arise when multiple users run multiple scripts concurrently. Use of @Grab on production system is therefore not recommended however its use during development and prototyping is possible.
JMS integration
Platform 6 has an embedded JMS provider called HornetQ.
Camel will therefore use this provider by default.
In order to create named queues for use with Camel the file $P6_HOME/conf/hornetq-jms.xml
should be moved to $P6_DATA/conf/hornetq-jms.xml
and edited to add the required queues:
<configuration xmlns="urn:hornetq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd"> <connection-factory name="ConnectionFactory"> <connectors> <connector-ref connector-name="in-vm"/> </connectors> <entries> <entry name="ConnectionFactory"/> </entries> <use-global-pools>false</use-global-pools> <scheduled-thread-pool-max-size>10</scheduled-thread-pool-max-size> <thread-pool-max-size>-1</thread-pool-max-size> <client-failure-check-period>2147483646</client-failure-check-period> <connection-ttl>-1</connection-ttl> <reconnect-attempts>-1</reconnect-attempts> </connection-factory> <queue name="myQueue"> <entry name="/queue/cameltest"/> </queue> </configuration>
Please see http://docs.jboss.org/hornetq/2.4.0.Final/docs/user-manual/html/index.html for more information.
JMX activation
JMX is activated within Platform 6 Camel by default allowing you to monitor and control the Camel managed objects with a JMX client.
Please see: http://camel.apache.org/camel-jmx.html for more information
Examples¶
Camel weather
This is an example of using one of the many components available to Camel.
For a full list see: https://github.com/apache/camel/tree/master/components#components
The following JARs were downloaded and copied to $P6_DATA/lib
:
- camel-weather-2.18.1.jar
- jackson-core-asl-1.9.12.jar
- jackson-mapper-asl-1.9.12.jar
I then created a free account with: http://openweathermap.org/ to obtain an appid.
See: http://camel.apache.org/weather.html
println "WEATHER INFO> " + p6.camel.endpointCallWaitInput("weather:foo?appid=3cacd3cba74f4288ec0d8c1396513824")
File listener
cf. File2 documentation for a route definition example
Script definition
p6.camel.processFile { println "------ Camel File2 ------" p6.pipeline.variables().findAll { it.startsWith("CamelFile") }.each { println "${it}: " + p6.pipeline.get(it) } }
Output
------ Camel File2 ------ CamelFileLastModified: 1571928076000 CamelFileParent: /opt/b2box5.data/test CamelFilePath: /opt/b2box5.data/test/file.xml CamelFileLength: 1234567 CamelFileAbsolute: true CamelFileName: file.xml CamelFileNameConsumed: file.xml CamelFileRelativePath: file.xml CamelFileAbsolutePath: /opt/b2box5.data/test/file.xml CamelFileNameOnly: file.xml
The output variables prefixed by CamelFile are defined in Camel documentation