Skip to content

Camel

Purpose

Apache Camel is a powerful open source integration framework based on known Enterprise Integration Patterns with powerful Bean Integration.

Exchange

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/user/Documents/temp/b2box-5.2.6-SNAPSHOT/b2run.sh"],
  properties: [ one : "one", two : "two" ]
]

Note

Camel endpoints can be service consumers, producers or both. The type is dependent upon the component being used and the capabilities supported. Furthermore, the exchange direction used by an endpoint call will also differ and will be specified in the component documentation The endpointCall/Exchange methods attempt to provide enough flexibility to use most component type and exchange direction combinations.

Methods

Binding name: p6.camel

Warning

Some methods are ONLY available when run via a Route Service deployment script. They cannot be used in a general Script service script. This is to avoid problems when running Platform 6 in cluster mode.


getCtx

Warning

Route Service deployment script only

Gets the Camel context within the current Platform 6 instance node

Syntax

CamelContext p6.camel.getCtx()

getEndpoint

Warning

Route Service deployment script only

Direct access to an endpoint within the context on this instance node, given it’s name.

Syntax

Endpoint p6.camel.getEndpoint(String endpoint)

endpointCallWaitInput

Warning

Route Service deployment script only

Synchronous call to producer endpoint (consumer endpoint if consumerOnly). Exchange In message returned as Map

Syntax

Map p6.camel.endpointCallWaitInput(String endpoint, [optional]:boolean conmsumerOnly)
Receiving Email

Info

This example use the camel-mail component

def endpoint = 'imaps://imap.gmail.com?username=noreply@amalto.com&password=mypassword&delete=false&unseen=true&delay=0&searchTerm.subject=Camel'

def responseMap = p6.camel.endpointCallWaitInput(endpoint, true)

println responseMap

Warning

It is recommended that email is normally sent using the email DSL

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")

endpointExchangeWaitInput

Synchronous exchange with producer endpoint. Exchange In message given as Map (Out if exchangeOut), Exchange In message returned as Map

Syntax

Map p6.camel.endpointExchangeWaitInput(String endpoint, Map exchangeMap, [optional:boolean exchangeOut)
FTP File Sender

See Ftp 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('p6file://${P6_DATA}/resources/test.xml'),
  headers: [CamelFileName : '/TEST/test.xml' ]
]

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

println "result: " + result

endpointExchangeWaitOutput

Synchronous exchange with producer endpoint. Exchange In message given as Map, Exchange Out message returned as Map

Syntax

Map p6.camel.endpointExchangeWaitOutput(String endpoint, Map exchangeMap)
Sending Email

Info

This example use the camel-mail component

def exchange = [
  body: 'Hi\nThis is a message from Camel\n',
  headers: [To : "user@server.com", From : "noreply@amalto.com", Subject: "Test Message From Camel" ],
  attachments: [
    mypdf: 'p6file:${P6_DATA}/resources/test.pdf',
    htmlContent: 'p6file:${P6_DATA}/resources/testpage.html'
  ],
]

def endpoint = 'smtps://smtp.gmail.com?username=noreply@amalto.com&password=mypassword'

def responseMap = p6.camel.endpointExchangeWaitOutput(endpoint, exchange)

println responseMap

Warning

It is recommended that email is normally sent using the email DSL


endpointExchangeWaitBoth

Synchronous exchange with producer endpoint. Exchange In message given as Map (Out if exchangeOut), Exchange In and Out message returned as Map

Syntax

Map p6.camel.endpointExchangeWaitBoth(String endpoint, Map exchangeMap, [optional:boolean exchangeOut)

registerBean

Warning

Route Service deployment script only

Registers a named bean in the Spring application context.

Syntax

void p6.camel.registerBean(String beanId, Class beanClass)

unregisterBean

Warning

Route Service deployment script only

Removes a named bean from the Spring application context.

Syntax

void p6.camel.unregisterBean(String beanId)

destroyRoute

Warning

Route Service deployment script only

Removes the route from the current camel context. False returned in case of failure.

Syntax

boolean p6.camel.destroyRoute(String routeId)

destroyAllRoutes

Warning

Route Service deployment script only

Removes all route from the current camel context.

Syntax

void p6.camel.destroyAllRoutes()

processFile

Syntax

void p6.camel.processFile(Closure processor)

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'
File Listener

See File documentation for a route definition example

Script definition
p6.camel.processFile {
    println "------ Camel File ------"
    p6.pipeline.variables().findAll {
        it.startsWith("CamelFile")
    }.each {
        println "${it}: " + p6.pipeline.get(it)
    }
}
Output
------ Camel File ------
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

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-api
  • camel-attachments
  • camel-base
  • camel-base-engine
  • camel-bean
  • camel-browse
  • camel-cloud
  • camel-cluster
  • camel-controlbus
  • camel-core
  • camel-core-catalog
  • camel-core-engine
  • camel-core-languages
  • camel-core-model
  • camel-core-processor
  • camel-core-reifier
  • camel-core-xml
  • camel-dataformat
  • camel-dataset
  • camel-direct
  • camel-directvm
  • camel-file
  • camel-ftp
  • camel-health
  • camel-http
  • camel-http-base
  • camel-http-common
  • camel-jackson
  • camel-language
  • camel-log
  • camel-mail
  • camel-management
  • camel-management-api
  • camel-master
  • camel-mock
  • camel-quartz
  • camel-ref
  • camel-rest
  • camel-saga
  • camel-scheduler
  • camel-seda
  • camel-spring
  • camel-spring-xml
  • camel-stub
  • camel-support
  • camel-timer
  • camel-tooling-model
  • camel-util
  • camel-util-json
  • camel-validator
  • camel-vm
  • camel-web3j
  • camel-xml-io-util
  • camel-xml-jaxb
  • camel-xml-jaxp
  • camel-xpath
  • camel-xslt

See here for detailed documentation of each: https://camel.apache.org/components/3.20.x/index.html

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