Skip to content

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.

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.

JMX client

Please see: http://camel.apache.org/camel-jmx.html for more information

Examples

File Listener

See 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/p6core.data/test
CamelFilePath: /opt/p6core.data/test/file.xml
CamelFileLength: 1234567
CamelFileAbsolute: true
CamelFileName: file.xml
CamelFileNameConsumed: file.xml
CamelFileRelativePath: file.xml
CamelFileAbsolutePath: /opt/p6core.data/test/file.xml
CamelFileNameOnly: file.xml

The output variables prefixed by CamelFile are defined in Camel documentation

FTP File Sender

See Ftp2 documentation for further details

Upload a single file to the TEST folder on server ftp.server.com:

Script definition

def exchange = [
  body: p6.uri.fileFromUrl('file:${P6_DATA}/resources/test.xml'),
  headers: [fileName : '/TEST/test.xml' ]
]

def result = p6.camel.endpointExchangeWaitInput('ftp://ftp.server.com?username=MYUSERNAME&password=PassWord&passiveMode=true', exchange)

println "result: " + result

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 JAR was downloaded and copied to $P6_DATA/lib:

  • camel-weather-2.24.2.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")