You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

Unknown macro: {div}

Component Appendix

There now follows the documentation on each Camel component.

Error formatting macro: include: java.lang.RuntimeException: org.owasp.validator.html.ScanException: java.util.EmptyStackException
Error formatting macro: include: java.lang.IllegalArgumentException: No link could be created for 'CAMEL:ActiveMQ Journal'.

AMQP

The amqp: component supports the AMQP 1.0 protocol using the JMS Client API of the Qpid project. In case you want to use AMQP 0.9 (in particular RabbitMQ) you might also be interested in the Camel RabbitMQ component. Please keep in mind that prior to the Camel 2.17.0 AMQP component supported AMQP 0.9 and above, however since Camel 2.17.0 it supports only AMQP 1.0.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-amqp</artifactId>
    <version>${camel.version}</version> <!-- use the same version as your Camel core version -->
</dependency>

URI format

amqp:[queue:|topic:]destinationName[?options]

AMQP Options

You can specify all of the various configuration options of the JMS component after the destination name.

Usage

As AMQP component inherits from the JMS component. The usage of the former is almost identical to the latter:

Using AMQP component
// Consuming from AMQP queue
from("amqp:queue:incoming")
  .to(...);
 
// Sending message to the AMQP topic
from(...)
  .to("amqp:topic:notify");

Configuring AMQP component

Starting from the Camel 2.16.1 you can also use the AMQPComponent#amqp10Component(String connectionURI) factory method to return the AMQP 1.0 component with the pre-configured topic prefix: 

Creating AMQP 1.0 component
 AMQPComponent amqp = AMQPComponent.amqp10Component("amqp://guest:guest@localhost:5672");

Keep in mind that starting from the Camel 2.17 the AMQPComponent#amqp10Component(String connectionURI) factory method has been deprecated on the behalf of the AMQPComponent#amqpComponent(String connectionURI)

Creating AMQP 1.0 component
AMQPComponent amqp = AMQPComponent.amqpComponent("amqp://localhost:5672");
 
AMQPComponent authorizedAmqp = AMQPComponent.amqpComponent("amqp://localhost:5672", "user", "password");

Starting from Camel 2.17, in order to automatically configure the AMQP component, you can also add an instance of org.apache.camel.component.amqp.AMQPConnectionDetails to the registry. For example for Spring Boot you just have to define bean:

AMQP connection details auto-configuration
@Bean
AMQPConnectionDetails amqpConnection() {
  return new AMQPConnectionDetails("amqp://lcoalhost:5672"); 
}
 
@Bean
AMQPConnectionDetails securedAmqpConnection() {
  return new AMQPConnectionDetails("amqp://lcoalhost:5672", "username", "password"); 
}

 

You can also rely on the Camel properties to read the AMQP connection details. The factory method AMQPConnectionDetails.discoverAMQP() attempts to read Camel properties in a Kubernetes-like convention, just as demonstrated on the snippet below:

 

AMQP connection details auto-configuration
export AMQP_SERVICE_HOST = "mybroker.com"
export AMQP_SERVICE_PORT = "6666"
export AMQP_SERVICE_USERNAME = "username"
export AMQP_SERVICE_PASSWORD = "password"
 
...
 
@Bean
AMQPConnectionDetails amqpConnection() {
  return AMQPConnectionDetails.discoverAMQP(); 
}

Configuring Connection Factory

Like with any other JMS-based component, usually it's important to configure JMS connection factory. For example, you'd like to set your broker URL or set proper connection credentials. Additionally, you would always want to set some kind of pooling (or caching) on the connection factory. An example of how to do both of these tasks is shown below.

<bean id="jmsConnectionFactory" class="org.apache.qpid.jms.JmsConnectionFactory">
  <property name="remoteURI" value="amqp://localhost:5672" />
  <property name="username" value="admin"/>
  <property name="password" value="admin"/>
</bean>

<bean id="jmsCachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
  <property name="targetConnectionFactory" ref="jmsConnectionFactory" />
</bean>

<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration" >
  <property name="connectionFactory" ref="jmsCachingConnectionFactory" /> 
  <property name="cacheLevelName" value="CACHE_CONSUMER" />
</bean>    

<bean id="amqp" class="org.apache.camel.component.amqp.AMQPComponent">
    <property name="configuration" ref="jmsConfig" />
</bean>    

<camelContext xmlns="http://camel.apache.org/schema/blueprint" id="simple">
  <route>
    <from uri="timer:simple?period=5000"/>
    <setBody>
        <simple>Hello World</simple>
    </setBody>
    <to uri="amqp:test"/>
  </route>
</camelContext>

Using amqp inside Karaf

To use the amqp component inside Karaf use the predefined feature called camel-amqp to install the necessary bundles.

Example:

karaf@root()> repo-add camel
karaf@root()> feature:install camel-amqp

and the environment would be set.

Use the camel-blueprint or camel-spring features to define routes in those contexts.

 

 

Bean Component

The bean: component binds beans to Camel message exchanges.

URI format

bean:beanID[?options]

Where beanID can be any string which is used to look up the bean in the Registry

Options

confluenceTableSmall

Name

Type

Default

Description

method

String

null

The method name from the bean that will be invoked. If not provided, Camel will try to determine the method itself. In case of ambiguity an exception will be thrown. See Bean Binding for more details. From Camel 2.8 onwards you can specify type qualifiers to pin-point the exact method to use for overloaded methods. From Camel 2.9 onwards you can specify parameter values directly in the method syntax. See more details at Bean Binding.

cache

boolean

false

If enabled, Camel will cache the result of the first Registry look-up. Cache can be enabled if the bean in the Registry is defined as a singleton scope.

multiParameterArray

boolean

false

How to treat the parameters which are passed from the message body; if it is true, the In message body should be an array of parameters.

bean.xxx

 

null

Camel 2.17: To configure additional options on the create bean instance from the class name. For example to configure a foo option on the bean, use bean.foo=123.

You can append query options to the URI in the following format, ?option=value&option=value&...

Using

The object instance that is used to consume messages must be explicitly registered with the Registry. For example, if you are using Spring you must define the bean in the Spring configuration, spring.xml; or if you don't use Spring, by registering the bean in JNDI.

// lets populate the context with the services we need
// note that we could just use a spring.xml file to avoid this step
JndiContext context = new JndiContext();
context.bind("bye", new SayService("Good Bye!"));

CamelContext camelContext = new DefaultCamelContext(context);

{snippet:id=register|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/pojo/PojoRouteTest.java}

Once an endpoint has been registered, you can build Camel routes that use it to process exchanges.

camelContext.addRoutes(new RouteBuilder() {
    public void configure() {
        from("direct:hello").to("bean:bye");
    }
});

A bean: endpoint cannot be defined as the input to the route; i.e. you cannot consume from it, you can only route from some inbound message Endpoint to the bean endpoint as output. So consider using a direct: or queue: endpoint as the input.

You can use the createProxy() methods on ProxyHelper to create a proxy that will generate BeanExchanges and send them to any endpoint:

Endpoint endpoint = camelContext.getEndpoint("direct:hello");
ISay proxy = PojoComponent.createProxy(endpoint, ISay.class);
String rc = proxy.say();
assertEquals("Good Bye!", rc);

And the same route using Spring DSL:

<route> 
 <from uri="direct:hello">
 <to uri="bean:bye"/>
</route>


Bean as endpoint

Camel also supports invoking Bean as an Endpoint. In the route below:

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:start"/>
    <to uri="myBean"/>
    <to uri="mock:results"/>
  </route>
</camelContext>

<bean id="myBean" class="org.apache.camel.spring.bind.ExampleBean"/>

What happens is that when the exchange is routed to the myBean Camel will use the Bean Binding to invoke the bean.
The source for the bean is just a plain POJO:

public class ExampleBean {
    public String sayHello(String name) {
        return "Hello " + name + "!";
    }
}

Camel will use Bean Binding to invoke the sayHello method, by converting the Exchange's In body to the String type and storing the output of the method on the Exchange Out body.

Java DSL bean syntax

Java DSL comes with syntactic sugar for the Bean component. Instead of specifying the bean explicitly as the endpoint (i.e. to("bean:beanName")) you can use the following syntax:

//Send message to the bean endpoint 
// and invoke method resolved using Bean Binding. 
from("direct:start").beanRef("beanName"); 
 
// Send message to the bean endpoint 
// and invoke given method. 
from("direct:start").beanRef("beanName", "methodName");


Instead of passing name of the reference to the bean (so that Camel will lookup for it in the registry), you can specify the bean itself:

// Send message to the given bean instance. 
from("direct:start").bean(new ExampleBean());
// Explicit selection of bean method to be invoked.
from("direct:start").bean(new ExampleBean(), "methodName");
// Camel will create the instance of bean and cache it for you.
from("direct:start").bean(ExampleBean.class);


Bean Binding

How bean methods to be invoked are chosen (if they are not specified explicitly through the method parameter) and how parameter values are constructed from the Message are all defined by the Bean Binding mechanism which is used throughout all of the various Bean Integration mechanisms in Camel.

Endpoint See Also

Error formatting macro: include: java.lang.RuntimeException: org.owasp.validator.html.ScanException: java.util.EmptyStackException

Direct Component

The direct: component provides direct, synchronous invocation of any consumers when a producer sends a message exchange. This endpoint can be used to connect existing routes in the same camel context.

Asynchronous

The SEDA component provides asynchronous invocation of any consumers when a producer sends a message exchange.

Connection to other camel contexts

The VM component provides connections between Camel contexts as long they run in the same JVM.

URI format

direct:someName[?options]

Where someName can be any string that uniquely identifies the endpoint.

Options

Name

Default Value

Description

allowMultipleConsumers

true

@deprecated

If set to false, then when a second consumer is started on the endpoint, an IllegalStateException is thrown.

Will be removed in Camel 2.1: Direct endpoint does not support multiple consumers.

block

false

Camel 2.11.1: If sending a message to a direct endpoint which has no active consumer, the producer will block for timeout milliseconds waiting for a consumer to become active.

timeout

30000

Camel 2.11.1: The timeout value, in milliseconds, to block, when enabled, for an active consumer.

failIfNoConsumers

true

Camel 2.16.0: Indicates whether the producer should fail by throwing an exception when sending to a direct endpoint with no active consumers.

You can append query options to the URI in the following format: ?option=value&option=value&...

 

Samples

In the route below we use the direct component to link the two routes together:

from("activemq:queue:order.in")
    .to("bean:orderServer?method=validate")
    .to("direct:processOrder?block=true&timeout=5000");

from("direct:processOrder")
    .to("bean:orderService?method=process")
    .to("activemq:queue:order.out");

And the sample using spring DSL:

   <route>
     <from uri="activemq:queue:order.in"/>
     <to uri="bean:orderService?method=validate"/>
     <to uri="direct:processOrder?failIfNoConsumers=false"/>
  </route>

  <route>
     <from uri="direct:processOrder"/>
     <to uri="bean:orderService?method=process"/>
     <to uri="activemq:queue:order.out"/>
  </route>    

See also samples from the SEDA component, how they can be used together.

Esper

The Esper component supports the Esper Library for Event Stream Processing. The camel-esper library is provided by the Camel Extra project which hosts all *GPL related components for Camel.

URI format

esper:name[?options]

When consuming from an Esper endpoint you must specify a pattern or eql statement to query the event stream.

Pattern example:

from("esper://cheese?pattern=every event=MyEvent(bar=5)")
  .to("activemq:Foo");

EQL example:

from("esper://esper-dom?eql=insert into DomStream select * from org.w3c.dom.Document")
  .to("log://esper-dom?level=INFO");
from("esper://esper-dom?eql=select childNodes from DomStream")
  .to("mock:results");

Options

Name

Default Value

Description

configured

false

Available as of camel-extra 2.11.3:
If flag is set to 'true' the default Esper configuration file (esper.cfg.xml) will be used. 
To configure Esper via a configuration file, please refer to the Esper documentation

pattern

 

The Esper Pattern expression as a String to filter events

eql

 

The Esper EQL expression as a String to filter events

You can append query options to the URI in the following format, ?option=value&option=value&...

EsperMessage

From Camel 2.12 onwards the esper consumer stores new and old events in the org.apacheextras.camel.component.esper.EsperMessage message as the input Message on the Exchange. You can get access to the esper event beans from java code with:

  EventBean newEvent = exchange.getIn(EsperMessage.class).getNewEvent();
  EventBean oldEvent = exchange.getIn(EsperMessage.class).getOldEvent();

By default if you get the body of org.apacheextras.camel.component.esper.EsperMessage it returns the new EventBean as in previous versions.

Demo

There is a demo which shows how to work with ActiveMQ, Camel and Esper in the Camel Extra project

Error formatting macro: include: java.lang.IllegalArgumentException: No link could be created for 'CAMEL:Event'.
Error formatting macro: include: java.lang.IllegalArgumentException: No link could be created for 'CAMEL:File'.
Error formatting macro: include: java.lang.IllegalArgumentException: No link could be created for 'CAMEL:FIX'.

FTP/SFTP/FTPS Component

This component provides access to remote file systems over the FTP and SFTP protocols.

Maven users will need to add the following dependency to their pom.xml for this component:

xml<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-ftp</artifactId> <version>x.x.x</version>See the documentation of the Apache Commons <!-- use the same version as your Camel core version --> </dependency>

 

 

More options

See File for more options as all the options from File is inherited.

Absolute paths

Absolute path is not supported.

Camel 2.16 will translate absolute paths to relative ones by trimming all leading slashes from directoryname. There'll be WARN message printed in the logs.

Consuming from remote FTP server

Make sure you read the section titled Default when consuming files further below for details related to consuming files.

URI format

ftp://[username@]hostname[:port]/directoryname[?options] sftp://[username@]hostname[:port]/directoryname[?options] ftps://[username@]hostname[:port]/directoryname[?options]

Where directoryname represents the underlying directory. The directory name is a relative path. Absolute paths are not supported. The relative path can contain nested folders, such as /inbox/us.

For Camel versions before Camel 2.16, the directoryName must exist already as this component does not support the autoCreate option (which the file component does). The reason is that its the FTP administrator (FTP server) task to properly setup user accounts, and home directories with the right file permissions etc.

For Camel 2.16autoCreate option is supported. When consumer starts, before polling is scheduled, there's additional FTP operation performed to create the directory configured for endpoint. The default value for autoCreate is true.

If no username is provided, then anonymous login is attempted using no password.
If no port number is provided, Camel will provide default values according to the protocol (ftp = 21, sftp = 22, ftps = 2222).

You can append query options to the URI in the following format, ?option=value&option=value&...

This component uses two different libraries for the actual FTP work. FTP and FTPS uses Apache Commons Net while SFTP uses JCraft JSCH.

The FTPS component is only available in Camel 2.2 or newer.
FTPS (also known as FTP Secure) is an extension to FTP that adds support for the Transport Layer Security (TLS) and the Secure Sockets Layer (SSL) cryptographic protocols.

URI Options

The options below are exclusive for the FTP component.

More options

See File for more options as all the options from File is inherited.

confluenceTableSmall

Name

Default Value

Description

username

null

Specifies the username to use to log in to the remote file systen.

password

null

Specifies the password to use to log in to the remote file system.

account

nullCamel 2.15.2: Specified the account to use to login to the remote FTP server (only for FTP and FTP Secure)

binary

false

Specifies the file transfer mode, BINARY or ASCII. Default is ASCII (false).

disconnect

false

Camel 2.2: Whether or not to disconnect from remote FTP server right after use. Can be used for both consumer and producer. Disconnect will only disconnect the current connection to the FTP server. If you have a consumer which you want to stop, then you need to stop the consumer/route instead.

localWorkDirectory

null

When consuming, a local work directory can be used to store the remote file content directly in local files, to avoid loading the content into memory. This is beneficial, if you consume a very big remote file and thus can conserve memory. See below for more details.

passiveMode

false

FTP and FTPS only: Specifies whether to use passive mode connections. Default is active mode (false).

securityProtocol

TLS

FTPS only: Sets the underlying security protocol. The following values are defined:
TLS: Transport Layer Security
SSL: Secure Sockets Layer

disableSecureDataChannelDefaults

false

Camel 2.4: FTPS only: Whether or not to disable using default values for execPbsz and execProt when using secure data transfer. You can set this option to true if you want to be in full control what the options execPbsz and execProt should be used.

download

true

Camel 2.11: Whether the FTP consumer should download the file. If this option is set to false, then the message body will be null, but the consumer will still trigger a Camel Exchange that has details about the file such as file name, file size, etc. It's just that the file will not be downloaded.

streamDownload

false

Camel 2.11: Whether the consumer should download the entire file up front, the default behavior, or if it should pass an InputStream read from the remote resource rather than an in-memory array as the in body of the Camel Exchange.  This option is ignored if download is false or is localWorkDirectory is provided.  This option is useful for working with large remote files.

execProt

null

Camel 2.4: FTPS only: Will by default use option P if secure data channel defaults hasn't been disabled. Possible values are:
C: Clear
S: Safe (SSL protocol only)
E: Confidential (SSL protocol only)
P: Private

execPbsz

null

Camel 2.4: FTPS only: This option specifies the buffer size of the secure data channel. If option useSecureDataChannel has been enabled and this option has not been explicit set, then value 0 is used.

isImplicit

false

FTPS only: Sets the security mode(implicit/explicit). Default is explicit (false).

knownHostsFile

null

SFTP only: Sets the known_hosts file, so that the SFTP endpoint can do host key verification.

useUserKnownHostsFiletrueSFTP onlly: Camel 2.18: If knownHostFile has not been explicit configured then use the host file from System.getProperty(user.home)/.ssh/known_hosts

knownHostsUri

null

SFTP only: Camel 2.11.1: Sets the known_hosts file (loaded from classpath by default), so that the SFTP endpoint can do host key verification.

keyPair

null

SFTP only: Camel 2.12.0: Sets the Java KeyPair for SSH public key authentication, it supports DSA or RSA keys.

privateKeyFile

null

SFTP only: Set the private key file to that the SFTP endpoint can do private key verification.

privateKeyUri

null

SFTP only: Camel 2.11.1: Set the private key file (loaded from classpath by default) to that the SFTP endpoint can do private key verification.

privateKey

null

SFTP only: Camel 2.11.1: Set the private key as byte[] to that the SFTP endpoint can do private key verification.

privateKeyFilePassphrase

null

SFTP only: Deprecated: use privateKeyPassphrase instead. Set the private key file passphrase to that the SFTP endpoint can do private key verification.

privateKeyPassphrase

null

SFTP only: Camel 2.11.1: Set the private key file passphrase to that the SFTP endpoint can do private key verification.

preferredAuthentications

null

SFTP only: Camel 2.10.7, 2.11.2,2.12.0: set the preferred authentications which SFTP endpoint will used. Some example include:password,publickey. If not specified the default list from JSCH will be used.

ciphers

null

Camel 2.8.2, 2.9: SFTP only Set a comma separated list of ciphers that will be used in order of preference. Possible cipher names are defined by JCraft JSCH. Some examples include: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc. If not specified the default list from JSCH will be used.

fastExistsCheck

false

Camel 2.8.2, 2.9: If set this option to be true, camel-ftp will use the list file directly to check if the file exists. Since some FTP server may not support to list the file directly, if the option is false, camel-ftp will use the old way to list the directory and check if the file exists. Note from Camel 2.10.1 onwards this option also influences readLock=changed to control whether it performs a fast check to update file information or not. This can be used to speed up the process if the FTP server has a lot of files.

strictHostKeyChecking

no

SFTP only: Camel 2.2: Sets whether to use strict host key checking. Possible values are: no, yes and ask. ask does not make sense to use as Camel cannot answer the question for you as its meant for human intervention. Note: The default in Camel 2.1 and below was ask.

maximumReconnectAttempts

3

Specifies the maximum reconnect attempts Camel performs when it tries to connect to the remote FTP server. Use 0 to disable this behavior.

reconnectDelay

1000

Delay in millis Camel will wait before performing a reconnect attempt.

connectTimeout

10000

Camel 2.4: Is the connect timeout in millis. This corresponds to using ftpClient.connectTimeout for the FTP/FTPS. For SFTP this option is also used when attempting to connect.

soTimeout

null / 30000

FTP and FTPS Only: Camel 2.4: Is the SocketOptions.SO_TIMEOUT value in millis. A good idea is to configure this to a value such as 300000 (5 minutes) to not hang a connection. On SFTP this option is set as timeout on the JSCH Session instance.

Also SFTP from Camel 2.14.3/2.15.3/2.16 onwards.

From Camel 2.16 onwards the default is 300000 (300 sec).

timeout

30000

FTP and FTPS Only: Camel 2.4: Is the data timeout in millis. This corresponds to using ftpClient.dataTimeout for the FTP/FTPS. For SFTP there is no data timeout.

throwExceptionOnConnectFailed

false

Camel 2.5: Whether or not to thrown an exception if a successful connection and login could not be establish. This allows a custom pollStrategy to deal with the exception, for example to stop the consumer or the likes.

siteCommand

null

FTP and FTPS Only: Camel 2.5: To execute site commands after successful login. Multiple site commands can be separated using a new line character (\n). Use help site to see which site commands your FTP server supports.

stepwise

true

Camel 2.6: Whether or not stepwise traversing directories should be used or not. Stepwise means that it will CD one directory at a time. See more details below. You can disable this in case you can't use this approach.

separator

UNIX

Camel 2.6: Dictates what path separator char to use when uploading files. Auto = Use the path provided without altering it. UNIX = Use unix style path separators. Windows = Use Windows style path separators.

Since Camel 2.15.2: The default value is changed to UNIX style path, before Camel 2.15.2: The default value is Auto.

chmod

null

SFTP Producer Only: Camel 2.9: Allows you to set chmod on the stored file. For example chmod=640.

compression

0

SFTP Only: Camel 2.8.3/2.9: To use compression. Specify a level from 1 to 10. Important: You must manually add the needed JSCH zlib JAR to the classpath for compression support.

receiveBufferSize

32768FTP/FTPS Only: Camel 2.15.1: The buffer size for downloading files. The default size is 32kb.

ftpClient

null

FTP and FTPS Only: Camel 2.1: Allows you to use a custom org.apache.commons.net.ftp.FTPClient instance.

ftpClientConfig

null

FTP and FTPS Only: Camel 2.1: Allows you to use a custom org.apache.commons.net.ftp.FTPClientConfig instance.

ftpClientConfig.XXX FTP and FTPS Only: To configure various options on the FTPClient instance from the uri. For example: ftpClientConfig.receiveDataSocketBufferSize=8192&ftpClientConfig.sendDataSocketBufferSize=8192

serverAliveInterval

0

SFTP Only: Camel 2.8 Allows you to set the serverAliveInterval of the sftp session

serverAliveCountMax

1

SFTP Only: Camel 2.8 Allows you to set the serverAliveCountMax of the sftp session

ftpClient.trustStore.file

null

FTPS Only: Sets the trust store file, so that the FTPS client can look up for trusted certificates.

ftpClient.trustStore.type

JKS

FTPS Only: Sets the trust store type.

ftpClient.trustStore.algorithm

SunX509

FTPS Only: Sets the trust store algorithm.

ftpClient.trustStore.password

null

FTPS Only: Sets the trust store password.

ftpClient.keyStore.file

null

FTPS Only: Sets the key store file, so that the FTPS client can look up for the private certificate.

ftpClient.keyStore.type

JKS

FTPS Only: Sets the key store type.

ftpClient.keyStore.algorithm

SunX509

FTPS Only: Sets the key store algorithm.

ftpClient.keyStore.password

null

FTPS Only: Sets the key store password.

ftpClient.keyStore.keyPassword

null

FTPS Only: Sets the private key password.

sslContextParameters

null

FTPS Only: Camel 2.9: Reference to a org.apache.camel.util.jsse.SSLContextParameters in the Registry.  This reference overrides any configured SSL related options on ftpClient as well as the securityProtocol (SSL, TLS, etc.) set on FtpsConfiguration.  See Using the JSSE Configuration Utility.

proxy

null

SFTP Only: Camel 2.10.7, 2.11.1: Reference to a com.jcraft.jsch.Proxy in the Registry.  This proxy is used to consume/send messages from the target SFTP host.

useList

true

FTP/FTPS Only: Camel 2.12.1: Whether the consumer should use FTP LIST command to retrieve directory listing to see which files exists. If this option is set to false, then stepwise=false must be configured, and also fileName must be configured to a fixed name, so the consumer knows the name of the file to retrieve. When doing this only that single file can be retrieved. See further below for more details.

ignoreFileNotFoundOrPermissionError

false

Camel 2.12.1: Whether the consumer should ignore when a file was attempted to be retrieved but did not exist (for some reason), or failure due insufficient file permission error. Camel 2.14.2: This option now applies to directories as well.

sendNooptrueCamel 2.16: Producer only. Whether to send a noop command as a pre-write check before uploading files to the FTP server. This is enabled by default as a validation of the connection is still valid, which allows to silently re-connect to be able to upload the file. However if this causes problems, you can turn this option off.
jschLoggingLevelWARNSFTP Only: Camel 2.15.3/2.16: The logging level to use for JSCH activity logging. As JSCH is verbose at by default at INFO level the threshold is WARN by default.
bulkRequest SFTP Only: Camel 2.17.1: Specifies how many requests may be outstanding at any one time. Increasing this value may slightly improve file transfer speed but will increase memory usage.
disconnectOnBatchCompletefalseCamel 2.18: Whether or not to disconnect from remote FTP server after a Batch is complete. Can be used for both consumer and producer. Disconnect will only disconnect the current connection to the FTP server. If you have a consumer which you want to stop, then you need to stop the consumer/route instead.
activePortRange Camel 2.18: Set the client side port range in active mode. The syntax is: minPort-maxPort. Both port numbers are inclusive, eg 10000-19999 to include all 1xxxx ports.
FTPS component default trust store

When using the ftpClient. properties related to SSL with the FTPS component, the trust store accepts all certificates. If you only want trust selective certificates, you have to configure the trust store with the ftpClient.trustStore.xxx options or by configuring a custom ftpClient.

When using sslContextParameters, the trust store is managed by the configuration of the provided SSLContextParameters instance.

You can configure additional options on the ftpClient and ftpClientConfig from the URI directly by using the ftpClient. or ftpClientConfig. prefix.

For example to set the setDataTimeout on the FTPClient to 30 seconds you can do:

from("ftp://foo@myserver?password=secret&ftpClient.dataTimeout=30000").to("bean:foo");

You can mix and match and have use both prefixes, for example to configure date format or timezones.

from("ftp://foo@myserver?password=secret&ftpClient.dataTimeout=30000&ftpClientConfig.serverLanguageCode=fr").to("bean:foo");

You can have as many of these options as you like.

See the documentation of the Apache Commons FTP FTPClientConfig for possible options and more details. And as well for Apache Commons FTP FTPClient.

If you do not like having many and long configuration in the url you can refer to the ftpClient or ftpClientConfig to use by letting Camel lookup in the Registry for it.

For example:

<bean id="myConfig" class="org.apache.commons.net.ftp.FTPClientConfig"> <property name="lenientFutureDates" value="true"/> <property name="serverLanguageCode" value="fr"/> </bean>

And then let Camel lookup this bean when you use the # notation in the url.

from("ftp://foo@myserver?password=secret&ftpClientConfig=#myConfig").to("bean:foo");

More URI options

title:More options

See File2 as all the options there also applies for this component.

Examples

ftp://someone@someftpserver.com/public/upload/images/holiday2008?password=secret&binary=true
ftp://someoneelse@someotherftpserver.co.uk:12049/reports/2008/password=secret&binary=false
ftp://publicftpserver.com/download

FTP Consumer does not support concurrency

The FTP consumer (with the same endpoint) does not support concurrency (the backing FTP client is not thread safe).
You can use multiple FTP consumers to poll from different endpoints. It is only a single endpoint that does not support concurrent consumers.

The FTP producer does not have this issue, it supports concurrency.

More information

This component is an extension of the File component. So there are more samples and details on the File component page.

Default when consuming files

The FTP consumer will by default leave the consumed files untouched on the remote FTP server. You have to configure it explicitly if you want it to delete the files or move them to another location. For example you can use delete=true to delete the files, or use move=.done to move the files into a hidden done sub directory.

The regular File consumer is different as it will by default move files to a .camel sub directory. The reason Camel does not do this by default for the FTP consumer is that it may lack permissions by default to be able to move or delete files.

limitations

The option readLock can be used to force Camel not to consume files that are currently being written. However, this option is turned off by default, as it requires that the user has write access. See the options table at File2 for more details about read locks.
There are other solutions to avoid consuming files that are currently being written over FTP; for instance, you can write to a temporary destination and move the file after it has been written.

When moving files using move or preMove option the files are restricted to the FTP_ROOT folder. That prevents you from moving files outside the FTP area. If you want to move files to another area you can use soft links and move files into a soft linked folder.

Message Headers

The following message headers can be used to affect the behavior of the component

confluenceTableSmall

Header

Description

CamelFileName

Specifies the output file name (relative to the endpoint directory) to be used for the output message when sending to the endpoint. If this is not present and no expression either, then a generated message ID is used as the filename instead.

CamelFileNameProduced

The actual filepath (path + name) for the output file that was written. This header is set by Camel and its purpose is providing end-users the name of the file that was written.

CamelBatchIndex

Current index out of total number of files being consumed in this batch.

CamelBatchSize

Total number of files being consumed in this batch.

CamelFileHost

The remote hostname.

CamelFileLocalWorkPath

Path to the local work file, if local work directory is used.

In addition the FTP/FTPS consumer and producer will enrich the Camel Message with the following headers

confluenceTableSmall

Header

Description

CamelFtpReplyCode

Camel 2.11.1: The FTP client reply code (the type is a integer)

CamelFtpReplyString

Camel 2.11.1: The FTP client reply string

About timeouts

The two set of libraries (see top) have different APIs for setting timeout. You can use the connectTimeout option for both of them to set a timeout in millis to establish a network connection. An individual soTimeout can also be set on the FTP/FTPS, which corresponds to using ftpClient.soTimeout. Notice SFTP will automatically use connectTimeout as its soTimeout. The timeout option only applies for FTP/FTSP as the data timeout, which corresponds to the ftpClient.dataTimeout value. All timeout values are in millis.

Using Local Work Directory

Camel supports consuming from remote FTP servers and downloading the files directly into a local work directory. This avoids reading the entire remote file content into memory as it is streamed directly into the local file using FileOutputStream.

Camel will store to a local file with the same name as the remote file, though with .inprogress as extension while the file is being downloaded. Afterwards, the file is renamed to remove the .inprogress suffix. And finally, when the Exchange is complete the local file is deleted.

So if you want to download files from a remote FTP server and store it as files then you need to route to a file endpoint such as:

javafrom("ftp://someone@someserver.com?password=secret&localWorkDirectory=/tmp").to("file://inbox"); Optimization by renaming work file

The route above is ultra efficient as it avoids reading the entire file content into memory. It will download the remote file directly to a local file stream. The java.io.File handle is then used as the Exchange body. The file producer leverages this fact and can work directly on the work file java.io.File handle and perform a java.io.File.rename to the target filename. As Camel knows it's a local work file, it can optimize and use a rename instead of a file copy, as the work file is meant to be deleted anyway.

Stepwise changing directories

Camel FTP can operate in two modes in terms of traversing directories when consuming files (eg downloading) or producing files (eg uploading)

  • stepwise
  • not stepwise

You may want to pick either one depending on your situation and security issues. Some Camel end users can only download files if they use stepwise, while others can only download if they do not. At least you have the choice to pick (from Camel 2.6 onwards).

In Camel 2.0 - 2.5 there is only one mode and it is:

  • before 2.5 not stepwise
  • 2.5 stepwise

From Camel 2.6 onwards there is now an option stepwise you can use to control the behavior.

Note that stepwise changing of directory will in most cases only work when the user is confined to it's home directory and when the home directory is reported as "/".

The difference between the two of them is best illustrated with an example. Suppose we have the following directory structure on the remote FTP server we need to traverse and download files:

/ /one /one/two /one/two/sub-a /one/two/sub-b

And that we have a file in each of sub-a (a.txt) and sub-b (b.txt) folder.

Using stepwise=true (default mode)

TYPE A 200 Type set to A PWD 257 "/" is current directory. CWD one 250 CWD successful. "/one" is current directory. CWD two 250 CWD successful. "/one/two" is current directory. SYST 215 UNIX emulated by FileZilla PORT 127,0,0,1,17,94 200 Port command successful LIST 150 Opening data channel for directory list. 226 Transfer OK CWD sub-a 250 CWD successful. "/one/two/sub-a" is current directory. PORT 127,0,0,1,17,95 200 Port command successful LIST 150 Opening data channel for directory list. 226 Transfer OK CDUP 200 CDUP successful. "/one/two" is current directory. CWD sub-b 250 CWD successful. "/one/two/sub-b" is current directory. PORT 127,0,0,1,17,96 200 Port command successful LIST 150 Opening data channel for directory list. 226 Transfer OK CDUP 200 CDUP successful. "/one/two" is current directory. CWD / 250 CWD successful. "/" is current directory. PWD 257 "/" is current directory. CWD one 250 CWD successful. "/one" is current directory. CWD two 250 CWD successful. "/one/two" is current directory. PORT 127,0,0,1,17,97 200 Port command successful RETR foo.txt 150 Opening data channel for file transfer. 226 Transfer OK CWD / 250 CWD successful. "/" is current directory. PWD 257 "/" is current directory. CWD one 250 CWD successful. "/one" is current directory. CWD two 250 CWD successful. "/one/two" is current directory. CWD sub-a 250 CWD successful. "/one/two/sub-a" is current directory. PORT 127,0,0,1,17,98 200 Port command successful RETR a.txt 150 Opening data channel for file transfer. 226 Transfer OK CWD / 250 CWD successful. "/" is current directory. PWD 257 "/" is current directory. CWD one 250 CWD successful. "/one" is current directory. CWD two 250 CWD successful. "/one/two" is current directory. CWD sub-b 250 CWD successful. "/one/two/sub-b" is current directory. PORT 127,0,0,1,17,99 200 Port command successful RETR b.txt 150 Opening data channel for file transfer. 226 Transfer OK CWD / 250 CWD successful. "/" is current directory. QUIT 221 Goodbye disconnected.

As you can see when stepwise is enabled, it will traverse the directory structure using CD xxx.

Using stepwise=false

230 Logged on TYPE A 200 Type set to A SYST 215 UNIX emulated by FileZilla PORT 127,0,0,1,4,122 200 Port command successful LIST one/two 150 Opening data channel for directory list 226 Transfer OK PORT 127,0,0,1,4,123 200 Port command successful LIST one/two/sub-a 150 Opening data channel for directory list 226 Transfer OK PORT 127,0,0,1,4,124 200 Port command successful LIST one/two/sub-b 150 Opening data channel for directory list 226 Transfer OK PORT 127,0,0,1,4,125 200 Port command successful RETR one/two/foo.txt 150 Opening data channel for file transfer. 226 Transfer OK PORT 127,0,0,1,4,126 200 Port command successful RETR one/two/sub-a/a.txt 150 Opening data channel for file transfer. 226 Transfer OK PORT 127,0,0,1,4,127 200 Port command successful RETR one/two/sub-b/b.txt 150 Opening data channel for file transfer. 226 Transfer OK QUIT 221 Goodbye disconnected.

As you can see when not using stepwise, there are no CD operation invoked at all.

Samples

In the sample below we set up Camel to download all the reports from the FTP server once every hour (60 min) as BINARY content and store it as files on the local file system.{snippet:id=e1|lang=java|url=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpToBinarySampleTest.java}And the route using Spring DSL:

xml <route> <from uri="ftp://scott@localhost/public/reports?password=tiger&amp;binary=true&amp;delay=60000"/> <to uri="file://target/test-reports"/> </route>

Consuming a remote FTPS server (implicit SSL) and client authentication

from("ftps://admin@localhost:2222/public/camel?password=admin&securityProtocol=SSL&isImplicit=true &ftpClient.keyStore.file=./src/test/resources/server.jks &ftpClient.keyStore.password=password&ftpClient.keyStore.keyPassword=password") .to("bean:foo");

Consuming a remote FTPS server (explicit TLS) and a custom trust store configuration

from("ftps://admin@localhost:2222/public/camel?password=admin&ftpClient.trustStore.file=./src/test/resources/server.jks&ftpClient.trustStore.password=password") .to("bean:foo");

Filter using org.apache.camel.component.file.GenericFileFilter

Camel supports pluggable filtering strategies. This strategy can be provided by implementing org.apache.camel.component.file.GenericFileFilter in Java. You can then configure the endpoint with such a filter to skip certain filters before being processed.

In the sample we have built our own filter that only accepts files starting with report in the filename.{snippet:id=e1|lang=java|url=camel/trunk/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/FromFtpRemoteFileFilterTest.java}And then we can configure our route using the filter attribute to reference our filter (using # notation) that we have defined in the spring XML file:

xml <!-- define our sorter as a plain spring bean --> <bean id="myFilter" class="com.mycompany.MyFileFilter"/> <route> <from uri="ftp://someuser@someftpserver.com?password=secret&amp;filter=#myFilter"/> <to uri="bean:processInbox"/> </route>

Filtering using ANT path matcher

The ANT path matcher is a filter that is shipped out-of-the-box in the camel-spring jar. So you need to depend on camel-spring if you are using Maven.
The reason is that we leverage Spring's AntPathMatcher to do the actual matching.

The file paths are matched with the following rules:

  • ? matches one character
  • * matches zero or more characters
  • ** matches zero or more directories in a path

The sample below demonstrates how to use it:{snippet:id=example|lang=xml|url=camel/trunk/tests/camel-itest/src/test/resources/org/apache/camel/itest/ftp/SpringFileAntPathMatcherRemoteFileFilterTest-context.xml}

Using a proxy with SFTP

To use an HTTP proxy to connect to your remote host, you can configure your route in the following way:

xml<!-- define our sorter as a plain spring bean --> <bean id="proxy" class="com.jcraft.jsch.ProxyHTTP"> <constructor-arg value="localhost"/> <constructor-arg value="7777"/> </bean> <route> <from uri="sftp://localhost:9999/root?username=admin&password=admin&proxy=#proxy"/> <to uri="bean:processFile"/> </route>

You can also assign a user name and password to the proxy, if necessary. Please consult the documentation for com.jcraft.jsch.Proxy to discover all options.

Setting preferred SFTP authentication method

If you want to explicitly specify the list of authentication methods that should be used by sftp component, use preferredAuthentications option. If for example you would like Camel to attempt to authenticate with private/public SSH key and fallback to user/password authentication in the case when no public key is available, use the following route configuration:

from("sftp://localhost:9999/root?username=admin&password=admin&preferredAuthentications=publickey,password"). to("bean:processFile");

Consuming a single file using a fixed name

When you want to download a single file and know the file name, you can use fileName=myFileName.txt to tell Camel the name of the file to download. By default the consumer will still do a FTP LIST command to do a directory listing and then filter these files based on the fileName option. Though in this use-case it may be desirable to turn off the directory listing by setting useList=false. For example the user account used to login to the FTP server may not have permission to do a FTP LIST command. So you can turn off this with useList=false, and then provide the fixed name of the file to download with fileName=myFileName.txt, then the FTP consumer can still download the file. If the file for some reason does not exist, then Camel will by default throw an exception, you can turn this off and ignore this by setting ignoreFileNotFoundOrPermissionError=true.

For example to have a Camel route that picks up a single file, and deletes it after use you can write

from("ftp://admin@localhost:21/nolist/?password=admin&stepwise=false&useList=false&ignoreFileNotFoundOrPermissionError=true&fileName=report.txt&delete=true") .to("activemq:queue:report");

Notice that we have used all the options we talked above.

You can also use this with ConsumerTemplate. For example to download a single file (if it exists) and grab the file content as a String type:

String data = template.retrieveBodyNoWait("ftp://admin@localhost:21/nolist/?password=admin&stepwise=false&useList=false&ignoreFileNotFoundOrPermissionError=true&fileName=report.txt&delete=true", String.class);

Debug logging

This component has log level TRACE that can be helpful if you have problems.

Endpoint See Also

HTTP Component

The http: component provides HTTP based endpoints for consuming external HTTP resources (as a client to call external servers using HTTP).

Maven users will need to add the following dependency to their pom.xml for this component:

xml<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-http</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>

URI Format

http:hostname[:port][/resourceUri][?param1=value1][&param2=value2]

Will by default use port 80 for HTTP and 443 for HTTPS.

camel-http vs camel-jetty

You can only produce to endpoints generated by the HTTP component. Therefore it should never be used as input into your camel Routes. To bind/expose an HTTP endpoint via a HTTP server as input to a camel route, you can use the Jetty Component or the Servlet Component

Examples

Call the URL with the body using POST and return response as the OUT message. If body is null call URL using GET and return response as OUT message:

Java DSL

Spring DSL

from("direct:start") .to("http://myhost/mypath");xml<from uri="direct:start"/> <to uri="http://oldhost"/>

You can override the HTTP endpoint URI by adding a header. Camel will call the http://newhost. This is very handy for e.g. REST URLs:

Java DSL

javafrom("direct:start") .setHeader(Exchange.HTTP_URI, simple("http://myserver/orders/${header.orderId}")) .to("http://dummyhost");

URI parameters can either be set directly on the endpoint URI or as a header:

Java DSL

javafrom("direct:start") .to("http://oldhost?order=123&detail=short"); from("direct:start") .setHeader(Exchange.HTTP_QUERY, constant("order=123&detail=short")) .to("http://oldhost");

Set the HTTP request method to POST:

Java DSL

Spring DSL

from("direct:start") .setHeader(Exchange.HTTP_METHOD, constant("POST")) .to("http://www.google.com"); xml<from uri="direct:start"/> <setHeader headerName="CamelHttpMethod"> <constant>POST</constant> </setHeader> <to uri="http://www.google.com"/> <to uri="mock:results"/>

HttpEndpoint Options

confluenceTableSmall

Name

Default Value

Description

throwExceptionOnFailure

true

Option to disable throwing the HttpOperationFailedException in case of failed responses from the remote server. This allows you to get all responses regardless of the HTTP status code.

bridgeEndpoint

false

If the option is trueHttpProducer will ignore the Exchange.HTTP_URI header, and use the endpoint's URI for request. You may also set throwExceptionOnFailure=false to ensure all responses are propagated back to the  HttpProducer.

From Camel 2.3: when true the HttpProducer and CamelServlet will skip gzip processing when content-encoding=gzip.

disableStreamCache

false

When false the DefaultHttpBinding will copy the request input stream into a stream cache and put it into message body which allows it to be read more than once.

When true the DefaultHttpBinding will set the request input stream direct into the message body.

From Camel 2.17: this options is now also support by the producer to allow using the response stream directly instead of stream caching as by default.

httpBindingRef

null

Deprecated and removed in Camel 2.17: Reference to a org.apache.camel.component.http.HttpBinding in the Registry. Use the httpBinding option instead.

httpBinding

null

From Camel 2.3: reference to a org.apache.camel.component.http.HttpBinding in the Registry.

httpClientConfigurerRef

null

Deprecated and removed in Camel 2.17: Reference to a org.apache.camel.component.http.HttpClientConfigurer in the Registry. Use the httpClientConfigurer option instead.

httpClientConfigurer

null

From Camel 2.3: reference to a org.apache.camel.component.http.HttpClientConfigurer in the Registry.

httpClient.XXX

null

Use this to option to configure the underlying HttpClientParams.

Example: httpClient.soTimeout=5000 will set the SO_TIMEOUT to 5 seconds.

clientConnectionManager

null

To use a custom org.apache.http.conn.ClientConnectionManager.

transferException

false

From Camel 2.6: If enabled and an Exchange failed processing on the consumer side, and if the caused Exception was send back serialized in the response as a application/x-java-serialized-object content type (for example using Jetty or SERVLET Camel components).

On the producer side the exception will be deserialized and thrown as is, instead of the HttpOperationFailedException. The caused exception will be serialized.

headerFilterStrategy

null

From Camel 2.11: reference to a instance of org.apache.camel.spi.HeaderFilterStrategy in the Registry. It will be used to apply the custom headerFilterStrategy on the new create HttpEndpoint.

urlRewrite

null

From Camel 2.11: Producer only!

Refers to a custom org.apache.camel.component.http.UrlRewrite which allows you to rewrite URLs when you bridge/proxy endpoints.

See more details at UrlRewrite and How to use Camel as a HTTP proxy between a client and server.

eagerCheckContentAvailable

false

From Camel 2.15.3/2.16: Consumer only!

Whether to eager check whether the HTTP requests has content when content-length=0 or is not present.

This option should be set to true for those HTTP clients that do not send streamed data.

copyHeaders

true

From Camel 2.16: if this option is true then IN exchange headers will be copied to OUT exchange headers according to copy strategy.

Setting this to false, allows to only include the headers from the HTTP response (not propagating IN headers).

okStatusCodeRange

200-299

From Camel 2.16: the range of HTTP status codes for which a response is considered a success. The values are inclusive. The range must be in the form from-to, dash included.

ignoreResponseBody

false

From Camel 2.16: when true the HttpProducer will not read the response body nor cache the input stream.

cookieHandler

null

From Camel: 2.19: configure a cookie handler to maintain a HTTP session

Authentication and Proxy

The following authentication options can also be set on the HttpEndpoint:

confluenceTableSmall

Name

Default Value

Description

authMethod

null

Authentication method, either as Basic, Digest or NTLM.

authMethodPriority

null

Priority of authentication methods. Is a list separated with comma.

For example: Basic,Digest to exclude NTLM.

authUsername

null

Username for authentication.

authPassword

null

Password for authentication.

authDomain

null

Domain for NTLM authentication.

authHost

null

Optional host for NTLM authentication.

proxyHost

null

The proxy host name.

proxyPort

null

The proxy port number.

proxyAuthMethod

null

Authentication method for proxy, either as Basic, Digest or NTLM.

proxyAuthUsername

null

Username for proxy authentication.

proxyAuthPassword

null

Password for proxy authentication.

proxyAuthDomain

null

Domain for proxy NTLM authentication.

proxyAuthHost

null

Optional host for proxy NTLM authentication.

When using authentication you must provide the choice of method for the authMethod or authProxyMethod options. You can configure the proxy and authentication details on either the HttpComponent or the HttpEndoint. Values provided on the HttpEndpoint will take precedence over HttpComponent. Its most likely best to configure this on the HttpComponent which allows you to do this once.

The HTTP component uses convention over configuration which means that if you have not explicit set a authMethodPriority then it will fallback and use the select(ed) authMethod as priority as well. So if you use authMethod.Basic then the auhtMethodPriority will be Basic only.

Notecamel-http is based on HttpClient v3.x and as such has only limited support for what is known as NTLMv1, the early version of the NTLM protocol. It does not support NTLMv2 at all. camel-http4 has support for NTLMv2.

HttpComponent Options

confluenceTableSmall

Name

Default Value

Description

httpBinding

null

To use a custom org.apache.camel.component.http.HttpBinding.

httpClientConfigurer

null

To use a custom org.apache.camel.component.http.HttpClientConfigurer.

httpConnectionManager

null

To use a custom org.apache.commons.httpclient.HttpConnectionManager.

httpConfiguration

null

To use a custom org.apache.camel.component.http.HttpConfiguration.

allowJavaSerializedObject

false

Camel 2.16.1/2.15.5: Whether to allow java serialization when a request uses context-type=application/x-java-serialized-object.

If you enable this then be aware that Java will deserialize the incoming data from the request to Java and that can be a potential security risk.

HttpConfiguration contains all the options listed in the table above under the section HttpConfiguration - Setting Authentication and Proxy.

Message Headers

confluenceTableSmall

Name

Type

Description

Exchange.HTTP_URI

String

URI to call. Will override existing URI set directly on the endpoint. This URI is the URI of the HTTP server to call. Its not the same as the Camel endpoint URI, where you can configure endpoint options such as security etc. This header does not support that, its only the URI of the HTTP server.

Exchange.HTTP_METHOD

String

HTTP method/verb to use.

Can be one of:

  • GET
  • POST
  • PUT
  • DELETE
  • HEAD
  • OPTIONS
  • TRACE

Exchange.HTTP_PATH

String

The request URI's path. The header will be used to build the request URI with the HTTP_URI.

From Camel 2.3.0: if the path starts with a /, the HttpProducer will try to find the relative path based on the Exchange.HTTP_BASE_URI header or the exchange.getFromEndpoint().getEndpointUri();.

Exchange.HTTP_QUERY

String

URI parameters. Will override existing URI parameters set directly on the endpoint.

Exchange.HTTP_RESPONSE_CODE

int

The HTTP response code from the external server. Is 200 for OK.

Exchange.HTTP_CHARACTER_ENCODING

String

Character encoding.

Exchange.CONTENT_TYPE

String

The HTTP content type. Is set on both the IN and OUT message to provide a content type, such as text/html.

Exchange.CONTENT_ENCODING

String

The HTTP content encoding. Is set on both the IN and OUT message to provide a content encoding, such as gzip.

Exchange.HTTP_SERVLET_REQUEST

HttpServletRequest

The HttpServletRequest object.

Exchange.HTTP_SERVLET_RESPONSE

HttpServletResponse

The HttpServletResponse object.

Exchange.HTTP_PROTOCOL_VERSION

String

From Camel 2.5: You can set the HTTP protocol version with this header, e.g., HTTP/1.0. If the header is not present the HttpProducer will use the default value HTTP/1.1.

Note: The header names above are constants. For the spring DSL you have to use the value of the constant instead of the name.

Message Body

Camel will store the HTTP response from the external server on the OUT body. All headers from the IN message will be copied to the OUT message, so headers are preserved during routing. Additionally Camel will add the HTTP response headers as well to the OUT message headers.

Response Code

Camel will handle according to the HTTP response code:

  • Response code is in the range 100..299, Camel regards it as a success response.
  • Response code is in the range 300..399, Camel regards it as a redirection response and will throw a HttpOperationFailedException with the information.
  • Response code is 400+, Camel regards it as an external server failure and will throw a HttpOperationFailedException with the information.

    throwExceptionOnFailure

    The option, throwExceptionOnFailure, can be set to false to prevent the HttpOperationFailedException from being thrown for failed response codes. This allows you to get any response from the remote server.
    There is a sample below demonstrating this.

HttpOperationFailedException

This exception contains the following information:

  • The HTTP status code.
  • The HTTP status line (text of the status code).
  • Redirect location, if server returned a redirect.
  • Response body as a java.lang.String, if server provided a body as response.

Calling Using GET or POST

The following algorithm is used to determine if either GET or POST HTTP method should be used:

  1. Use method provided in header.
  2. GET if query string is provided in header.
  3. GET if endpoint is configured with a query string.
  4. POST if there is data to send (body is not null).
  5. GET otherwise.

How To Access The HttpServletRequest and HttpServletResponse

You can get access to these two using the Camel type converter system using:

javaHttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class); HttpServletRequest response = exchange.getIn().getBody(HttpServletResponse.class);

Using Client Timeout - SO_TIMEOUT

See the unit test in this link

More Examples

Configuring a Proxy

Java DSL

from("direct:start") .to("http://oldhost?proxyHost=www.myproxy.com&proxyPort=80");

There is also support for proxy authentication via the proxyUsername and proxyPassword options.

Using Proxy Settings Outside of the URI

Java DSL

Spring DSL

context.getProperties().put("http.proxyHost", "172.168.18.9"); context.getProperties().put("http.proxyPort" "8080"); <camelContext> <properties> <property key="http.proxyHost" value="172.168.18.9"/> <property key="http.proxyPort" value="8080"/> </properties> </camelContext>

Options on Endpoint will override options on the context.

Configuring charset

If you are using POST to send data you can configure the charset:

.setProperty(Exchange.CHARSET_NAME, "iso-8859-1");

Sample with Scheduled Poll

The sample polls the Google homepage every 10 seconds and write the page to the file message.html:

javafrom("timer://foo?fixedRate=true&delay=0&period=10000") .to("http://www.google.com") .setHeader(FileComponent.HEADER_FILE_NAME, "message.html") .to("file:target/google");

Getting the Response Code

You can get the HTTP response code from the HTTP component by getting the value from the OUT message header with Exchange.HTTP_RESPONSE_CODE:

javaExchange exchange = template.send("http://www.google.com/search", new Processor() { public void process(Exchange exchange) throws Exception { exchange.getIn().setHeader(Exchange.HTTP_QUERY, constant("hl=en&q=activemq")); } }); Message out = exchange.getOut(); int responseCode = out.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);

Using throwExceptionOnFailure=false To Obtain All Server Responses

In the route below we want to route a message that we enrich with data returned from a remote HTTP call. As we want all responses from the remote server, we set the throwExceptionOnFailure=false so we get any response in the AggregationStrategy. As the code is based on a unit test that simulates a HTTP status code 404, there is some assertion code etc.{snippet:id=e1|lang=java|url=camel/tags/camel-2.2.0/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySimplifiedHandle404Test.java}

Disabling Cookies

To disable cookies you can set the HTTP Client to ignore cookies by adding this URI option: httpClient.cookiePolicy=ignoreCookies

Advanced Usage

If you need more control over the HTTP producer you should use the HttpComponent where you can set various classes to give you custom behavior.

Setting MaxConnectionsPerHost

The HTTP Component has a org.apache.commons.httpclient.HttpConnectionManager where you can configure various global configuration for the given component. By global, we mean that any endpoint the component creates has the same shared HttpConnectionManager. So, if we want to set a different value for the max connection per host, we need to define it on the HTTP component and not on the endpoint URI that we usually use. So here comes:

First, we define the http component in Spring XML. Yes, we use the same scheme name, http, because otherwise Camel will auto-discover and create the component with default settings. What we need is to overrule this so we can set our options. In the sample below we set the max connection to 5 instead of the default of 2.{snippet:id=e1|lang=xml|url=camel/tags/camel-2.2.0/tests/camel-itest/src/test/resources/org/apache/camel/itest/http/HttpMaxConnectionPerHostTest-context.xml}And then we can just use it as we normally do in our routes:{snippet:id=e2|lang=xml|url=camel/tags/camel-2.2.0/tests/camel-itest/src/test/resources/org/apache/camel/itest/http/HttpMaxConnectionPerHostTest-context.xml}

Using Pre-Emptive Authentication

If an HTTP server should fail to respond correctly with an expected 401 Authorization Required response for a failed authentication attempt a client can instead use preemptive authentication by specifying the URI option: httpClient.authenticationPreemptive=true.

Accepting Self-Signed Certificates From Remote Server

See this link from a mailing list discussion with some code to outline how to do this with the Apache Commons HTTP API.

Setting up SSL for HTTP Client

Using the JSSE Configuration Utility

From Camel 2.8: the HTTP4 component supports SSL/TLS configuration through the Camel JSSE Configuration Utility.  This utility greatly decreases the amount of component specific code you need to write and is configurable at the endpoint and component levels.  The following examples demonstrate how to use the utility with the HTTP4 component.

The version of the Apache HTTP client used in this component resolves SSL/TLS information from a global "protocol" registry.  This component provides an implementation, org.apache.camel.component.http.SSLContextParametersSecureProtocolSocketFactory, of the HTTP client's protocol socket factory in order to support the use of the Camel JSSE Configuration utility.  The following example demonstrates how to configure the protocol registry and use the registered protocol information in a route.

javaKeyStoreParameters ksp = new KeyStoreParameters(); ksp.setResource("/users/home/server/keystore.jks"); ksp.setPassword("keystorePassword"); KeyManagersParameters kmp = new KeyManagersParameters(); kmp.setKeyStore(ksp); kmp.setKeyPassword("keyPassword"); SSLContextParameters scp = new SSLContextParameters(); scp.setKeyManagers(kmp); ProtocolSocketFactory factory = new SSLContextParametersSecureProtocolSocketFactory(scp); Protocol.registerProtocol("https", new Protocol("https", factory, 443)); from("direct:start") .to("https://mail.google.com/mail/") .to("mock:results");
Configuring Apache HTTP Client Directly

Basically camel-http component is built on the top of Apache HTTP client, and you can implement a custom org.apache.camel.component.http.HttpClientConfigurer to do some configuration on the HTTP client if you need full control of it.

However, if you just want to specify the keystore and truststore you can do this with Apache HTTP HttpClientConfigurer, for example:

javaProtocol authhttps = new Protocol("https", new AuthSSLProtocolSocketFactory(new URL("file:my.keystore"), "mypassword", new URL("file:my.truststore"), "mypassword"), 443); Protocol.registerProtocol("https", authhttps);

And then you need to create a class that implements HttpClientConfigurer, and registers HTTPS protocol providing a keystore or truststore per example above. Then, from your Camel RouteBuilder class you can hook it up like so:

javaHttpComponent httpComponent = getContext().getComponent("http", HttpComponent.class); httpComponent.setHttpClientConfigurer(new MyHttpClientConfigurer());

If you are doing this using the Spring DSL, you can specify your HttpClientConfigurer using the URI. For example:

xml<bean id="myHttpClientConfigurer" class="my.https.HttpClientConfigurer"/> <to uri="https://myhostname.com:443/myURL?httpClientConfigurerRef=myHttpClientConfigurer"/>

As long as you implement the HttpClientConfigurer and configure your keystore and truststore as described above, it will work fine.

Endpoint See Also

iBATIS

The ibatis: component allows you to query, poll, insert, update and delete data in a relational database using Apache iBATIS.

Prefer MyBatis

The Apache iBatis project is no longer active. The project is moved outside Apache and is now know as the MyBatis project.
Therefore we encourage users to use MyBatis instead. This camel-ibatis component will be removed in Camel 3.0.

iBatis do not support Spring 4.x. So you can only use Spring 3.x or older with iBatis.

Maven users will need to add the following dependency to their pom.xml for this component:

xml<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-ibatis</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>

URI format

ibatis:statementName[?options]

Where statementName is the name in the iBATIS XML configuration file which maps to the query, insert, update or delete operation you wish to evaluate.

You can append query options to the URI in the following format, ?option=value&option=value&...

This component will by default load the iBatis SqlMapConfig file from the root of the classpath and expected named as SqlMapConfig.xml.
It uses Spring resource loading so you can define it using classpath, file or http as prefix to load resources with those schemes.
In Camel 2.2 you can configure this on the iBatisComponent with the setSqlMapConfig(String) method.

Options

confluenceTableSmall

Option

Type

Default

Description

consumer.onConsume

String

null

Statements to run after consuming. Can be used, for example, to update rows after they have been consumed and processed in Camel. See sample later. Multiple statements can be separated with comma.

consumer.useIterator

boolean

true

If true each row returned when polling will be processed individually. If false the entire List of data is set as the IN body.

consumer.routeEmptyResultSet

boolean

false

Sets whether empty result set should be routed or not. By default, empty result sets are not routed.

statementType

StatementType

null

Mandatory to specify for IbatisProducer to control which iBatis SqlMapClient method to invoke. The enum values are: QueryForObject, QueryForList, Insert, Update, Delete.

maxMessagesPerPoll

int

0

An integer to define a maximum messages to gather per poll. By default, no maximum is set. Can be used to set a limit of e.g. 1000 to avoid when starting up the server that there are thousands of files. Set a value of 0 or negative to disabled it.

isolation

String

TRANSACTION_REPEATABLE_READ

Camel 2.9: A String the defines the transaction isolation level of the will be used. Allowed values are TRANSACTION_NONE, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_READ_COMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE

isolation

String

TRANSACTION_REPEATABLE_READ

Camel 2.9: A String the defines the transaction isolation level of the will be used. Allowed values are TRANSACTION_NONE, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_READ_COMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE

Message Headers

Camel will populate the result message, either IN or OUT with a header with the operationName used:

confluenceTableSmall

Header

Type

Description

CamelIBatisStatementName

String

The statementName used (for example: insertAccount).

CamelIBatisResult

Object

The response returned from iBatis in any of the operations. For instance an INSERT could return the auto-generated key, or number of rows etc.

Message Body

The response from iBatis will only be set as body if it's a SELECT statement. That means, for example, for INSERT statements Camel will not replace the body. This allows you to continue routing and keep the original body. The response from iBatis is always stored in the header with the key CamelIBatisResult.

Samples

For example if you wish to consume beans from a JMS queue and insert them into a database you could do the following:

from("activemq:queue:newAccount"). to("ibatis:insertAccount?statementType=Insert");

Notice we have to specify the statementType, as we need to instruct Camel which SqlMapClient operation to invoke.

Where insertAccount is the iBatis ID in the SQL map file:

xml <!-- Insert example, using the Account parameter class --> <insert id="insertAccount" parameterClass="Account"> insert into ACCOUNT ( ACC_ID, ACC_FIRST_NAME, ACC_LAST_NAME, ACC_EMAIL ) values ( #id#, #firstName#, #lastName#, #emailAddress# ) </insert>

Using StatementType for better control of IBatis

When routing to an iBatis endpoint you want more fine grained control so you can control whether the SQL statement to be executed is a SELEECT, UPDATE, DELETE or INSERT etc. So for instance if we want to route to an iBatis endpoint in which the IN body contains parameters to a SELECT statement we can do:

{snippet:id=e1|lang=java|url=camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisQueryForObjectTest.java}

In the code above we can invoke the iBatis statement selectAccountById and the IN body should contain the account id we want to retrieve, such as an Integer type.

We can do the same for some of the other operations, such as QueryForList:

{snippet:id=e1|lang=java|url=camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisQueryForListTest.java}

And the same for UPDATE, where we can send an Account object as IN body to iBatis:

{snippet:id=e1|lang=java|url=camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisQueryForUpdateTest.java}

Scheduled polling example

Since this component does not support scheduled polling, you need to use another mechanism for triggering the scheduled polls, such as the Timer or Quartz components.

In the sample below we poll the database, every 30 seconds using the Timer component and send the data to the JMS queue:

javafrom("timer://pollTheDatabase?delay=30000").to("ibatis:selectAllAccounts?statementType=QueryForList").to("activemq:queue:allAccounts");

And the iBatis SQL map file used:

xml <!-- Select with no parameters using the result map for Account class. --> <select id="selectAllAccounts" resultMap="AccountResult"> select * from ACCOUNT </select>

Using onConsume

This component supports executing statements after data have been consumed and processed by Camel. This allows you to do post updates in the database. Notice all statements must be UPDATE statements. Camel supports executing multiple statements whose name should be separated by comma.

The route below illustrates we execute the consumeAccount statement data is processed. This allows us to change the status of the row in the database to processed, so we avoid consuming it twice or more.

{snippet:id=e1|lang=java|url=camel/trunk/components/camel-ibatis/src/test/java/org/apache/camel/component/ibatis/IBatisQueueTest.java}

And the statements in the sqlmap file:

{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-ibatis/src/test/resources/org/apache/camel/component/ibatis/Account.xml}{snippet:id=e2|lang=xml|url=camel/trunk/components/camel-ibatis/src/test/resources/org/apache/camel/component/ibatis/Account.xml}

Endpoint See Also

IRC Component

The irc component implements an IRC (Internet Relay Chat) transport.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-irc</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

irc:nick@host[:port]/#room[?options]
irc:nick@host[:port]?channels=#channel1,#channel2,#channel3[?options]

You can append query options to the URI in the following format, ?option=value&option=value&...

Options

Name

Description

Example

Default Value

channels

Comma separated list of IRC channels to join.

channels=#channel1,#channel2

null

nickname

The nickname used in chat.

irc:MyNick@irc.server.org#channel or irc:irc.server.org#channel?nickname=MyUser

null

username

The IRC server user name.

irc:MyUser@irc.server.org#channel or irc:irc.server.org#channel?username=MyUser

Same as nickname.

password

The IRC server password.

password=somepass

None

realname

The IRC user's actual name.

realname=MyName

None

nickPasswordCamel 2.18: Your IRC server nickname password.nickPassword=mysecretNone

colors

Whether or not the server supports color codes.

true, false

true

onReply

Whether or not to handle general responses to commands or informational messages.

true, false

false

onNick

Handle nickname change events.

true, false

true

onQuit

Handle user quit events.

true, false

true

onJoin

Handle user join events.

true, false

true

onKick

Handle kick events.

true, false

true

onMode

Handle mode change events.

true, false

true

onPart

Handle user part events.

true, false

true

onTopic

Handle topic change events.

true, false

true

onPrivmsg

Handle message events.

true, false

true

trustManager

The trust manager used to verify the SSL server's certificate.

trustManager=#referenceToTrustManagerBean

The default trust manager, which accepts all certificates, will be used.

keys

Camel 2.2: Comma separated list of IRC channel keys. Important to be listed in same order as channels. When joining multiple channels with only some needing keys just insert an empty value for that channel.

irc:MyNick@irc.server.org/#channel?keys=chankey

null

sslContextParameters

Camel 2.9: Reference to a org.apache.camel.util.jsse.SSLContextParameters in the Registry.  This reference overrides any configured SSLContextParameters at the component level.  See Using the JSSE Configuration Utility.  Note that this setting overrides the trustManager option.

#mySslContextParameters

null

SSL Support

Using the JSSE Configuration Utility

As of Camel 2.9, the IRC component supports SSL/TLS configuration through the Camel JSSE Configuration Utility.  This utility greatly decreases the amount of component specific code you need to write and is configurable at the endpoint and component levels.  The following examples demonstrate how to use the utility with the IRC component.

Programmatic configuration of the endpoint
KeyStoreParameters ksp = new KeyStoreParameters();
ksp.setResource("/users/home/server/truststore.jks");
ksp.setPassword("keystorePassword");

TrustManagersParameters tmp = new TrustManagersParameters();
tmp.setKeyStore(ksp);

SSLContextParameters scp = new SSLContextParameters();
scp.setTrustManagers(tmp);

Registry registry = ...
registry.bind("sslContextParameters", scp);

...

from(...)
    .to("ircs://camel-prd-user@server:6669/#camel-test?nickname=camel-prd&password=password&sslContextParameters=#sslContextParameters");

Spring DSL based configuration of endpoint
...
  <camel:sslContextParameters
      id="sslContextParameters">
    <camel:trustManagers>
      <camel:keyStore
          resource="/users/home/server/truststore.jks"
          password="keystorePassword"/>
    </camel:keyManagers>
  </camel:sslContextParameters>...
...
  <to uri="ircs://camel-prd-user@server:6669/#camel-test?nickname=camel-prd&password=password&sslContextParameters=#sslContextParameters"/>...

Using the legacy basic configuration options

You can also connect to an SSL enabled IRC server, as follows:

ircs:host[:port]/#room?username=user&password=pass

By default, the IRC transport uses SSLDefaultTrustManager. If you need to provide your own custom trust manager, use the trustManager parameter as follows:

ircs:host[:port]/#room?username=user&password=pass&trustManager=#referenceToMyTrustManagerBean

Using keys

Available as of Camel 2.2

Some irc rooms requires you to provide a key to be able to join that channel. The key is just a secret word.

For example we join 3 channels where as only channel 1 and 3 uses a key.

irc:nick@irc.server.org?channels=#chan1,#chan2,#chan3&keys=chan1Key,,chan3key

JBI Component

The jbi component is implemented by the ServiceMix Camel module and provides integration with a JBI Normalized Message Router, such as the one provided by Apache ServiceMix.

See below for information about how to use StreamSource types from ServiceMix in Camel.

The following code:

from("jbi:endpoint:http://foo.bar.org/MyService/MyEndpoint")

Automatically exposes a new endpoint to the bus, where the service QName is {http://foo.bar.org}MyService and the endpoint name is MyEndpoint (see #URI-format).

When a JBI endpoint appears at the end of a route, for example:

to("jbi:endpoint:http://foo.bar.org/MyService/MyEndpoint")

The messages sent by this producer endpoint are sent to the already deployed JBI endpoint.

URI format

jbi:service:serviceNamespace[sep]serviceName[?options]
jbi:endpoint:serviceNamespace[sep]serviceName[sep]endpointName[?options]
jbi:name:endpointName[?options]

The separator that should be used in the endpoint URL is:

  • / (forward slash), if serviceNamespace starts with http://, or
  • : (colon), if serviceNamespace starts with urn:foo:bar.

For more details of valid JBI URIs see the ServiceMix URI Guide.

Using the jbi:service: or jbi:endpoint: URI formats sets the service QName on the JBI endpoint to the one specified. Otherwise, the default Camel JBI Service QName is used, which is:

{http://activemq.apache.org/camel/schema/jbi}endpoint

You can append query options to the URI in the following format, ?option=value&option=value&...

Examples

jbi:service:http://foo.bar.org/MyService
jbi:endpoint:urn:foo:bar:MyService:MyEndpoint
jbi:endpoint:http://foo.bar.org/MyService/MyEndpoint
jbi:name:cheese

URI options

Name

Default value

Description

mep

MEP of the Camel Exchange

Allows users to override the MEP set on the Exchange object. Valid values for this option are in-only, in-out, robust-in-out and in-optional-out.

operation

Value of the jbi.operation header property

Specifies the JBI operation for the MessageExchange. If no value is supplied, the JBI binding will use the value of the jbi.operation header property.

serialization

basic

Default value (basic) will check if headers are serializable by looking at the type, setting this option to strict will detect objects that can not be serialized although they implement the Serializable interface. Set to nocheck to disable this check altogether, note that this should only be used for in-memory transports like SEDAFlow, otherwise you can expect to get NotSerializableException thrown at runtime.

convertException

false

false: send any exceptions thrown from the Camel route back unmodified
true: convert all exceptions to a JBI FaultException (can be used to avoid non-serializable exceptions or to implement generic error handling

Examples

jbi:service:http://foo.bar.org/MyService?mep=in-out       (override the MEP, use InOut JBI MessageExchanges)
jbi:endpoint:urn:foo:bar:MyService:MyEndpoint?mep=in      (override the MEP, use InOnly JBI MessageExchanges)  
jbi:endpoint:urn:foo:bar:MyService:MyEndpoint?operation={http://www.mycompany.org}AddNumbers 
 (overide the operation for the JBI Exchange to {http://www.mycompany.org}AddNumbers)

Using Stream bodies

If you are using a stream type as the message body, you should be aware that a stream is only capable of being read once. So if you enable DEBUG logging, the body is usually logged and thus read. To deal with this, Camel has a streamCaching option that can cache the stream, enabling you to read it multiple times.

from("jbi:endpoint:http://foo.bar.org/MyService/MyEndpoint").streamCaching().to("xslt:transform.xsl", "bean:doSomething");

The stream caching is default enabled, so it is not necessary to set the streamCaching() option.
We store big input streams (by default, over 64K) in a temp file using CachedOutputStream. When you close the input stream, the temp file will be deleted.

Creating a JBI Service Unit

If you have some Camel routes that you want to deploy inside JBI as a Service Unit, you can use the JBI Service Unit Archetype to create a new Maven project for the Service Unit.

If you have an existing Maven project that you need to convert into a JBI Service Unit, you may want to consult ServiceMix Maven JBI Plugins for further help. The key steps are as follows:

  • Create a Spring XML file at src/main/resources/camel-context.xml to bootstrap your routes inside the JBI Service Unit.
  • Change the POM file's packaging to jbi-service-unit.

Your pom.xml should look something like this to enable the jbi-service-unit packaging:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>myGroupId</groupId>
  <artifactId>myArtifactId</artifactId>
  <packaging>jbi-service-unit</packaging>
  <version>1.0-SNAPSHOT</version>

  <name>A Camel based JBI Service Unit</name>

  <url>http://www.myorganization.org</url>

  <properties>
    <camel-version>x.x.x</camel-version>
    <servicemix-version>3.3</servicemix-version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.servicemix</groupId>
      <artifactId>servicemix-camel</artifactId>
      <version>${servicemix-version}</version>
    </dependency>

    <dependency>
      <groupId>org.apache.servicemix</groupId>
      <artifactId>servicemix-core</artifactId>
      <version>${servicemix-version}</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <defaultGoal>install</defaultGoal>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
        </configuration>
      </plugin>

      <!-- creates the JBI deployment unit -->
      <plugin>
        <groupId>org.apache.servicemix.tooling</groupId>
        <artifactId>jbi-maven-plugin</artifactId>
        <version>${servicemix-version}</version>
        <extensions>true</extensions>
      </plugin>
    </plugins>
  </build>
</project>

JCR Component

The jcr component allows you to add/read nodes to/from a JCR compliant content repository (for example, Apache Jackrabbit) with its producer, or register an EventListener with the consumer.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jcr</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

jcr://user:password@repository/path/to/node

Consumer added

From Camel 2.10 onwards you can use consumer as an EventListener in JCR or a producer to read a node by identifier.

Usage

The repository element of the URI is used to look up the JCR Repository object in the Camel context registry.

Producer

Name

Default Value

Description

CamelJcrOperation

CamelJcrInsert

CamelJcrInsert or CamelJcrGetById operation to use

CamelJcrNodeName

null

Used to determine the node name to use.

CamelJcrNodeTypenullCamel 2.16: To use a specify primary node type when creating adding a new node.

When a message is sent to a JCR producer endpoint:

  • If the operation is CamelJcrInsert: A new node is created in the content repository, all the message headers of the IN message are transformed to javax.jcr.Value instances and added to the new node and the node's UUID is returned in the OUT message.
  • If the operation is CamelJcrGetById: A new node is retrieved from the repository using the message body as node identifier.

Please note that the JCR Producer used message properties instead of message headers in Camel versions earlier than 2.12.3. See https://issues.apache.org/jira/browse/CAMEL-7067 for more details.

 

Consumer

The consumer will connect to JCR periodically and return a List<javax.jcr.observation.Event> in the message body.

Name

Default Value

Description

eventTypes

0

A combination of one or more event types encoded as a bit mask value such as javax.jcr.observation.Event.NODE_ADDED, javax.jcr.observation.Event.NODE_REMOVED, etc.

deep

false

When it is true, events whose associated parent node is at current path or within its subgraph are received.

uuids

null

Only events whose associated parent node has one of the identifiers in the comma separated uuid list will be received.

nodeTypeNames

null

Only events whose associated parent node has one of the node types (or a subtype of one of the node types) in this list will be received.

noLocal

false

If noLocal is true, then events generated by the session through which the listener was registered are ignored. Otherwise, they are not ignored.

sessionLiveCheckInterval

60000

Interval in milliseconds to wait before each session live checking.

sessionLiveCheckIntervalOnStart

3000

Interval in milliseconds to wait before the first session live checking.

username

 

Camel 2.15: Allows to specify the username as a uri parameter instead of in the authority section of the uri

password

 

Camel 2.15: Allows to specify the password as a uri parameter instead of in the authority section of the uri

workspaceName

null

Camel 2.16: Allows to specify a workspace different from default

Example

The snippet below creates a node named node under the /home/test node in the content repository. One additional property is added to the node as well: my.contents.property which will contain the body of the message being sent.

from("direct:a").setHeader(JcrConstants.JCR_NODE_NAME, constant("node"))
    .setHeader("my.contents.property", body())
    .to("jcr://user:pass@repository/home/test");

 

The following code will register an EventListener under the path import-application/inbox for Event.NODE_ADDED and Event.NODE_REMOVED events (event types 1 and 2, both masked as 3) and listening deep for all the children.

<route>
    <from uri="jcr://user:pass@repository/import-application/inbox?eventTypes=3&deep=true" />
    <to uri="direct:execute-import-application" />
</route>

JDBC Component

The jdbc component enables you to access databases through JDBC, where SQL queries (SELECT) and operations (INSERT, UPDATE, etc) are sent in the message body. This component uses the standard JDBC API, unlike the SQL Component component, which uses spring-jdbc.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jdbc</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>

This component can only be used to define producer endpoints, which means that you cannot use the JDBC component in a from() statement.

URI format

jdbc:dataSourceName[?options]

This component only supports producer endpoints.

You can append query options to the URI in the following format, ?option=value&option=value&...

Options

confluenceTableSmall

Name

Default Value

Description

readSize

0

The default maximum number of rows that can be read by a polling query. The default value is 0.

statement.<xxx>

null

Camel 2.1: Sets additional options on the java.sql.Statement that is used behind the scenes to execute the queries. For instance, statement.maxRows=10. For detailed documentation, see the java.sql.Statement javadoc documentation.

useJDBC4ColumnNameAndLabelSemantics

true

Camel 2.2: Sets whether to use JDBC 4/3 column label/name semantics. You can use this option to turn it false in case you have issues with your JDBC driver to select data. This only applies when using SQL SELECT using aliases (e.g. SQL SELECT id as identifier, name as given_name from persons).

resetAutoCommit

true

Camel 2.9: If true, Camel will set the autoCommit on the JDBC connection to be false, commit the change after executing the statement and reset the autoCommit flag of the connection at the end. If the JDBC connection does not support resetting the autoCommit flag, set this to false.
When used with XA transactions you most likely need to set it to false so that the transaction manager is in charge of committing this tx.

allowNamedParameters

true

Camel 2.12: Whether to allow using named parameters in the queries.

prepareStatementStrategy

 

Camel 2.12: Allows to plugin to use a custom org.apache.camel.component.jdbc.JdbcPrepareStatementStrategy to control preparation of the query and prepared statement.

useHeadersAsParameters

false

Camel 2.12: Set this option to true to use the prepareStatementStrategy with named parameters. This allows to define queries with named placeholders, and use headers with the dynamic values for the query placeholders.

outputType

SelectList

Camel 2.12.1: outputType='SelectList', for consumer or producer, will output a List of Map. SelectOne will output single Java object in the following way:
a) If the query has only single column, then that JDBC Column object is returned. (such as SELECT COUNT( * ) FROM PROJECT will return a Long object.
b) If the query has more than one column, then it will return a Map of that result.
c) If the outputClass is set, then it will convert the query result into an Java bean object by calling all the setters that match the column names. It will assume your class has a default constructor to create instance with. From Camel 2.14 onwards then SelectList is also supported.
d) If the query resulted in more than one rows, it throws an non-unique result exception.
Camel 2.14.0: New StreamList output type value that streams the result of the query using an Iterator<Map<String, Object>>, it can be used along with the Splitter EIP.

outputClass

null

Camel 2.12.1: Specify the full package and class name to use as conversion when outputType=SelectOne. From Camel 2.14 onwards then SelectList is also supported.

beanRowMapper

 

Camel 2.12.1: To use a custom org.apache.camel.component.jdbc.BeanRowMapper when using outputClass. The default implementation will lower case the row names and skip underscores, and dashes. For example "CUST_ID" is mapped as "custId".

useGetBytesForBlobfalseCamel 2.16: To read BLOB columns as bytes instead of string data. This may be needed for certain databases such as Oracle where you must read BLOB columns as bytes.

Result

By default the result is returned in the OUT body as an ArrayList<HashMap<String, Object>>. The List object contains the list of rows and the Map objects contain each row with the String key as the column name. You can use the option outputType to control the result.

Note: This component fetches ResultSetMetaData to be able to return the column name as the key in the Map.

Message Headers

Header

Description

CamelJdbcRowCount

If the query is a SELECT, query the row count is returned in this OUT header.

CamelJdbcUpdateCount

If the query is an UPDATE, query the update count is returned in this OUT header.

CamelGeneratedKeysRows

Camel 2.10: Rows that contains the generated keys.

CamelGeneratedKeysRowCount

Camel 2.10: The number of rows in the header that contains generated keys.

CamelJdbcColumnNames

Camel 2.11.1: The column names from the ResultSet as a java.util.Set type.

CamelJdbcParameters

Camel 2.12: A java.util.Map which has the headers to be used if useHeadersAsParameters has been enabled.

Generated keys

Available as of Camel 2.10

If you insert data using SQL INSERT, then the RDBMS may support auto generated keys. You can instruct the JDBC producer to return the generated keys in headers.
To do that set the header CamelRetrieveGeneratedKeys=true. Then the generated keys will be provided as headers with the keys listed in the table above.

You can see more details in this unit test.

Using generated keys does not work with together with named parameters.

Using named parameters

Available as of Camel 2.12

In the given route below, we want to get all the projects from the projects table. Notice the SQL query has 2 named parameters, :?lic and :?min.
Camel will then lookup these parameters from the message headers. Notice in the example above we set two headers with constant value
for the named parameters:

from("direct:projects") .setHeader("lic", constant("ASF")) .setHeader("min", constant(123)) .setBody("select * from projects where license = :?lic and id > :?min order by id") .to("jdbc:myDataSource?useHeadersAsParameters=true")

You can also store the header values in a java.util.Map and store the map on the headers with the key CamelJdbcParameters.

Samples

In the following example, we fetch the rows from the customer table.

First we register our datasource in the Camel registry as testdb:{snippet:id=register|lang=java|url=camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/AbstractJdbcTestSupport.java}Then we configure a route that routes to the JDBC component, so the SQL will be executed. Note how we refer to the testdb datasource that was bound in the previous step:{snippet:id=route|lang=java|url=camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java}Or you can create a DataSource in Spring like this:{snippet:id=example|lang=java|url=camel/trunk/components/camel-jdbc/src/test/resources/org/apache/camel/component/jdbc/camelContext.xml}We create an endpoint, add the SQL query to the body of the IN message, and then send the exchange. The result of the query is returned in the OUT body:{snippet:id=invoke|lang=java|url=camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteTest.java}If you want to work on the rows one by one instead of the entire ResultSet at once you need to use the Splitter EIP such as:

In Camel 2.13.x or older{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcRouteSplitTest.java}In Camel 2.14.x or newer

from("direct:hello") // here we split the data from the testdb into new messages one by one // so the mock endpoint will receive a message per row in the table // the StreamList option allows to stream the result of the query without creating a List of rows // and notice we also enable streaming mode on the splitter .to("jdbc:testdb?outputType=StreamList") .split(body()).streaming() .to("mock:result");


Sample - Polling the database every minute

If we want to poll a database using the JDBC component, we need to combine it with a polling scheduler such as the Timer or Quartz etc. In the following example, we retrieve data from the database every 60 seconds:

javafrom("timer://foo?period=60000").setBody(constant("select * from customer")).to("jdbc:testdb").to("activemq:queue:customers");

Sample - Move Data Between Data Sources

A common use case is to query for data, process it and move it to another data source (ETL operations). In the following example, we retrieve new customer records from the source table every hour, filter/transform them and move them to a destination table:

javafrom("timer://MoveNewCustomersEveryHour?period=3600000") .setBody(constant("select * from customer where create_time > (sysdate-1/24)")) .to("jdbc:testdb") .split(body()) .process(new MyCustomerProcessor()) //filter/transform results as needed .setBody(simple("insert into processed_customer values('${body[ID]}','${body[NAME]}')")) .to("jdbc:testdb");

 

Endpoint See Also

Jetty Component

The producer is deprecated - do not use. We only recommend using jetty as consumer (eg from jetty)

 

The jetty component provides HTTP-based endpoints for consuming and producing HTTP requests. That is, the Jetty component behaves as a simple Web server. Jetty can also be used as an HTTP client which mean you can also use it with Camel as a producer.

Stream

The assert call appears in this example, because the code is part of an unit test. Jetty is stream based, which means the input it receives is submitted to Camel as a stream. That means you will only be able to read the content of the stream once.

If you find a situation where the message body appears to be empty or you need to access the Exchange.HTTP_RESPONSE_CODE data multiple times, e.g., doing multi-casting, or re-delivery error handling, you should use Stream caching or convert the message body to a String which is safe to be re-read multiple times.

Maven users should add the following dependency to their pom.xml to use this component:

xml<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jetty</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>

URI Format

jetty:http://hostname[:port][/resourceUri][?options]

Query options should be appended to the URI using the following format: ?option=value&option=value&...

Options

confluenceTableSmall

Option

Default Value

Description

bridgeEndpoint

false

Camel 2.1: If the option is trueHttpProducer will ignore the Exchange.HTTP_URI header, and use the endpoint's URI for request. You may also set the throwExceptionOnFailure to be false to let the HttpProducer send all the fault response back.

Camel 2.3: If the option is true, HttpProducer and CamelServlet will skip the gzip processing if the Content-Encoding is gzip.

Consider setting disableStreamCache=true to optimize when bridging.

chunked

true

Camel 2.2: If this option is false Jetty Servlet will disable the HTTP streaming and set the Content-Length header on the response

continuationTimeout

null

Camel 2.6: Allows to set a timeout in milliseconds when using Jetty as consumer (server). By default Jetty uses 30000. You can use a value of <= 0 to never expire. If a timeout occurs then the request will be expired and Jetty will return back an HTTP error 503 to the client.

This option is only in use when using Jetty with the Asynchronous Routing Engine.

cookieHandler

null

Camel 2.19: Producer only Configure a cookie handler to maintain a HTTP session.

disableStreamCache

false

Camel 2.3: Determines whether or not the raw input stream from Jetty is cached or not (Camel will read the stream into a in memory/overflow to file, Stream caching) cache. By default Camel will cache the Jetty input stream to support reading it multiple times to ensure it Camel can retrieve all data from the stream. However, you can set this option to true when you for example need to access the raw stream, such as streaming it directly to a file or other persistent store. 

DefaultHttpBinding will copy the request input stream into a stream cache and put it into message body if this option is false to support reading the stream multiple times. If you use Jetty to bridge/proxy an endpoint then consider enabling this option to improve performance, in case you do not need to read the message payload multiple times.

enableCORS

false

Camel 2.15: if the option is true, Jetty server will setup the CrossOriginFilter which supports the CORS out of box.

enableJmx

false

Camel 2.3: If this option is true, Jetty JMX support will be enabled for this endpoint. See Jetty JMX support for more details.

enablemulti-partFilter

true

Camel 2.5: Whether Jetty org.eclipse.jetty.servlets.multi-partFilter is enabled or not.

Set this option to false when bridging endpoints, to ensure multi-part requests is proxied/bridged as well.

filterInit.xxx

null

Camel 2.17: Configuration for the InitParameters of filter.

For example, setting filterInit.parameter=value the parameter could be used when calling the filter init() method.

filtersRef

null

Camel 2.9: Allows using a custom filters which is putted into a list and can be find in the Registry

handlers

null

Specifies a comma-delimited set of org.mortbay.jetty.Handler instances in your Registry (such as your Spring ApplicationContext). These handlers are added to the Jetty Servlet context (for example, to add security).

Note: you can not use different handlers with different Jetty endpoints using the same port number. The handlers is associated to the port number. If you need different handlers, then use different port numbers.

headerFilterStrategy

null

Camel 2.11: Reference to a instance of org.apache.camel.spi.HeaderFilterStrategy in the Registry. It will be used to apply the custom headerFilterStrategy on the new create HttpJettyEndpoint.

httpBindingRef

null

Reference to an org.apache.camel.component.http.HttpBinding in the Registry. HttpBinding can be used to customize how a response should be written for the consumer.

httpClient.xxx

null

Configuration of Jetty's HttpClient. For example, setting httpClient.idleTimeout=30000 sets the idle timeout to 30 seconds. And httpClient.timeout=30000 sets the request timeout to 30 seconds, in case you want to timeout sooner if you have long running request/response calls.

httpClient

null

To use a shared org.eclipse.jetty.client.HttpClient for all producers created by this endpoint. This option should only be used in special circumstances.

httpClientMinThreads

null

Camel 2.11: Producer only: To set a value for minimum number of threads in HttpClient thread pool. This setting override any setting configured on component level. Notice that both a min and max size must be configured. If not set it default to min 8 threads used in Jetty's thread pool.

httpClientMaxThreads

null

Camel 2.11: Producer only: To set a value for maximum number of threads in HttpClient thread pool. This setting override any setting configured on component level. Notice that both a min and max size must be configured. If not set it default to max 16 threads used in Jetty's thread pool.

httpMethodRestrict

null

Camel 2.11: Consumer only: Used to only allow consuming if the HttpMethod matches, such as GET/POST/PUT etc. From Camel 2.15: multiple methods can be specified separated by comma.

jettyHttpBindingRef

null

Camel 2.6.0+: Reference to an org.apache.camel.component.jetty.JettyHttpBinding in the Registry. JettyHttpBinding can be used to customize how a response should be written for the producer.

matchOnUriPrefix

false

Whether or not the CamelServlet should try to find a target consumer by matching the URI prefix if no exact match is found.

See here How do I let Jetty match wildcards.

multi-partFilterRef

null

Camel 2.6: Allows using a custom multi-part filter.

Note: setting multi-partFilterRef forces the value of enablemulti-partFilter to true.

okStatusCodeRange

200-299

Camel 2.16: Producer only The status codes which is considered a success response. The values are inclusive. The range must be defined as from-to with the dash included.

optionsEnabled

false

Camel 2.17: Specifies whether to enable HTTP OPTIONS for this Jetty consumer. By default OPTIONS is turned off.

proxyHost

null

Camel 2.11: Producer only The HTTP proxy Host URL which will be used by Jetty client.

proxyPort

null

Camel 2.11: Producer only The HTTP proxy port which will be used by Jetty client.

responseBufferSize

null

Camel 2.12: To use a custom buffer size on the javax.servlet.ServletResponse.

sendDateHeader

false

Camel 2.14: if the option is true, jetty server will send the date header to the client which sends the request.

Note: ensure that there are no any other camel-jetty endpoints that share the same port, otherwise this option may not work as expected.

sendServerVersion

true

Camel 2.13: if the option is true, jetty will send the server header with the jetty version information to the client which sends the request.

Note: ensure that there are no any other camel-jetty endpoints that share the same port, otherwise this option may not work as expected.

sessionSupport

false

Specifies whether to enable the session manager on the server side of Jetty.

sslContextParameters

null

Camel 2.17: Reference to a org.apache.camel.util.jsse.SSLContextParameters in the Registry.  This reference overrides any configured SSLContextParameters at the component level.   

See Using the JSSE Configuration Utility.

sslContextParametersRef

null

Camel 2.8: Deprecated Reference to a org.apache.camel.util.jsse.SSLContextParameters in the Registry.  This reference overrides any configured SSLContextParameters at the component level. 

See Using the JSSE Configuration Utility.

throwExceptionOnFailure

true

Option to disable throwing the HttpOperationFailedException in case of failed responses from the remote server. This allows you to get all responses regardless of the HTTP status code.

traceEnabled

false

Specifies whether to enable HTTP TRACE for this Jetty consumer. By default TRACE is turned off.

transferException

false

Camel 2.6: If enabled and an Exchange failed processing on the consumer side, and if the caused Exception was send back serialized in the response as a application/x-java-serialized-object content type.

On the producer side the exception will be deserialized and thrown as is, instead of the HttpOperationFailedException. The caused exception is required to be serialized.

urlRewrite

null

Camel 2.11: Producer only Refers to a custom org.apache.camel.component.http.UrlRewrite which allows you to rewrite URLs when you bridge/proxy endpoints.

See more details at UrlRewrite and How to use Camel as a HTTP proxy between a client and server.

useContinuation

true

Camel 2.6: Whether or not to use Jetty continuations for the Jetty Server.

Message Headers

Camel uses the same message headers as the HTTP component. From Camel 2.2, it also uses (Exchange.HTTP_CHUNKEDCamelHttpChunked) header to toggle chunked encoding on the camel-jetty consumer. Camel also populates all request.parameter and request.headers. For example, given a client request with the URL, http://myserver/myserver?orderid=123, the exchange will contain a header named orderid with the value 123.

From Camel 2.2.0: you can get the request.parameter from the message header not only from GET HTTP Method, but also other HTTP method.

Usage

The Jetty component supports both consumer and producer endpoints. Another option for producing to other HTTP endpoints, is to use the HTTP Component

Component Options

The JettyHttpComponent provides the following options:

confluenceTableSmall

Option

Default Value

Description

allowJavaSerializedObject

false

Camel 2.16.1/2.15.5: Whether to allow java serialization when a request uses context-type=application/x-java-serialized-object.

When true, be aware that Java will deserialize the incoming data from the request to Java and that can be a potential security risk.

enableJmx

false

Camel 2.3: If this option is true, Jetty JMX support will be enabled for this endpoint. See Jetty JMX support for more details.

errorHandler

null

Camel 2.15: This option is used to set the ErrorHandler that Jetty server uses.

httpClient

null

Deprecated: Producer only: To use a custom HttpClient with the jetty producer.

Note: from Camel 2.11 this option has been removed. Set the option on the endpoint instead.

httpClientMaxThreads

null

Producer only: To set a value for maximum number of threads in HttpClient thread pool. Notice that both a min and max size must be configured.

httpClientMinThreads

null

Producer only: To set a value for minimum number of threads in HttpClient thread pool. Notice that both a min and max size must be configured.

httpClientThreadPool

null

Deprecated: Producer only: To use a custom thread pool for the client.

Note: this option has been removed from Camel 2.11.

maxThreads

null

Camel 2.5 Consumer only: To set a value for maximum number of threads in server thread pool. Notice that both a min and max size must be configured.

minThreads

null

Camel 2.5 Consumer only: To set a value for minimum number of threads in server thread pool. Notice that both a min and max size must be configured.

proxyHost

null

Camel 2.12.2/2.11.3 To use an HTTP proxy.

proxyPort

null

Camel 2.12.2/2.11.3: To use an HTTP proxy.

socketConnectors

null

Camel 2.5 Consumer only: A map which contains per port number specific HTTP connectors. Uses the same principle as sslSocketConnectors and therefore see section SSL support for more details.

socketConnectorProperties

null

Camel 2.5 Consumer only. A map which contains general HTTP connector properties. Uses the same principle as sslSocketConnectorProperties and therefore see section SSL support for more details.

sslContextParameters

null

Camel 2.8: To configure a custom SSL/TLS configuration options at the component level. 

See  Using the JSSE Configuration Utility for more details.

sslKeyPassword

null

Consumer only: The password for the keystore when using SSL.

sslKeystore

null

Consumer only: The path to the keystore.

sslPassword

null

Consumer only: The password when using SSL.

sslSocketConnectors

null

Camel 2.3 Consumer only: A map which contains per port number specific SSL connectors. See section SSL support for more details.

sslSocketConnectorProperties

null

Camel 2.5 Consumer only. A map which contains general SSL connector properties. See section SSL support for more details.

requestBufferSize

null

Camel 2.11.2: Allows to configure a custom value of the request buffer size on the Jetty connectors.

requestHeaderSize

null

Camel 2.11.2: Allows to configure a custom value of the request header size on the Jetty connectors.

responseBufferSize

null

Camel 2.11.2: Allows to configure a custom value of the response buffer size on the Jetty connectors.

responseHeaderSize

null

Camel 2.11.2: Allows to configure a custom value of the response header size on the Jetty connectors.

threadPool

null

Camel 2.5 Consumer only: To use a custom thread pool for the server. This option should only be used in special circumstances.

Producer Example

The following is a basic example of how to send an HTTP request to an existing HTTP endpoint.

Java DSL:

javafrom("direct:start") .to("jetty://http://www.google.com");

XML DSL:

xml<route> <from uri="direct:start"/> <to uri="jetty://http://www.google.com"/> <route>

Consumer Example

In this sample we define a route that exposes a HTTP service at http://localhost:8080/myapp/myservice:{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteTest.java}

Usage of localhost

When you specify localhost in a URL, Camel exposes the endpoint only on the local TCP/IP network interface, so it cannot be accessed from outside the machine it operates on.

If you need to expose a Jetty endpoint on a specific network interface, the numerical IP address of this interface should be used as the host. If you need to expose a Jetty endpoint on all network interfaces, the 0.0.0.0 address should be used.

To listen across an entire URI prefix, see How do I let Jetty match wildcards.

If you actually want to expose routes by HTTP and already have a Servlet, you should instead refer to the Servlet Transport.

 

Our business logic is implemented in the MyBookService class, which accesses the HTTP request contents and then returns a response.
Note: The assert call appears in this example, because the code is part of an unit test.{snippet:id=e2|lang=java|url=camel/trunk/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteTest.java}The following sample shows a content-based route that routes all requests containing the URI parameter, one, to the endpoint, mock:one, and all others to mock:other.{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyContentBasedRouteTest.java}If a client sends an HTTP request, http://serverUri?one=hello, the Jetty component will copy the HTTP request parameter, one to the exchange's in.header. We can then use the simple language to route exchanges that contain this header to a specific endpoint and all others to another. If we used a language more powerful than Simple, e.g., EL or OGNL, then we can also test for the parameter value and route based on the header value as well.

Session Support

The session support option, sessionSupport, can be used to enable a HttpSession object and access the session object while processing the exchange.

For example, the following route enables sessions:

xml<route> <from uri="jetty:http://0.0.0.0/myapp/myservice/?sessionSupport=true"/> <processRef ref="myCode"/> <route>

The myCode Processor can be instantiated by a Spring bean element:

xml<bean id="myCode"class="com.mycompany.MyCodeProcessor"/>

Where the processor implementation can access the HttpSession as follows:

javapublic void process(Exchange exchange) throws Exception { HttpSession session = exchange.getIn(HttpMessage.class).getRequest().getSession(); // ... }

SSL Support (HTTPS)

Using the JSSE Configuration Utility

From Camel 2.8: the camel-jetty component supports SSL/TLS configuration through the Camel JSSE Configuration Utility.  This utility greatly decreases the amount of component specific code you need to write and is configurable at the endpoint and component levels.  The following examples demonstrate how to use the utility with the Jetty component.

Programmatic configuration of the component
javaKeyStoreParameters ksp = new KeyStoreParameters(); ksp.setResource("/users/home/server/keystore.jks"); ksp.setPassword("keystorePassword"); KeyManagersParameters kmp = new KeyManagersParameters(); kmp.setKeyStore(ksp); kmp.setKeyPassword("keyPassword"); SSLContextParameters scp = new SSLContextParameters(); scp.setKeyManagers(kmp); JettyComponent jettyComponent = getContext().getComponent("jetty", JettyComponent.class); jettyComponent.setSslContextParameters(scp);
Spring DSL based configuration of endpoint
xml<camel:sslContextParameters id="sslContextParameters"> <camel:keyManagers keyPassword="keyPassword"> <camel:keyStore resource="/users/home/server/keystore.jks" password="keystorePassword"/> </camel:keyManagers> </camel:sslContextParameters> <to uri="jetty:https://127.0.0.1/mail/?sslContextParametersRef=sslContextParameters"/>
Configuring Jetty Directly

Jetty provides SSL support out of the box. To enable Jetty to run in SSL mode, simply format the URI using the https:// prefix.

Example:

xml<from uri="jetty:https://0.0.0.0/myapp/myservice/"/>

Jetty also needs to know where to load your keystore from and what passwords to use in order to load the correct SSL certificate. Set the following JVM System Properties:

Before Camel 2.3:

confluenceTableSmall

Property

Description

jetty.ssl.keystore

Specifies the location of the Java keystore file, which contains the Jetty server's own X.509 certificate in a key entry. A key entry stores the X.509 certificate (effectively, the public key) and also its associated private key.

jetty.ssl.password

The store password, which is required to access the keystore file (this is the same password that is supplied to the keystore command's -storepass option).

jetty.ssl.keypassword

The key password, which is used to access the certificate's key entry in the keystore (this is the same password that is supplied to the keystore command's -keypass option).

 

From Camel 2.3:

confluenceTableSmall

Property

Description

org.eclipse.jetty.ssl.keystore

Specifies the location of the Java keystore file, which contains the Jetty server's own X.509 certificate in a key entry. A key entry stores the X.509 certificate (effectively, the public key) and also its associated private key.

org.eclipse.jetty.ssl.password

The store password, which is required to access the keystore file (this is the same password that is supplied to the keystore command's keystore option).

org.eclipse.jetty.ssl.keypassword

The key password, which is used to access the certificate's key entry in the keystore (this is the same password that is supplied to the keystore command's keystore option).

For details of how to configure SSL on a Jetty endpoint, read the following Jetty documentation. Some SSL properties aren't exposed directly by Camel. However, Camel does expose the underlying SslSocketConnector, which will allow you to set properties like needClientAuth for mutual authentication requiring a client certificate or wantClientAuth for mutual authentication where a client doesn't need a certificate but can have one.

There's a slight difference between the various Camel versions:

Up to Camel 2.2

xml<bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent"> <property name="sslSocketConnectors"> <map> <entry key="8043"> <bean class="org.mortbay.jetty.security.SslSocketConnector"> <property name="password"value="..."/> <property name="keyPassword"value="..."/> <property name="keystore"value="..."/> <property name="needClientAuth"value="..."/> <property name="truststore"value="..."/> </bean> </entry> </map> </property> </bean>

Camel 2.3, 2.4

xml<bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent"> <property name="sslSocketConnectors"> <map> <entry key="8043"> <bean class="org.eclipse.jetty.server.ssl.SslSocketConnector"> <property name="password"value="..."/> <property name="keyPassword"value="..."/> <property name="keystore"value="..."/> <property name="needClientAuth"value="..."/> <property name="truststore"value="..."/> </bean> </entry> </map> </property> </bean>

From Camel 2.5: we switch to use SslSelectChannelConnector *

xml<bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent"> <property name="sslSocketConnectors"> <map> <entry key="8043"> <bean class="org.eclipse.jetty.server.ssl.SslSelectChannelConnector"> <property name="password"value="..."/> <property name="keyPassword"value="..."/> <property name="keystore"value="..."/> <property name="needClientAuth"value="..."/> <property name="truststore"value="..."/> </bean> </entry> </map> </property> </bean>

The value you use as keys in the above map is the port you configure Jetty to listen on.

Configuring General SSL Properties

From Camel 2.5: instead of a per port number specific SSL socket connector (as shown above) you can now configure general properties which applies for all SSL socket connectors (which is not explicit configured as above with the port number as entry).

xml<bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent"> <property name="sslSocketConnectorProperties"> <map> <entry key="password"value="..."/> <entry key="keyPassword"value="..."/> <entry key="keystore"value="..."/> <entry key="needClientAuth"value="..."/> <entry key="truststore"value="..."/> </map> </property> </bean>

How to Obtain A Reference to the X509Certificate

Jetty stores a reference to the certificate in the HttpServletRequest which you can access from code as follows:

javaHttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class); X509Certificate cert = (X509Certificate) req.getAttribute("javax.servlet.request.X509Certificate")

Configuring General HTTP Properties

From Camel 2.5: instead of a per port number specific HTTP socket connector (as shown above) you can now configure general properties which applies for all HTTP socket connectors (which is not explicit configured as above with the port number as entry).

xml<bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent"> <property name="socketConnectorProperties"> <map> <entry key="acceptors" value="4"/> <entry key="maxIdleTime" value="300000"/> </map> </property> </bean>

How to Get the Value of The X-Forwarded-For HTTP Header Using HttpServletRequest.getRemoteAddr()

If the HTTP requests are handled by an Apache server and forwarded to Jetty with mod_proxy, the original client IP address is in the X-Forwarded-For header and the HttpServletRequest.getRemoteAddr() will return the address of the Apache proxy.

Jetty has a forwarded property which takes the value from X-Forwarded-For and places it in the HttpServletRequest remoteAddr property.  This property is not available directly through the endpoint configuration but it can be easily added using the socketConnectors property:

xml<bean id="jetty" class="org.apache.camel.component.jetty.JettyHttpComponent"> <property name="socketConnectors"> <map> <entry key="8080"> <bean class="org.eclipse.jetty.server.nio.SelectChannelConnector"> <property name="forwarded" value="true"/> </bean> </entry> </map> </property> </bean>

This is particularly useful when an existing Apache server handles TLS connections for a domain and proxies them to application servers internally.

Default Behavior for Returning HTTP Status Codes

The default behavior of HTTP status codes is defined by the org.apache.camel.component.http.DefaultHttpBinding class, which handles how a response is written and also sets the HTTP status code. If the exchange was processed successfully, the 200 HTTP status code is returned. If the exchange failed with an exception, the 500 HTTP status code is returned, and the stacktrace is returned in the body. If you want to specify which HTTP status code to return, set the code in the Exchange.HTTP_RESPONSE_CODE header of the OUT message.

Customizing HttpBinding

By default, Camel uses the org.apache.camel.component.http.DefaultHttpBinding to handle how a response is written. If you like, you can customize this behavior either by implementing your own HttpBinding class or by extending DefaultHttpBinding and overriding the appropriate methods.

The following example shows how to customize the DefaultHttpBinding in order to change how exceptions are returned:{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpBindingRefTest.java}We can then create an instance of our binding and register it in the Spring registry as follows:

xml<bean id="mybinding"class="com.mycompany.MyHttpBinding"/>

And then we can reference this binding when we define the route:

xml<route> <from uri="jetty:http://0.0.0.0:8080/myapp/myservice?httpBindingRef=mybinding"/> <to uri="bean:doSomething"/> </route>

Jetty Handlers and Security Configuration

You can configure a list of Jetty handlers on the endpoint, which can be useful for enabling advanced Jetty security features. These handlers are configured in Spring XML as follows:

xml<-- Jetty Security handling --> <bean id="userRealm" class="org.mortbay.jetty.plus.jaas.JAASUserRealm"> <property name="name" value="tracker-users"/> <property name="loginModuleName" value="ldaploginmodule"/> </bean> <bean id="constraint" class="org.mortbay.jetty.security.Constraint"> <property name="name" value="BASIC"/> <property name="roles" value="tracker-users"/> <property name="authenticate" value="true"/> </bean> <bean id="constraintMapping" class="org.mortbay.jetty.security.ConstraintMapping"> <property name="constraint" ref="constraint"/> <property name="pathSpec" value="/*"/> </bean> <bean id="securityHandler" class="org.mortbay.jetty.security.SecurityHandler"> <property name="userRealm" ref="userRealm"/> <property name="constraintMappings" ref="constraintMapping"/> </bean>

From Camel 2.3: you can configure a list of Jetty handlers as follows:

xml<-- Jetty Security handling --> <bean id="constraint" class="org.eclipse.jetty.http.security.Constraint"> <property name="name" value="BASIC"/> <property name="roles" value="tracker-users"/> <property name="authenticate" value="true"/> </bean> <bean id="constraintMapping" class="org.eclipse.jetty.security.ConstraintMapping"> <property name="constraint" ref="constraint"/> <property name="pathSpec" value="/*"/> </bean> <bean id="securityHandler" class="org.eclipse.jetty.security.ConstraintSecurityHandler"> <property name="authenticator"> <bean class="org.eclipse.jetty.security.authentication.BasicAuthenticator"/> </property> <property name="constraintMappings"> <list> <ref bean="constraintMapping"/> </list> </property> </bean>

You can then define the endpoint as:

javafrom("jetty:http://0.0.0.0:9080/myservice?handlers=securityHandler")

If you need more handlers, set the handlers option equal to a comma-separated list of bean IDs.

How to Customize the Response on an HTTP 500 Server Error

You may want to return a custom reply message when something goes wrong, instead of the default reply message Camel Jetty replies with. You could use a custom HttpBinding to be in control of the message mapping, but often it may be easier to use Camel's Exception Clause to construct the custom reply message.

Example: return the message: Dude something went wrong for the HTTP error code 500:{snippet:id=e1|lang=java|url=camel/trunk/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyOnExceptionHandledTest.java}

Multi-Part Form Support

From Camel 2.3.0: camel-jetty support to multi-part form post out of box. The submitted form-data are mapped into the message header. camel-jetty creates an attachment for each uploaded file. The file name is mapped to the name of the attachment. The content type is set as the content type of the attachment file name. You can find the example here.{snippet:id=e1|lang=java|url=camel/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java|title=Note: getName() functions as shown below in versions 2.5 and higher. In earlier versions you receive the temporary file name for the attachment instead}

Jetty JMX Support

From Camel 2.3.0camel-jetty supports the enabling of Jetty's JMX capabilities at the component and endpoint level with the endpoint configuration taking priority.

Note: JMX must be enabled within the Camel context in order to enable JMX support in this component as the component provides Jetty with a reference to the MBeanServer registered with the Camel context.

As the camel-jetty component caches and reuses Jetty resources for a given protocol/host/port pairing, this configuration option will only be evaluated during the creation of the first endpoint to use a protocol/host/port pairing.

Example: given two routes created from the following XML fragments, JMX support would remain enabled for all endpoints listening on: https://0.0.0.0.

xml<from uri="jetty:https://0.0.0.0/myapp/myservice1/?enableJmx=true"/> xml<from uri="jetty:https://0.0.0.0/myapp/myservice2/?enableJmx=false"/>

The camel-jetty component also provides for direct configuration of the Jetty MBeanContainer. Jetty creates MBean names dynamically. If you are running another instance of Jetty outside of the Camel context and sharing the same MBeanContainer between the instances, you can provide both instances with a reference to the same MBeanContainer in order to avoid name collisions when registering Jetty MBeans.

Endpoint See Also

Error formatting macro: include: java.lang.RuntimeException: org.owasp.validator.html.ScanException: java.util.EmptyStackException
Error formatting macro: include: java.lang.NullPointerException

JPA Component

The jpa component enables you to store and retrieve Java objects from persistent storage using EJB 3's Java Persistence Architecture (JPA), which is a standard interface layer that wraps Object/Relational Mapping (ORM) products such as OpenJPA, Hibernate, TopLink, and so on.

Maven users will need to add the following dependency to their pom.xml for this component:

xml<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jpa</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>

Sending to the endpoint

You can store a Java entity bean in a database by sending it to a JPA producer endpoint. The body of the In message is assumed to be an entity bean (that is, a POJO with an @Entity annotation on it) or a collection or array of entity beans.

If the body is a List of entities, make sure to use entityType=java.util.ArrayList as a configuration passed to the producer endpoint.

If the body does not contain one of the previous listed types, put a Message Translator in front of the endpoint to perform the necessary conversion first.

From Camel 2.19 onwards you can use query, namedQuery and nativeQuery option for the producer as well to retrieve a set of entities or execute bulk update/delete.

Consuming from the endpoint

Consuming messages from a JPA consumer endpoint removes (or updates) entity beans in the database. This allows you to use a database table as a logical queue: consumers take messages from the queue and then delete/update them to logically remove them from the queue.

If you do not wish to delete the entity bean when it has been processed (and when routing is done), you can specify consumeDelete=false on the URI. This will result in the entity being processed each poll.

If you would rather perform some update on the entity to mark it as processed (such as to exclude it from a future query) then you can annotate a method with @Consumed which will be invoked on your entity bean when the entity bean when it has been processed (and when routing is done).

From Camel 2.13 onwards you can use @PreConsumed which will be invoked on your entity bean before it has been processed (before routing).

If you are consuming a lot (100K+) of rows and experience OutOfMemory problems you should set the maximumResults to sensible value.

Note: Since Camel 2.18, JPA now includes a JpaPollingConsumer implementation that better supports Content Enricher using pollEnrich() to do an on-demand poll that returns either none, one or a list of entities as the result.

 

URI format

jpa:entityClassName[?options]

For sending to the endpoint, the entityClassName is optional. If specified, it helps the Type Converter to ensure the body is of the correct type.

For consuming, the entityClassName is mandatory.

You can append query options to the URI in the following format, ?option=value&option=value&...

Options

Name

Default Value

Description

entityType

entityClassName

Overrides the entityClassName from the URI.

persistenceUnit

camel

The JPA persistence unit used by default.

consumeDelete

true

JPA consumer only: If true, the entity is deleted after it is consumed; if false, the entity is not deleted.

consumeLockEntity

true

JPA consumer only: Specifies whether or not to set an exclusive lock on each entity bean while processing the results from polling.

flushOnSend

true

JPA producer only: Flushes the EntityManager after the entity bean has been persisted.

maximumResults

-1

JPA consumer only: Set the maximum number of results to retrieve on the Query. Camel 2.19: it's also used for the producer when it executes a query.

transactionManager

null

This option is Registry based which requires the # notation so that the given transactionManager being specified can be looked up properly, e.g. transactionManager=#myTransactionManager. It specifies the transaction manager to use. If none provided, Camel will use a JpaTransactionManager by default. Can be used to set a JTA transaction manager (for integration with an EJB container).

consumer.delay

500

JPA consumer only: Delay in milliseconds between each poll.

consumer.initialDelay

1000

JPA consumer only: Milliseconds before polling starts.

consumer.useFixedDelay

false

JPA consumer only: Set to true to use fixed delay between polls, otherwise fixed rate is used. See ScheduledExecutorService in JDK for details.

maxMessagesPerPoll

0

JPA consumer only: An integer value to define the maximum number of messages to gather per poll. By default, no maximum is set. Can be used to avoid polling many thousands of messages when starting up the server. Set a value of 0 or negative to disable.

consumer.query

 

JPA consumer only: To use a custom query when consuming data.

consumer.namedQuery

 

JPA consumer only: To use a named query when consuming data.

consumer.nativeQuery

 

JPA consumer only: To use a custom native query when consuming data. You may want to use the option consumer.resultClass also when using native queries.

consumer.parameters

 

Camel 2.12: JPA consumer only: This option is Registry based which requires the # notation. This key/value mapping is used for building the query parameters. It's is expected to be of the generic type java.util.Map<String, Object> where the keys are the named parameters of a given JPA query and the values are their corresponding effective values you want to select for.

consumer.resultClass

 

Camel 2.7: JPA consumer only: Defines the type of the returned payload (we will call entityManager.createNativeQuery(nativeQuery, resultClass) instead of entityManager.createNativeQuery(nativeQuery)). Without this option, we will return an object array. Only has an affect when using in conjunction with native query when consuming data.

consumer.transacted

false

Camel 2.7.5/2.8.3/2.9: JPA consumer only: Whether to run the consumer in transacted mode, by which all messages will either commit or rollback, when the entire batch has been processed. The default behavior (false) is to commit all the previously successfully processed messages, and only rollback the last failed message.

consumer.lockModeType

WRITE

Camel 2.11.2/2.12: To configure the lock mode on the consumer. The possible values is defined in the enum javax.persistence.LockModeType. The default value is changed to PESSIMISTIC_WRITE since Camel 2.13.

consumer.SkipLockedEntity

false

Camel 2.13: To configure whether to use NOWAIT on lock and silently skip the entity.

usePersist

false

Camel 2.5: JPA producer only: Indicates to use entityManager.persist(entity) instead of entityManager.merge(entity). Note: entityManager.persist(entity) doesn't work for detached entities (where the EntityManager has to execute an UPDATE instead of an INSERT query)!

joinTransaction

true

Camel 2.12.3: camel-jpa will join transaction by default from Camel 2.12 onwards. You can use this option to turn this off, for example if you use LOCAL_RESOURCE and join transaction doesn't work with your JPA provider. This option can also be set globally on the JpaComponent, instead of having to set it on all endpoints.

usePassedInEntityManager

falseCamel 2.12.4/2.13.1 JPA producer only: If set to true, then Camel will use the EntityManager from the header

JpaConstants.ENTITYMANAGER instead of the configured entity manager on the component/endpoint. This allows end users to control which entity manager will be in use.

sharedEntityManagerfalse

Camel 2.16: whether to use spring's SharedEntityManager for the consumer/producer. A good idea may be to set joinTransaction=false if this option is true, as sharing the entity manager and mixing transactions is not a good idea. 

query To use a custom query. Camel 2.19: it can be used for producer as well.
namedQuery To use a named query. Camel 2.19: it can be used for producer as well.
nativeQuery To use a custom native query. You may want to use the option resultClass also when using native queries. Camel 2.19: it can be used for producer as well.
parameters This option is Registry based which requires the # notation. This key/value mapping is used for building the query parameters. It is expected to be of the generic type java.util.Map<String, Object> where the keys are the named parameters of a given JPA query and the values are their corresponding effective values you want to select for. Camel 2.19: it can be used for producer as well. When it's used for producer, Simple expression can be used as a parameter value. It allows you to retrieve parameter values from the message body header and etc.
resultClass Defines the type of the returned payload (we will call entityManager.createNativeQuery(nativeQuery, resultClass) instead of entityManager.createNativeQuery(nativeQuery)). Without this option, we will return an object array. Only has an affect when using in conjunction with native query. Camel 2.19: it can be used for producer as well.
useExecuteUpdate Camel 2.19: JPA producer only: To configure whether to use executeUpdate() when producer executes a query. When you use INSERT, UPDATE or DELETE statement as a named query, you need to specify this option to 'true'.

Message Headers

Camel adds the following message headers to the exchange:

confluenceTableSmall

Header

Type

Description

CamelJpaTemplate

JpaTemplate

Not supported anymore since Camel 2.12: The JpaTemplate object that is used to access the entity bean. You need this object in some situations, for instance in a type converter or when you are doing some custom processing. See CAMEL-5932 for the reason why the support for this header has been dropped.

CamelEntityManager

EntityManager

Camel 2.12: JPA consumer / Camel 2.12.2: JPA producer: The JPA EntityManager object being used by JpaConsumer or JpaProducer.

Configuring EntityManagerFactory

Its strongly advised to configure the JPA component to use a specific EntityManagerFactory instance. If failed to do so each JpaEndpoint will auto create their own instance of EntityManagerFactory which most often is not what you want.

For example, you can instantiate a JPA component that references the myEMFactory entity manager factory, as follows:

xml<bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent"> <property name="entityManagerFactory" ref="myEMFactory"/> </bean>

In Camel 2.3 the JpaComponent will auto lookup the EntityManagerFactory from the Registry which means you do not need to configure this on the JpaComponent as shown above. You only need to do so if there is ambiguity, in which case Camel will log a WARN.

Configuring TransactionManager

Since Camel 2.3 the JpaComponent will auto lookup the TransactionManager from the Registry. If Camel won't find any TransactionManager instance registered, it will also look up for the TransactionTemplate and try to extract TransactionManager from it.

If none TransactionTemplate is available in the registry, JpaEndpoint will auto create their own instance of TransactionManager which most often is not what you want.

If more than single instance of the TransactionManager is found, Camel will log a WARN. In such cases you might want to instantiate and explicitly configure a JPA component that references the myTransactionManager transaction manager, as follows:

xml<bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent"> <property name="entityManagerFactory" ref="myEMFactory"/> <property name="transactionManager" ref="myTransactionManager"/> </bean>

Using a consumer with a named query

For consuming only selected entities, you can use the consumer.namedQuery URI query option. First, you have to define the named query in the JPA Entity class:

@Entity @NamedQuery(name = "step1", query = "select x from MultiSteps x where x.step = 1") public class MultiSteps { ... }

After that you can define a consumer uri like this one:

from("jpa://org.apache.camel.examples.MultiSteps?consumer.namedQuery=step1") .to("bean:myBusinessLogic");

Using a consumer with a query

For consuming only selected entities, you can use the consumer.query URI query option. You only have to define the query option:

from("jpa://org.apache.camel.examples.MultiSteps?consumer.query=select o from org.apache.camel.examples.MultiSteps o where o.step = 1") .to("bean:myBusinessLogic");

Using a consumer with a native query

For consuming only selected entities, you can use the consumer.nativeQuery URI query option. You only have to define the native query option:

from("jpa://org.apache.camel.examples.MultiSteps?consumer.nativeQuery=select * from MultiSteps where step = 1") .to("bean:myBusinessLogic");

If you use the native query option, you will receive an object array in the message body.

 

Using a producer with a named query

For retrieving selected entities or execute bulk update/delete, you can use the namedQuery URI query option. First, you have to define the named query in the JPA Entity class:

@Entity @NamedQuery(name = "step1", query = "select x from MultiSteps x where x.step = 1") public class MultiSteps { ... }

After that you can define a producer uri like this one:

from("direct:namedQuery") .to("jpa://org.apache.camel.examples.MultiSteps?namedQuery=step1");

Using a producer with a query

For retrieving selected entities or execute bulk update/delete, you can use the query URI query option. You only have to define the query option:

from("direct:query") .to("jpa://org.apache.camel.examples.MultiSteps?query=select o from org.apache.camel.examples.MultiSteps o where o.step = 1");

Using a producer with a native query

For retrieving selected entities or execute bulk update/delete, you can use the nativeQuery URI query option. You only have to define the native query option:

from("direct:nativeQuery") .to("jpa://org.apache.camel.examples.MultiSteps?resultClass=org.apache.camel.examples.MultiSteps&nativeQuery=select * from MultiSteps where step = 1");

If you use the native query option without specifying resultClass, you will receive an object array in the message body.

 

Example

See Tracer Example for an example using JPA to store traced messages into a database.

Using the JPA based idempotent repository

In this section we will use the JPA based idempotent repository.

First we need to setup a persistence-unit in the persistence.xml file:{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-jpa/src/test/resources/META-INF/persistence.xml}Second we have to setup a org.springframework.orm.jpa.JpaTemplate which is used by the org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository:{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/spring.xml}Afterwards we can configure our org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository:{snippet:id=jpaStore|lang=xml|url=camel/trunk/components/camel-jpa/src/test/resources/org/apache/camel/processor/jpa/fileConsumerJpaIdempotentTest-config.xml}And finally we can create our JPA idempotent repository in the spring XML file as well:

xml<camelContext xmlns="http://camel.apache.org/schema/spring"> <route id="JpaMessageIdRepositoryTest"> <from uri="direct:start" /> <idempotentConsumer messageIdRepositoryRef="jpaStore"> <header>messageId</header> <to uri="mock:result" /> </idempotentConsumer> </route> </camelContext> When running this Camel component tests inside your IDE

In case you run the tests of this component directly inside your IDE (and not necessarily through Maven itself) then you could spot exceptions like:

javaorg.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is <openjpa-2.2.1-r422266:1396819 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: This configuration disallows runtime optimization, but the following listed types were not enhanced at build time or at class load time with a javaagent: "org.apache.camel.examples.SendEmail". at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:427) at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371) at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:127) at org.apache.camel.processor.jpa.JpaRouteTest.cleanupRepository(JpaRouteTest.java:96) at org.apache.camel.processor.jpa.JpaRouteTest.createCamelContext(JpaRouteTest.java:67) at org.apache.camel.test.junit4.CamelTestSupport.doSetUp(CamelTestSupport.java:238) at org.apache.camel.test.junit4.CamelTestSupport.setUp(CamelTestSupport.java:208)

The problem here is that the source has been compiled/recompiled through your IDE and not through Maven itself which would enhance the byte-code at build time. To overcome this you would need to enable dynamic byte-code enhancement of OpenJPA. As an example assuming the current OpenJPA version being used in Camel itself is 2.2.1, then as running the tests inside your favorite IDE you would need to pass the following argument to the JVM:

-javaagent:<path_to_your_local_m2_cache>/org/apache/openjpa/openjpa/2.2.1/openjpa-2.2.1.jar

Then it will all become green again (smile)

Endpoint See Also

List Component

deprecated: is renamed to the Browse component in Camel 2.0

The List component provides a simple BrowsableEndpoint which can be useful for testing, visualisation tools or debugging. The exchanges sent to the endpoint are all available to be browsed.

URI format

list:someName

Where someName can be any string to uniquely identify the endpoint.

Sample

In the route below we have the list component to be able to browse the Exchanges that is passed through:

  from("activemq:order.in").to("list:orderReceived").to("bean:processOrder");

Then we will be able to inspect the received exchanges from java code:

    private CamelContext context;

    public void inspectRecievedOrders() {
        BrowsableEndpoint browse = context.getEndpoint("list:orderReceived", BrowsableEndpoint.class);
        List<Exchange> exchanges = browse.getExchanges();
        ...
        // then we can inspect the list of received exchanges from Java
        for (Exchange exchange : exchanges) {
            String payload = exchange.getIn().getBody();
            ...
        }
   }

Log Component

The log: component logs message exchanges to the underlying logging mechanism.

Camel uses sfl4j which allows you to configure logging via, among others:

URI format

log:loggingCategory[?options]

Where loggingCategory is the name of the logging category to use. You can append query options to the URI in the following format, ?option=value&option=value&...

Using Logger instance from the the Registry

As of Camel 2.12.4/2.13.1, if there's single instance of org.slf4j.Logger found in the Registry, the loggingCategory is no longer used to create logger instance. The registered instance is used instead. Also it is possible to reference particular Logger instance using ?logger=#myLogger URI parameter. Eventually, if there's no registered and URI logger parameter, the logger instance is created using loggingCategory.

For example, a log endpoint typically specifies the logging level using the level option, as follows:

log:org.apache.camel.example?level=DEBUG

The default logger logs every exchange (regular logging). But Camel also ships with the Throughput logger, which is used whenever the groupSize option is specified.

Also a log in the DSL

There is also a log directly in the DSL, but it has a different purpose. Its meant for lightweight and human logs. See more details at LogEIP.

Options

Option

Default

Type

Description

groupActiveOnly

true

boolean

If true, will hide stats when no new messages have been received for a time interval.

If false, show stats regardless of message traffic

groupDelay

0

Integer

Set the initial delay for stats (in millis)

groupInterval

null

Integer

If specified will group message stats by this time interval (in millis)

groupSize

null

Integer

An integer that specifies a group size for throughput logging.

level

INFO

String

Logging level to use. Possible values: ERROR, WARN, INFO, DEBUG, TRACE, OFF

logger

 

Logger

Camel 2.12.4/2.13.1: An optional reference to org.slf4j.Logger from Registry to use.

marker

null

String

Camel 2.9: An optional Marker name to use.

 


groupDelay and groupActiveOnly are only applicable when using groupInterval.

 

Formatting

The log formats the execution of exchanges to log lines. 

By default, the log uses LogFormatter to format the log output, where LogFormatter has the following options:

Option

Default

Description

maxChars

 

Limits the number of characters logged per line. The default value, from Camel 2.9 is 10000.

multiline

false

If true, each piece of information is logged on a new line.

showAll

false

Quick option for turning all options on. (multilinemaxChars has to be manually set if to be used)

showBody

true

Show the IN body.

showBodyType

true

Show the IN body Java type.

showCaughtException

false

If the exchange has a caught exception, show the exception message (no stack trace).

A caught exception is stored as a property on the exchange (using the key Exchange.EXCEPTION_CAUGHT) and for instance a doCatch can catch exceptions.

See Try Catch Finally.

showException

false

If the exchange has an exception, show the exception message (no stack trace).

showExchangeId

false

Show the unique exchange ID.

showExchangePattern

true

Shows the Message Exchange Pattern (or MEP for short).

showFiles

false

Camel 2.9: Whether Camel should show file bodies or not, e.g., such as java.io.File.

showFuture

false

Whether Camel should show java.util.concurrent.Future bodies or not. If enabled Camel could potentially wait until the Future task is done. Will not wait, by default.

showHeaders

false

Show the IN message headers.

showOut

false

If the exchange has an OUT message, show the OUT message.

showProperties

false

Show the exchange properties.

showStackTrace

false

Show the stack trace, if an exchange has an exception. Only effective if one of showAll, showException or showCaughtException are enabled.

showStreams

false

Camel 2.8: Whether Camel should show stream bodies or not, e.g., such as java.io.InputStream.

If you enable this option then you may not be able later to access the message body as the stream have already been read by this logger.

To remedy this you will have to use Stream caching.

skipBodyLineSeparator

true

Camel 2.12.2: Whether to skip line separators when logging the message body. This will log the message body on a single line.

Set to false to preserve any line separators present in the body, therefore logging the body as is.

Logging stream bodies

For older versions of Camel that do not support the showFiles or showStreams properties above, you can set the following property instead on the CamelContext to log both stream and file bodies:

camelContext.getProperties().put(Exchange.LOG_DEBUG_BODY_STREAMS, true);

Regular Logger Example

In the route below we log the incoming orders at DEBUG level before the order is processed:

from("activemq:orders")
  .to("log:com.mycompany.order?level=DEBUG")
  .to("bean:processOrder");

Or using Spring XML:

  <route>
    <from uri="activemq:orders"/>
    <to uri="log:com.mycompany.order?level=DEBUG"/>
    <to uri="bean:processOrder"/>
  </route> 

Regular Logger with Formatter

In the route below we log the incoming orders at INFO level before the order is processed.

from("activemq:orders")
  .to("log:com.mycompany.order?showAll=true&multiline=true")
  .to("bean:processOrder");

Throughput Logger With groupSize

In the route below we log the throughput of the incoming orders at DEBUG level grouped by 10 messages.

from("activemq:orders")
  .to("log:com.mycompany.order?level=DEBUG&groupSize=10")
  .to("bean:processOrder");

Throughput Logger With groupInterval

This route will result in message stats logged every 10s, with an initial 60s delay and stats should be displayed even if there isn't any message traffic.

from("activemq:orders")
  .to("log:com.mycompany.order?level=DEBUG&groupInterval=10000&groupDelay=60000&groupActiveOnly=false")
  .to("bean:processOrder");

The following will be logged:

"Received: 1000 new messages, with total 2000 so far. Last group took: 10000 millis which is: 100 messages per second. average: 100"

Full Customization of the Logged Output

Available as of Camel 2.11

With the options outlined in the #Formatting section, you can control much of the output of the logger. However, log lines will always follow this structure:

Exchange[Id:ID-machine-local-50656-1234567901234-1-2, ExchangePattern:InOut, 
Properties:{CamelToEndpoint=log://org.apache.camel.component.log.TEST?showAll=true, 
CamelCreatedTimestamp=Thu Mar 28 00:00:00 WET 2013}, 
Headers:{breadcrumbId=ID-machine-local-50656-1234567901234-1-1}, BodyType:String, Body:Hello World, Out: null]

This format is unsuitable in some cases, perhaps because you need to:

  • Filter the headers and properties that are printed, to strike a balance between insight and verbosity.
  • Adjust the log message to whatever you deem most readable.
  • Tailor log messages for digestion by log mining systems, e.g. Splunk.
  • Print specific body types differently.
  • Etc.

Whenever you require absolute customization, you can create a class that implements the ExchangeFormatter interface. Within the format(Exchange) method you have access to the full Exchange, so you can select and extract the precise information you need, format it in a custom manner and return it. The return value will become the final log message.

You can have the Log component pick up your custom ExchangeFormatter in one of two ways:

Explicitly instantiating the LogComponent in your Registry

<bean name="log" class="org.apache.camel.component.log.LogComponent">
   <property name="exchangeFormatter" ref="myCustomFormatter"/>
</bean>

Convention Over Configuration

Simply by registering a bean with the name logFormatter; the Log Component is intelligent enough to pick it up automatically.

<bean name="logFormatter" class="com.xyz.MyCustomExchangeFormatter"/>
The ExchangeFormatter gets applied to all Log endpoints within that Camel Context. If you need a different ExchangeFormatter for each endpoint, just instantiate the LogComponent as many times as needed, and use the relevant bean name as the endpoint prefix.

From Camel 2.11.2/2.12: when using a custom log formatter, you can specify parameters in the log URI, which gets configured on the custom log formatter. Though when you do that you should define the logFormatter as prototype scoped so its not shared if you have different parameters.

Example:

<bean name="logFormatter" class="com.xyz.MyCustomExchangeFormatter" scope="prototype"/>

And then we can have Camel routes using the log URI with different options:

<to uri="log:foo?param1=foo&amp;param2=100"/>
<!-- ... -->
<to uri="log:bar?param1=bar&amp;param2=200"/>

Using Log Component in OSGi

Improvements from Camel 2.12.4/2.13.1

When using Log component inside OSGi (e.g., in Karaf), the underlying logging mechanisms are provided by PAX logging. It searches for a bundle which invokes org.slf4j.LoggerFactory.getLogger() method and associates the bundle with the logger instance. Without specifying custom org.sfl4j.Logger instance, the logger created by Log component is associated with camel-core bundle.

In some scenarios it is required that the bundle associated with logger should be the bundle which contains route definition. To do this, either register a single instance of org.slf4j.Logger in the Registry or reference it using logger URI parameter.

Mail Component

The mail component provides access to Email via Spring's Mail support and the underlying JavaMail system.

Maven users will need to add the following dependency to their pom.xml for this component:

xml<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-mail</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> Geronimo mail .jar

We have discovered that the geronimo mail .jar (v1.6) has a bug when polling mails with attachments. It cannot correctly identify the Content-Type. So, if you attach a .jpeg file to a mail and you poll it, the Content-Type is resolved as text/plain and not as image/jpeg. For that reason, we have added an org.apache.camel.component.ContentTypeResolver SPI interface which enables you to provide your own implementation and fix this bug by returning the correct Mime type based on the file name. So if the file name ends with jpeg/jpg, you can return image/jpeg.

You can set your custom resolver on the MailComponent instance or on the MailEndpoint instance.

POP3 or IMAP

POP3 has some limitations and end users are encouraged to use IMAP if possible.

Using mock-mail for testing

You can use a mock framework for unit testing, which allows you to test without the need for a real mail server. However you should remember to not include the mock-mail when you go into production or other environments where you need to send mails to a real mail server. Just the presence of the mock-javamail.jar on the classpath means that it will kick in and avoid sending the mails.

URI format

Mail endpoints can have one of the following URI formats (for the protocols, SMTP, POP3, or IMAP, respectively):

smtp://[username@]host[:port][?options] pop3://[username@]host[:port][?options] imap://[username@]host[:port][?options]

The mail component also supports secure variants of these protocols (layered over SSL). You can enable the secure protocols by adding s to the scheme:

smtps://[username@]host[:port][?options] pop3s://[username@]host[:port][?options] imaps://[username@]host[:port][?options]

You can append query options to the URI in the following format, ?option=value&option=value&...

Sample endpoints

Typically, you specify a URI with login credentials as follows (taking SMTP as an example):

smtp://[username@]host[:port][?password=somepwd]

Alternatively, it is possible to specify both the user name and the password as query options:

smtp://host[:port]?password=somepwd&username=someuser

For example:

smtp://mycompany.mailserver:30?password=tiger&username=scott

DefaultPortsDefault ports

Default port numbers are supported. If the port number is omitted, Camel determines the port number to use based on the protocol.

confluenceTableSmall

Protocol

Default Port Number

SMTP

25

SMTPS

465

POP3

110

POP3S

995

IMAP

143

IMAPS

993

Options

confluenceTableSmall

Property

Default

Description

host

 

The host name or IP address to connect to.

port

See #DefaultPorts

The TCP port number to connect on.

username

 

The user name on the email server.

password

null

The password on the email server.

ignoreUriScheme

false

If false, Camel uses the scheme to determine the transport protocol (POP, IMAP, SMTP etc.)

contentType

text/plain

The mail message content type. Use text/html for HTML mails.

folderName

INBOX

The folder to poll.

destination

username@host

@deprecated Use the to option instead. The TO recipients (receivers of the email).

to

username@host

The TO recipients (the receivers of the mail). Separate multiple email addresses with a comma. Email addresses containing special characters such as "&" will need to be handled differently - see How do I configure password options on Camel endpoints without the value being encoded.

replyTo

alias@host

As of Camel 2.8.4, 2.9.1+, the Reply-To recipients (the receivers of the response mail). Separate multiple email addresses with a comma.

cc

null

The CC recipients (the receivers of the mail). Separate multiple email addresses with a comma.

bcc

null

The BCC recipients (the receivers of the mail). Separate multiple email addresses with a comma.

from

camel@localhost

The FROM email address.

subject

 

As of Camel 2.3, the Subject of the message being sent. Note: Setting the subject in the header takes precedence over this option.

peek

true

Camel 2.11.3/2.12.2: Consumer only. Will mark the javax.mail.Message as peeked before processing the mail message. This applies to IMAPMessage messages types only. By using peek the mail will not be eager marked as SEEN on the mail server, which allows us to rollback the mail message if there is an error processing in Camel.

delete

false

Deletes the messages after they have been processed. This is done by setting the DELETED flag on the mail message. If false, the SEEN flag is set instead. As of Camel 2.10 you can override this configuration option by setting a header with the key delete to determine if the mail should be deleted or not.

unseen

true

It is possible to configure a consumer endpoint so that it processes only unseen messages (that is, new messages) or all messages. Note that Camel always skips deleted messages. The default option of true will filter to only unseen messages. POP3 does not support the SEEN flag, so this option is not supported in POP3; use IMAP instead. Important: This option is not in use if you also use searchTerm options. Instead if you want to disable unseen when using searchTerm's then add searchTerm.unseen=false as a term.

copyTo

null

Camel 2.10: Consumer only. After processing a mail message, it can be copied to a mail folder with the given name. You can override this configuration value, with a header with the key copyTo, allowing you to copy messages to folder names configured at runtime.

fetchSize

-1

Sets the maximum number of messages to consume during a poll. This can be used to avoid overloading a mail server, if a mailbox folder contains a lot of messages. Default value of -1 means no fetch size and all messages will be consumed. Setting the value to 0 is a special corner case, where Camel will not consume any messages at all.

alternativeBodyHeader

CamelMailAlternativeBody

Specifies the key to an IN message header that contains an alternative email body. For example, if you send emails in text/html format and want to provide an alternative mail body for non-HTML email clients, set the alternative mail body with this key as a header.

debugMode

false

Enable debug mode on the underlying mail framework. The SUN Mail framework logs the debug messages to System.out by default.

connectionTimeout

30000

The connection timeout in milliseconds. Default is 30 seconds.

consumer.initialDelay

1000

Milliseconds before the polling starts.

consumer.delay

60000

Camel will poll the mailbox only once a minute by default to avoid overloading the mail server.

consumer.useFixedDelay

false

Set to true to use a fixed delay between polls, otherwise fixed rate is used. See ScheduledExecutorService in JDK for details.

disconnect

false

Camel 2.8.3/2.9: Whether the consumer should disconnect after polling. If enabled this forces Camel to connect on each poll.

closeFolder

true

Camel 2.10.4: Whether the consumer should close the folder after polling. Setting this option to false and having disconnect=false as well, then the consumer keep the folder open between polls.

mail.XXX

null

Set any additional java mail properties. For instance if you want to set a special property when using POP3 you can now provide the option directly in the URI such as: mail.pop3.forgettopheaders=true. You can set multiple such options, for example: mail.pop3.forgettopheaders=true&mail.mime.encodefilename=true.

mapMailMessage

true

Camel 2.8: Specifies whether Camel should map the received mail message to Camel body/headers. If set to true, the body of the mail message is mapped to the body of the Camel IN message and the mail headers are mapped to IN headers. If this option is set to false then the IN message contains a raw javax.mail.Message. You can retrieve this raw message by calling exchange.getIn().getBody(javax.mail.Message.class).

maxMessagesPerPoll

0

Specifies the maximum number of messages to gather per poll. By default, no maximum is set. Can be used to set a limit of e.g. 1000 to avoid downloading thousands of files when the server starts up. Set a value of 0 or negative to disable this option.

javaMailSender

null

Specifies a pluggable org.apache.camel.component.mail.JavaMailSender instance in order to use a custom email implementation.

ignoreUnsupportedCharset

false

Option to let Camel ignore unsupported charset in the local JVM when sending mails. If the charset is unsupported then charset=XXX (where XXX represents the unsupported charset) is removed from the content-type and it relies on the platform default instead.

sslContextParameters

null

Camel 2.10: Reference to a org.apache.camel.util.jsse.SSLContextParameters in the Registry.  This reference overrides any configured SSLContextParameters at the component level.  See Using the JSSE Configuration Utility.

searchTerm

null

Camel 2.11: Refers to a javax.mail.search.SearchTerm which allows to filter mails based on search criteria such as subject, body, from, sent after a certain date etc. See further below for examples.

searchTerm.xxx

null

Camel 2.11: To configure search terms directly from the endpoint uri, which supports a limited number of terms defined by the org.apache.camel.component.mail.SimpleSearchTerm class. See further below for examples.

sortTerm

nullCamel 2.15: To configure the sortTerms that IMAP supports to sort the searched mails. You may need to define an array of

com.sun.mail.imap.sortTerm in the registry first and #name to reference it in this URI option.

Camel 2.16: You can also specify a comma separated list of sort terms on the URI that Camel will convert internally. For example, to sort descending by date you would use sortTerm=reverse,date. You can use any of the sort terms defined in com.sun.mail.imap.SortTerm.

postProcessAction

nullCamel 2.15: Refers to aorg.apache.camel.component.mail.MailBoxPostProcessAction for doing post processing tasks on the mailbox once the normal processing ended.
skipFailedMessagefalseCamel 2.15.1: If the mail consumer cannot retrieve a given mail message, then this option allows to skip the message and move on to retrieve the next mail message. The default behavior would be the consumer throws an exception and no mails from the batch would be able to be routed by Camel.
handleFailedMessagefalseCamel 2.15.1: If the mail consumer cannot retrieve a given mail message, then this option allows to handle the caused exception by the consumer's error handler. By enable the bridge error handler on the consumer, then the Camel routing error handler can handle the exception instead. The default behavior would be the consumer throws an exception and no mails from the batch would be able to be routed by Camel.
dummyTrustManager
falseCamel 2.17:To use a dummy security setting for trusting all certificates. Should only be used for development mode, and not production.
idempotentRepositorynullCamel 2.17: A pluggable repository org.apache.camel.spi.IdempotentRepository which allows to cluster consuming from the same mailbox, and let the repository coordinate whether a mail message is valid for the consumer to process.
idempotentRepositoryRemoveOnCommittrueCamel 2.17: When using idempotent repository, then when the mail message has been successfully processed and is committed, should the message id be removed from the idempotent repository (default) or be kept in the repository. By default its assumed the message id is unique and has no value to be kept in the repository, because the mail message will be marked as seen/moved or deleted to prevent it from being consumed again. And therefore having the message id stored in the idempotent repository has little value. However this option allows to store the message id, for whatever reason you may have.
mailUidGenerator Camel 2.17: A pluggable MailUidGenerator that allows to use custom logic to generate UUID of the mail message.

SSL support

The underlying mail framework is responsible for providing SSL support.  You may either configure SSL/TLS support by completely specifying the necessary Java Mail API configuration options, or you may provide a configured SSLContextParameters through the component or endpoint configuration.

Using the JSSE Configuration Utility

As of Camel 2.10, the mail component supports SSL/TLS configuration through the Camel JSSE Configuration Utility.  This utility greatly decreases the amount of component specific code you need to write and is configurable at the endpoint and component levels.  The following examples demonstrate how to use the utility with the mail component.

Programmatic configuration of the endpoint
KeyStoreParameters ksp = new KeyStoreParameters(); ksp.setResource("/users/home/server/truststore.jks"); ksp.setPassword("keystorePassword"); TrustManagersParameters tmp = new TrustManagersParameters(); tmp.setKeyStore(ksp); SSLContextParameters scp = new SSLContextParameters(); scp.setTrustManagers(tmp); Registry registry = ... registry.bind("sslContextParameters", scp); ... from(...) &nbsp; &nbsp; .to("smtps://smtp.google.com?username=user@gmail.com&password=password&sslContextParameters=#sslContextParameters");
Spring DSL based configuration of endpoint
xml... <camel:sslContextParameters id="sslContextParameters"> <camel:trustManagers> <camel:keyStore resource="/users/home/server/truststore.jks" password="keystorePassword"/> </camel:trustManagers> </camel:sslContextParameters>... ... <to uri="smtps://smtp.google.com?username=user@gmail.com&password=password&sslContextParameters=#sslContextParameters"/>...

Configuring JavaMail Directly

Camel uses SUN JavaMail, which only trusts certificates issued by well known Certificate Authorities (the default JVM trust configuration). If you issue your own certificates, you have to import the CA certificates into the JVM's Java trust/key store files, override the default JVM trust/key store files (see SSLNOTES.txt in JavaMail for details).

Mail Message Content

Camel uses the message exchange's IN body as the MimeMessage text content. The body is converted to String.class.

Camel copies all of the exchange's IN headers to the MimeMessage headers. You may wish to read How to avoid sending some or all message headers to prevent inadvertent data "leaks" from your application.

The subject of the MimeMessage can be configured using a header property on the IN message. The code below demonstrates this:{snippet:id=e1|lang=java|url=camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSubjectTest.java}The same applies for other MimeMessage headers such as recipients, so you can use a header property as To:{snippet:id=e1|lang=java|url=camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailUsingHeadersTest.java}Since Camel 2.11 When using the MailProducer the send the mail to server, you should be able to get the message id of the MimeMessage with the key CamelMailMessageId from the Camel message header.

Headers take precedence over pre-configured recipients

The recipients specified in the message headers always take precedence over recipients pre-configured in the endpoint URI. The idea is that if you provide any recipients in the message headers, that is what you get. The recipients pre-configured in the endpoint URI are treated as a fallback.

In the sample code below, the email message is sent to davsclaus@apache.org, because it takes precedence over the pre-configured recipient, info@mycompany.com. Any cc and bcc settings in the endpoint URI are also ignored and those recipients will not receive any mail. The choice between headers and pre-configured settings is all or nothing: the mail component either takes the recipients exclusively from the headers or exclusively from the pre-configured settings. It is not possible to mix and match headers and pre-configured settings.

java Map<String, Object> headers = new HashMap<String, Object>(); headers.put("to", "davsclaus@apache.org"); template.sendBodyAndHeaders("smtp://admin@localhost?to=info@mycompany.com", "Hello World", headers);

Multiple recipients for easier configuration

It is possible to set multiple recipients using a comma-separated or a semicolon-separated list. This applies both to header settings and to settings in an endpoint URI. For example:

java Map<String, Object> headers = new HashMap<String, Object>(); headers.put("to", "davsclaus@apache.org ; jstrachan@apache.org ; ningjiang@apache.org");

The preceding example uses a semicolon, ;, as the separator character.

Setting sender name and email

You can specify recipients in the format, name <email>, to include both the name and the email address of the recipient.

For example, you define the following headers on the a Message:

Map headers = new HashMap(); map.put("To", "Claus Ibsen <davsclaus@apache.org>"); map.put("From", "James Strachan <jstrachan@apache.org>"); map.put("Subject", "Camel is cool");

JavaMail API (ex SUN JavaMail)

JavaMail API is used under the hood for consuming and producing mails.
We encourage end-users to consult these references when using either POP3 or IMAP protocol. Note particularly that POP3 has a much more limited set of features than IMAP.

Samples

We start with a simple route that sends the messages received from a JMS queue as emails. The email account is the admin account on mymailserver.com.

from("jms://queue:subscription").to("smtp://admin@mymailserver.com?password=secret");

In the next sample, we poll a mailbox for new emails once every minute. Notice that we use the special consumer option for setting the poll interval, consumer.delay, as 60000 milliseconds = 60 seconds.

from("imap://admin@mymailserver.com password=secret&unseen=true&consumer.delay=60000") .to("seda://mails");

In this sample we want to send a mail to multiple recipients:{snippet:id=e1|lang=java|url=camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailRecipientsTest.java}

Sending mail with attachment sample

Attachments are not support by all Camel components

The Attachments API is based on the Java Activation Framework and is generally only used by the Mail API. Since many of the other Camel components do not support attachments, the attachments could potentially be lost as they propagate along the route. The rule of thumb, therefore, is to add attachments just before sending a message to the mail endpoint.

The mail component supports attachments. In the sample below, we send a mail message containing a plain text message with a logo file attachment.{snippet:id=e1|lang=java|url=camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentTest.java}

SSL sample

In this sample, we want to poll our Google mail inbox for mails. To download mail onto a local mail client, Google mail requires you to enable and configure SSL. This is done by logging into your Google mail account and changing your settings to allow IMAP access. Google have extensive documentation on how to do this.

from("imaps://imap.gmail.com?username=YOUR_USERNAME@gmail.com&password=YOUR_PASSWORD" + "&delete=false&unseen=true&consumer.delay=60000").to("log:newmail");

The preceding route polls the Google mail inbox for new mails once every minute and logs the received messages to the newmail logger category.
Running the sample with DEBUG logging enabled, we can monitor the progress in the logs:

2008-05-08 06:32:09,640 DEBUG MailConsumer - Connecting to MailStore imaps//imap.gmail.com:993 (SSL enabled), folder=INBOX 2008-05-08 06:32:11,203 DEBUG MailConsumer - Polling mailfolder: imaps//imap.gmail.com:993 (SSL enabled), folder=INBOX 2008-05-08 06:32:11,640 DEBUG MailConsumer - Fetching 1 messages. Total 1 messages. 2008-05-08 06:32:12,171 DEBUG MailConsumer - Processing message: messageNumber=[332], from=[James Bond <007@mi5.co.uk>], to=YOUR_USERNAME@gmail.com], subject=[... 2008-05-08 06:32:12,187 INFO newmail - Exchange[MailMessage: messageNumber=[332], from=[James Bond <007@mi5.co.uk>], to=YOUR_USERNAME@gmail.com], subject=[...

Consuming mails with attachment sample

In this sample we poll a mailbox and store all attachments from the mails as files. First, we define a route to poll the mailbox. As this sample is based on google mail, it uses the same route as shown in the SSL sample:

from("imaps://imap.gmail.com?username=YOUR_USERNAME@gmail.com&password=YOUR_PASSWORD" + "&delete=false&unseen=true&consumer.delay=60000").process(new MyMailProcessor());

Instead of logging the mail we use a processor where we can process the mail from java code:

public void process(Exchange exchange) throws Exception { // the API is a bit clunky so we need to loop Map<String, DataHandler> attachments = exchange.getIn().getAttachments(); if (attachments.size() > 0) { for (String name : attachments.keySet()) { DataHandler dh = attachments.get(name); // get the file name String filename = dh.getName(); // get the content and convert it to byte[] byte[] data = exchange.getContext().getTypeConverter() .convertTo(byte[].class, dh.getInputStream()); // write the data to a file FileOutputStream out = new FileOutputStream(filename); out.write(data); out.flush(); out.close(); } } }

As you can see the API to handle attachments is a bit clunky but it's there so you can get the javax.activation.DataHandler so you can handle the attachments using standard API.

How to split a mail message with attachments

In this example we consume mail messages which may have a number of attachments. What we want to do is to use the Splitter EIP per individual attachment, to process the attachments separately. For example if the mail message has 5 attachments, we want the Splitter to process five messages, each having a single attachment. To do this we need to provide a custom Expression to the Splitter where we provide a List<Message> that contains the five messages with the single attachment.

The code is provided out of the box in Camel 2.10 onwards in the camel-mail component. The code is in the class: org.apache.camel.component.mail.SplitAttachmentsExpression, which you can find the source code here

In the Camel route you then need to use this Expression in the route as shown below:{snippet:id=e1|lang=java|url=camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailSplitAttachmentsTest.java}If you use XML DSL then you need to declare a method call expression in the Splitter as shown below

xml<split> <method beanType="org.apache.camel.component.mail.SplitAttachmentsExpression"/> <to uri="mock:split"/> </split>

 

From Camel 2.16 onwards you can also split the attachments as byte[] to be stored as the message body. This is done by creating the expression with boolean true

SplitAttachmentsExpression split = SplitAttachmentsExpression(true);

And then use the expression with the splitter eip.

Using custom SearchTerm

Available as of Camel 2.11

You can configure a searchTerm on the MailEndpoint which allows you to filter out unwanted mails.

For example to filter mails to contain Camel in either Subject or Text you can do as follows:

xml<route> <from uri="imaps://mymailseerver?username=foo&password=secret&searchTerm.subjectOrBody=Camel"/> <to uri="bean:myBean"/> </route>

Notice we use the "searchTerm.subjectOrBody" as parameter key to indicate that we want to search on mail subject or body, to contain the word "Camel".
The class org.apache.camel.component.mail.SimpleSearchTerm has a number of options you can configure:

Or to get the new unseen emails going 24 hours back in time you can do. Notice the "now-24h" syntax. See the table below for more details.

xml<route> <from uri="imaps://mymailseerver?username=foo&password=secret&searchTerm.fromSentDate=now-24h"/> <to uri="bean:myBean"/> </route>

You can have multiple searchTerm in the endpoint uri configuration. They would then be combined together using AND operator, eg so both conditions must match. For example to get the last unseen emails going back 24 hours which has Camel in the mail subject you can do:

xml<route> <from uri="imaps://mymailseerver?username=foo&password=secret&searchTerm.subject=Camel&searchTerm.fromSentDate=now-24h"/> <to uri="bean:myBean"/> </route> confluenceTableSmall

Option

Default

Description

unseen

true

Whether to limit by unseen mails only.

subjectOrBody

null

To limit by subject or body to contain the word.

subject

null

The subject must contain the word.

body

null

The body must contain the word.

from

null

The mail must be from a given email pattern.

to

null

The mail must be to a given email pattern.

fromSentDate

null

The mail must be sent after or equals (GE) a given date. The date pattern is yyyy-MM-dd HH:mm:SS, eg use "2012-01-01 00:00:00" to be from the year 2012 onwards. You can use "now" for current timestamp. The "now" syntax supports an optional offset, that can be specified as either + or - with a numeric value. For example for last 24 hours, you can use "now - 24h" or without spaces "now-24h". Notice that Camel supports shorthands for hours, minutes, and seconds.

toSentDate

null

The mail must be sent before or equals (BE) a given date. The date pattern is yyyy-MM-dd HH:mm:SS, eg use "2012-01-01 00:00:00" to be before the year 2012. You can use "now" for current timestamp. The "now" syntax supports an optional offset, that can be specified as either + or - with a numeric value. For example for last 24 hours, you can use "now - 24h" or without spaces "now-24h". Notice that Camel supports shorthands for hours, minutes, and seconds.

The SimpleSearchTerm is designed to be easily configurable from a POJO, so you can also configure it using a <bean> style in XML

<bean id="mySearchTerm" class="org.apache.camel.component.mail.SimpleSearchTerm"> <property name="subject" value="Order"/> <property name="to" value="acme-order@acme.com"/> <property name="fromSentDate" value="now"/> </bean>

You can then refer to this bean, using #beanId in your Camel route as shown:

xml<route> <from uri="imaps://mymailseerver?username=foo&password=secret&searchTerm=#mySearchTerm"/> <to uri="bean:myBean"/> </route>

In Java there is a builder class to build compound SearchTerm}}s using the {{org.apache.camel.component.mail.SearchTermBuilder class.
This allows you to build complex terms such as:

// we just want the unseen mails which is not spam SearchTermBuilder builder = new SearchTermBuilder(); builder.unseen().body(Op.not, "Spam").subject(Op.not, "Spam") // which was sent from either foo or bar .from("foo@somewhere.com").from(Op.or, "bar@somewhere.com"); // .. and we could continue building the terms SearchTerm term = builder.build();

Endpoint See Also

MINA Component

Deprecated

Deprecated

This component is deprecated as the Apache Mina 1.x project is EOL. Instead use MINA2 or Netty instead.

The mina: component is a transport for working with Apache MINA

Maven users will need to add the following dependency to their pom.xml for this component:

xml<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-mina</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>

URI format

mina:tcp://hostname[:port][?options] mina:udp://hostname[:port][?options] mina:vm://hostname[:port][?options]

You can specify a codec in the Registry using the codec option. If you are using TCP and no codec is specified then the textline flag is used to determine if text line based codec or object serialization should be used instead. By default the object serialization is used.

For UDP if no codec is specified the default uses a basic ByteBuffer based codec.

The VM protocol is used as a direct forwarding mechanism in the same JVM. See the MINA VM-Pipe API documentation for details.

A Mina producer has a default timeout value of 30 seconds, while it waits for a response from the remote server.

In normal use, camel-mina only supports marshalling the body content—message headers and exchange properties are not sent.
However, the option, transferExchange, does allow you to transfer the exchange itself over the wire. See options below.

You can append query options to the URI in the following format, ?option=value&option=value&...

Options

confluenceTableSmall

Option

Default Value

Description

codec

null

You can refer to a named ProtocolCodecFactory instance in your Registry such as your Spring ApplicationContext, which is then used for the marshalling.

codec

null

You must use the # notation to look up your codec in the Registry. For example, use #myCodec to look up a bean with the id value, myCodec.

disconnect

false

Camel 2.3: Whether or not to disconnect(close) from Mina session right after use. Can be used for both consumer and producer.

textline

false

Only used for TCP. If no codec is specified, you can use this flag to indicate a text line based codec; if not specified or the value is false, then Object Serialization is assumed over TCP.

textlineDelimiter

DEFAULT

Only used for TCP and if textline=true. Sets the text line delimiter to use. Possible values are: DEFAULT, AUTO, WINDOWS, UNIX or MAC. If none provided, Camel will use DEFAULT. This delimiter is used to mark the end of text.

sync

true

Setting to set endpoint as one-way or request-response.

lazySessionCreation

true

Sessions can be lazily created to avoid exceptions, if the remote server is not up and running when the Camel producer is started.

timeout

30000

You can configure the timeout that specifies how long to wait for a response from a remote server. The timeout unit is in milliseconds, so 60000 is 60 seconds. The timeout is only used for Mina producer.

encoding

JVM Default

You can configure the encoding (a charset name) to use for the TCP textline codec and the UDP protocol. If not provided, Camel will use the JVM default Charset.

transferExchange

false

Only used for TCP. You can transfer the exchange over the wire instead of just the body. The following fields are transferred: In body, Out body, fault body, In headers, Out headers, fault headers, exchange properties, exchange exception. This requires that the objects are serializable. Camel will exclude any non-serializable objects and log it at WARN level.

minaLogger

false

You can enable the Apache MINA logging filter. Apache MINA uses slf4j logging at INFO level to log all input and output.

filters

null

You can set a list of Mina IoFilters to register. The filters value must be one of the following:

  • Camel 2.2: comma-separated list of bean references (e.g. #filterBean1,#filterBean2) where each bean must be of type org.apache.mina.common.IoFilter.
  • before Camel 2.2: a reference to a bean of type List<org.apache.mina.common.IoFilter>.

encoderMaxLineLength

-1

As of 2.1, you can set the textline protocol encoder max line length. By default the default value of Mina itself is used which are Integer.MAX_VALUE.

decoderMaxLineLength

-1

As of 2.1, you can set the textline protocol decoder max line length. By default the default value of Mina itself is used which are 1024.

producerPoolSize

16

The TCP producer is thread safe and supports concurrency much better. This option allows you to configure the number of threads in its thread pool for concurrent producers. Note: Camel has a pooled service which ensured it was already thread safe and supported concurrency already.

allowDefaultCodec

true

The mina component installs a default codec if both, codec is null and textline is false. Setting allowDefaultCodec to false prevents the mina component from installing a default codec as the first element in the filter chain. This is useful in scenarios where another filter must be the first in the filter chain, like the SSL filter.

disconnectOnNoReply

true

Camel 2.3: If sync is enabled then this option dictates MinaConsumer if it should disconnect where there is no reply to send back.

noReplyLogLevel

WARN

Camel 2.3: If sync is enabled this option dictates MinaConsumer which logging level to use when logging a there is no reply to send back. Values are: FATAL, ERROR, INFO, DEBUG, OFF.

clientModefalseCamel 2.15: Consumer only. If the clientMode is true, mina consumer will connect the address as a TCP client.

Using a custom codec

See the Mina documentation how to write your own codec. To use your custom codec with camel-mina, you should register your codec in the Registry; for example, by creating a bean in the Spring XML file. Then use the codec option to specify the bean ID of your codec. See HL7 that has a custom codec.

Sample with sync=false

In this sample, Camel exposes a service that listens for TCP connections on port 6200. We use the textline codec. In our route, we create a Mina consumer endpoint that listens on port 6200:

{snippet:id=e1|lang=java|url=camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaConsumerTest.java}

As the sample is part of a unit test, we test it by sending some data to it on port 6200.

{snippet:id=e2|lang=java|url=camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaConsumerTest.java}

Sample with sync=true

In the next sample, we have a more common use case where we expose a TCP service on port 6201 also use the textline codec. However, this time we want to return a response, so we set the sync option to true on the consumer.

{snippet:id=e3|lang=java|url=camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaConsumerTest.java}

Then we test the sample by sending some data and retrieving the response using the template.requestBody() method. As we know the response is a String, we cast it to String and can assert that the response is, in fact, something we have dynamically set in our processor code logic.

{snippet:id=e4|lang=java|url=camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaConsumerTest.java}

Sample with Spring DSL

Spring DSL can, of course, also be used for MINA. In the sample below we expose a TCP server on port 5555:

xml <route> <from uri="mina:tcp://localhost:5555?textline=true"/> <to uri="bean:myTCPOrderHandler"/> </route>

In the route above, we expose a TCP server on port 5555 using the textline codec. We let the Spring bean with ID, myTCPOrderHandler, handle the request and return a reply. For instance, the handler bean could be implemented as follows:

java public String handleOrder(String payload) { ... return "Order: OK" }

Configuring Mina endpoints using Spring bean style

Configuration of Mina endpoints is possible using regular Spring bean style configuration in the Spring DSL.

However, in the underlying Apache Mina toolkit, it is relatively difficult to set up the acceptor and the connector, because you can not use simple setters. To resolve this difficulty, we leverage the MinaComponent as a Spring factory bean to configure this for us. If you really need to configure this yourself, there are setters on the MinaEndpoint to set these when needed.

The sample below shows the factory approach:

{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-mina/src/test/resources/org/apache/camel/component/mina/SpringMinaEndpointTest-context.xml}

And then we can refer to our endpoint directly in the route, as follows:

{snippet:id=e2|lang=xml|url=camel/trunk/components/camel-mina/src/test/resources/org/apache/camel/component/mina/SpringMinaEndpointTest-context.xml}

Closing Session When Complete

When acting as a server you sometimes want to close the session when, for example, a client conversion is finished. To instruct Camel to close the session, you should add a header with the key CamelMinaCloseSessionWhenComplete set to a boolean true value.

For instance, the example below will close the session after it has written the bye message back to the client:

java from("mina:tcp://localhost:8080?sync=true&textline=true").process(new Processor() { public void process(Exchange exchange) throws Exception { String body = exchange.getIn().getBody(String.class); exchange.getOut().setBody("Bye " + body); exchange.getOut().setHeader(MinaConstants.MINA_CLOSE_SESSION_WHEN_COMPLETE, true); } });

Get the IoSession for message

Available since Camel 2.1
You can get the IoSession from the message header with this key MinaEndpoint.HEADER_MINA_IOSESSION, and also get the local host address with the key MinaEndpoint.HEADER_LOCAL_ADDRESS and remote host address with the key MinaEndpoint.HEADER_REMOTE_ADDRESS.

Configuring Mina filters

Filters permit you to use some Mina Filters, such as SslFilter. You can also implement some customized filters. Please note that codec and logger are also implemented as Mina filters of type, IoFilter. Any filters you may define are appended to the end of the filter chain; that is, after codec and logger.

If using the SslFilter you need to add the mina-filter-ssl JAR to the classpath.

For instance, the example below will send a keep-alive message after 10 seconds of inactivity:

javapublic class KeepAliveFilter extends IoFilterAdapter { @Override public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception { session.setIdleTime(IdleStatus.BOTH_IDLE, 10); nextFilter.sessionCreated(session); } @Override public void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception { session.write("NOOP"); // NOOP is a FTP command for keep alive nextFilter.sessionIdle(session, status); } }

As Camel Mina may use a request-reply scheme, the endpoint as a client would like to drop some message, such as greeting when the connection is established. For example, when you connect to an FTP server, you will get a 220 message with a greeting (220 Welcome to Pure-FTPd). If you don't drop the message, your request-reply scheme will be broken.

javapublic class DropGreetingFilter extends IoFilterAdapter { @Override public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception { if (message instanceof String) { String ftpMessage = (String) message; // "220" is given as greeting. "200 Zzz" is given as a response to "NOOP" (keep alive) if (ftpMessage.startsWith("220") || or ftpMessage.startsWith("200 Zzz")) { // Dropping greeting return; } } nextFilter.messageReceived(session, message); } }

Then, you can configure your endpoint using Spring DSL:

xml<bean id="myMinaFactory" class="org.apache.camel.component.mina.MinaComponent"> <constructor-arg index="0" ref="camelContext" /> </bean> <bean id="myMinaEndpoint" factory-bean="myMinaFactory" factory-method="createEndpoint"> <constructor-arg index="0" ref="myMinaConfig"/> </bean> <bean id="myMinaConfig" class="org.apache.camel.component.mina.MinaConfiguration"> <property name="protocol" value="tcp" /> <property name="host" value="localhost" /> <property name="port" value="2121" /> <property name="sync" value="true" /> <property name="minaLogger" value="true" /> <property name="filters" ref="listFilters"/> </bean> <bean id="listFilters" class="java.util.ArrayList" > <constructor-arg> <list value-type="org.apache.mina.common.IoFilter"> <bean class="com.example.KeepAliveFilter"/> <bean class="com.example.DropGreetingFilter"/> </list> </constructor-arg> </bean>

Endpoint See Also

Mock Component

Testing Summary Include

The Mock component provides a powerful declarative testing mechanism, which is similar to jMock in that it allows declarative expectations to be created on any Mock endpoint before a test begins. Then the test is run, which typically fires messages to one or more endpoints, and finally the expectations can be asserted in a test case to ensure the system worked as expected.

This allows you to test various things like:

  • The correct number of messages are received on each endpoint,
  • The correct payloads are received, in the right order,
  • Messages arrive on an endpoint in order, using some Expression to create an order testing function,
  • Messages arrive match some kind of Predicate such as that specific headers have certain values, or that parts of the messages match some predicate, such as by evaluating an XPath or XQuery Expression.

Note that there is also the Test endpoint which is a Mock endpoint, but which uses a second endpoint to provide the list of expected message bodies and automatically sets up the Mock endpoint assertions. In other words, it's a Mock endpoint that automatically sets up its assertions from some sample messages in a File or database, for example.

Mock endpoints keep received Exchanges in memory indefinitely

Remember that Mock is designed for testing. When you add Mock endpoints to a route, each Exchange sent to the endpoint will be stored (to allow for later validation) in memory until explicitly reset or the JVM is restarted. If you are sending high volume and/or large messages, this may cause excessive memory use. If your goal is to test deployable routes inline, consider using NotifyBuilder or AdviceWith in your tests instead of adding Mock endpoints to routes directly.

From Camel 2.10 onwards there are two new options retainFirst, and retainLast that can be used to limit the number of messages the Mock endpoints keep in memory.

URI format

mock:someName[?options]

Where someName can be any string that uniquely identifies the endpoint.

You can append query options to the URI in the following format, ?option=value&option=value&...

Options

confluenceTableSmall

Option

Default

Description

reportGroup

null

A size to use a throughput logger for reporting

retainFirst

 

Camel 2.10: To only keep first X number of messages in memory.

retainLast

 

Camel 2.10: To only keep last X number of messages in memory.

Simple Example

Here's a simple example of Mock endpoint in use. First, the endpoint is resolved on the context. Then we set an expectation, and then, after the test has run, we assert that our expectations have been met.

MockEndpoint resultEndpoint = context.resolveEndpoint("mock:foo", MockEndpoint.class); resultEndpoint.expectedMessageCount(2); // send some messages ... // now lets assert that the mock:foo endpoint received 2 messages resultEndpoint.assertIsSatisfied();

You typically always call the assertIsSatisfied() method to test that the expectations were met after running a test.

Camel will by default wait 10 seconds when the assertIsSatisfied() is invoked. This can be configured by setting the setResultWaitTime(millis) method.

Using assertPeriod

Available as of Camel 2.7
When the assertion is satisfied then Camel will stop waiting and continue from the assertIsSatisfied method. That means if a new message arrives on the mock endpoint, just a bit later, that arrival will not affect the outcome of the assertion. Suppose you do want to test that no new messages arrives after a period thereafter, then you can do that by setting the setAssertPeriod method, for example:

MockEndpoint resultEndpoint = context.resolveEndpoint("mock:foo", MockEndpoint.class); resultEndpoint.setAssertPeriod(5000); resultEndpoint.expectedMessageCount(2); // send some messages ... // now lets assert that the mock:foo endpoint received 2 messages resultEndpoint.assertIsSatisfied();

Setting expectations

You can see from the javadoc of MockEndpoint the various helper methods you can use to set expectations. The main methods are as follows:

confluenceTableSmall

Method

Description

expectedMessageCount(int)

To define the expected message count on the endpoint.

expectedMinimumMessageCount(int)

To define the minimum number of expected messages on the endpoint.

expectedBodiesReceived(...)

To define the expected bodies that should be received (in order).

expectedHeaderReceived(...)

To define the expected header that should be received

expectsAscending(Expression)

To add an expectation that messages are received in order, using the given Expression to compare messages.

expectsDescending(Expression)

To add an expectation that messages are received in order, using the given Expression to compare messages.

expectsNoDuplicates(Expression)

To add an expectation that no duplicate messages are received; using an Expression to calculate a unique identifier for each message. This could be something like the JMSMessageID if using JMS, or some unique reference number within the message.

Here's another example:

resultEndpoint.expectedBodiesReceived("firstMessageBody", "secondMessageBody", "thirdMessageBody");

Adding expectations to specific messages

In addition, you can use the message(int messageIndex) method to add assertions about a specific message that is received.

For example, to add expectations of the headers or body of the first message (using zero-based indexing like java.util.List), you can use the following code:

resultEndpoint.message(0).header("foo").isEqualTo("bar");

There are some examples of the Mock endpoint in use in the camel-core processor tests.

Mocking existing endpoints

Available as of Camel 2.7

Camel now allows you to automatically mock existing endpoints in your Camel routes.

How it works

Important: The endpoints are still in action. What happens differently is that a Mock endpoint is injected and receives the message first and then delegates the message to the target endpoint. You can view this as a kind of intercept and delegate or endpoint listener.

Suppose you have the given route below:

{snippet:id=route|title=Route|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockEndpointsTest.java}

You can then use the adviceWith feature in Camel to mock all the endpoints in a given route from your unit test, as shown below:

{snippet:id=e1|title=adviceWith mocking all endpoints|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockEndpointsTest.java}

Notice that the mock endpoints is given the uri mock:<endpoint>, for example mock:direct:foo. Camel logs at INFO level the endpoints being mocked:

INFO Adviced endpoint [direct://foo] with mock endpoint [mock:direct:foo] Mocked endpoints are without parameters

Endpoints which are mocked will have their parameters stripped off. For example the endpoint "log:foo?showAll=true" will be mocked to the following endpoint "mock:log:foo". Notice the parameters have been removed.

Its also possible to only mock certain endpoints using a pattern. For example to mock all log endpoints you do as shown:

{snippet:id=e2|lang=java|title=adviceWith mocking only log endpoints using a pattern|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockEndpointsTest.java}

The pattern supported can be a wildcard or a regular expression. See more details about this at Intercept as its the same matching function used by Camel.

Mind that mocking endpoints causes the messages to be copied when they arrive on the mock.
That means Camel will use more memory. This may not be suitable when you send in a lot of messages.

Mocking existing endpoints using the camel-test component

Instead of using the adviceWith to instruct Camel to mock endpoints, you can easily enable this behavior when using the camel-test Test Kit.
The same route can be tested as follows. Notice that we return "*" from the isMockEndpoints method, which tells Camel to mock all endpoints.
If you only want to mock all log endpoints you can return "log*" instead.

{snippet:id=e1|lang=java|title=isMockEndpoints using camel-test kit|url=camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/IsMockEndpointsJUnit4Test.java}

Mocking existing endpoints with XML DSL

If you do not use the camel-test component for unit testing (as shown above) you can use a different approach when using XML files for routes.
The solution is to create a new XML file used by the unit test and then include the intended XML file which has the route you want to test.

Suppose we have the route in the camel-route.xml file:

{snippet:id=e1|lang=xml|title=camel-route.xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/camel-route.xml}

Then we create a new XML file as follows, where we include the camel-route.xml file and define a spring bean with the class org.apache.camel.impl.InterceptSendToMockEndpointStrategy which tells Camel to mock all endpoints:

{snippet:id=e1|lang=xml|title=test-camel-route.xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/InterceptSendToMockEndpointStrategyTest.xml}

Then in your unit test you load the new XML file (test-camel-route.xml) instead of camel-route.xml.

To only mock all Log endpoints you can define the pattern in the constructor for the bean:

xml<bean id="mockAllEndpoints" class="org.apache.camel.impl.InterceptSendToMockEndpointStrategy"> <constructor-arg index="0" value="log*"/> </bean>

Mocking endpoints and skip sending to original endpoint

Available as of Camel 2.10

Sometimes you want to easily mock and skip sending to a certain endpoints. So the message is detoured and send to the mock endpoint only. From Camel 2.10 onwards you can now use the mockEndpointsAndSkip method using AdviceWith or the Test Kit. The example below will skip sending to the two endpoints "direct:foo", and "direct:bar".

{snippet:id=e1|lang=java|title=adviceWith mock and skip sending to endpoints|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockMultipleEndpointsWithSkipTest.java}

The same example using the Test Kit

{snippet:id=e1|lang=java|title=isMockEndpointsAndSkip using camel-test kit|url=camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/IsMockEndpointsAndSkipJUnit4Test.java}

Limiting the number of messages to keep

Available as of Camel 2.10

The Mock endpoints will by default keep a copy of every Exchange that it received. So if you test with a lot of messages, then it will consume memory.
From Camel 2.10 onwards we have introduced two options retainFirst and retainLast that can be used to specify to only keep N'th of the first and/or last Exchanges.

For example in the code below, we only want to retain a copy of the first 5 and last 5 Exchanges the mock receives.

MockEndpoint mock = getMockEndpoint("mock:data"); mock.setRetainFirst(5); mock.setRetainLast(5); mock.expectedMessageCount(2000); ... mock.assertIsSatisfied();

Using this has some limitations. The getExchanges() and getReceivedExchanges() methods on the MockEndpoint will return only the retained copies of the Exchanges. So in the example above, the list will contain 10 Exchanges; the first five, and the last five.
The retainFirst and retainLast options also have limitations on which expectation methods you can use. For example the expectedXXX methods that work on message bodies, headers, etc. will only operate on the retained messages. In the example above they can test only the expectations on the 10 retained messages.

Testing with arrival times

Available as of Camel 2.7

The Mock endpoint stores the arrival time of the message as a property on the Exchange.

Date time = exchange.getProperty(Exchange.RECEIVED_TIMESTAMP, Date.class);

You can use this information to know when the message arrived on the mock. But it also provides foundation to know the time interval between the previous and next message arrived on the mock. You can use this to set expectations using the arrives DSL on the Mock endpoint.

For example to say that the first message should arrive between 0-2 seconds before the next you can do:

mock.message(0).arrives().noLaterThan(2).seconds().beforeNext();

You can also define this as that 2nd message (0 index based) should arrive no later than 0-2 seconds after the previous:

mock.message(1).arrives().noLaterThan(2).seconds().afterPrevious();

You can also use between to set a lower bound. For example suppose that it should be between 1-4 seconds:

mock.message(1).arrives().between(1, 4).seconds().afterPrevious();

You can also set the expectation on all messages, for example to say that the gap between them should be at most 1 second:

mock.allMessages().arrives().noLaterThan(1).seconds().beforeNext(); time units

In the example above we use seconds as the time unit, but Camel offers milliseconds, and minutes as well.

Endpoint See Also

MSV Component

The MSV component performs XML validation of the message body using the MSV Library and any of the supported XML schema languages, such as XML Schema or RelaxNG XML Syntax.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-msv</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

Note that the Jing component also supports RelaxNG Compact Syntax

URI format

msv:someLocalOrRemoteResource[?options]

Where someLocalOrRemoteResource is some URL to a local resource on the classpath or a full URL to a remote resource or resource on the file system. For example

msv:org/foo/bar.rng
msv:file:../foo/bar.rng
msv:http://acme.com/cheese.rng

You can append query options to the URI in the following format, ?option=value&option=value&...

Options

Option

Default

Description

useDom

true

Whether DOMSource/DOMResult or SaxSource/SaxResult should be used by the validator. Note: DOM must be used by the MSV component.

Example

The following example shows how to configure a route from endpoint direct:start which then goes to one of two endpoints, either mock:valid or mock:invalid based on whether or not the XML matches the given RelaxNG XML Schema (which is supplied on the classpath).

Error formatting macro: snippet: java.lang.NullPointerException

Pojo Component

The pojo: component is now just an alias for the Bean component.

Has been removed in Camel 2.0.

Quartz Component

The quartz: component provides a scheduled delivery of messages using the Quartz Scheduler 1.x .
Each endpoint represents a different timer (in Quartz terms, a Trigger and JobDetail).

If you are using Quartz 2.x then from Camel 2.12 onwards there is a Quartz2 component you should use

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-quartz</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

quartz://timerName?options
quartz://groupName/timerName?options
quartz://groupName/timerName?cron=expression
quartz://timerName?cron=expression

The component uses either a CronTrigger or a SimpleTrigger. If no cron expression is provided, the component uses a simple trigger. If no groupName is provided, the quartz component uses the Camel group name.

You can append query options to the URI in the following format, ?option=value&option=value&...

Options

Parameter

Default

Description

cron

None

Specifies a cron expression (not compatible with the trigger.* or job.* options).

trigger.repeatCount

0

SimpleTrigger: How many times should the timer repeat?

trigger.repeatInterval

0

SimpleTrigger: The amount of time in milliseconds between repeated triggers.

job.name

null

Sets the job name.

job.XXX

null

Sets the job option with the XXX setter name.

trigger.XXX

null

Sets the trigger option with the XXX setter name.

stateful

false

Uses a Quartz StatefulJob instead of the default job.

fireNow

false

New to Camel 2.2.0, if it is true will fire the trigger when the route is start when using SimpleTrigger.

deleteJob

true

Camel 2.12: If set to true, then the trigger automatically delete when route stop. Else if set to false, it will remain in scheduler. When set to false, it will also mean user may reuse pre-configured trigger with camel Uri. Just ensure the names match. Notice you cannot have both deleteJob and pauseJob set to true.

pauseJob

false

Camel 2.12: If set to true, then the trigger automatically pauses when route stop. Else if set to false, it will remain in scheduler. When set to false, it will also mean user may reuse pre-configured trigger with camel Uri. Just ensure the names match. Notice you cannot have both deleteJob and pauseJob set to true.

usingFixedCamelContextName

falseCamel 2.15.0: If it is true, JobDataMap uses the CamelContext name directly to reference the camel context, if it is false, JobDataMap uses use the CamelContext management name which could be changed during the deploy time.

For example, the following routing rule will fire two timer events to the mock:results endpoint:

Error formatting macro: snippet: java.lang.NullPointerException

When using a StatefulJob, the JobDataMap is re-persisted after every execution of the job, thus preserving state for the next execution.

Running in OSGi and having multiple bundles with quartz routes

If you run in OSGi such as Apache ServiceMix, or Apache Karaf, and have multiple bundles with Camel routes that start from Quartz endpoints, then make sure if you assign
an id to the <camelContext> that this id is unique, as this is required by the QuartzScheduler in the OSGi container. If you do not set any id on <camelContext> then
a unique id is auto assigned, and there is no problem.

Configuring quartz.properties file

By default Quartz will look for a quartz.properties file in the org/quartz directory of the classpath. If you are using WAR deployments this means just drop the quartz.properties in WEB-INF/classes/org/quartz.

However the Camel Quartz component also allows you to configure properties:

Parameter

Default

Type

Description

properties

null

Properties

Camel 2.4: You can configure a java.util.Properties instance.

propertiesFile

null

String

Camel 2.4: File name of the properties to load from the classpath

To do this you can configure this in Spring XML as follows

<bean id="quartz" class="org.apache.camel.component.quartz.QuartzComponent">
    <property name="propertiesFile" value="com/mycompany/myquartz.properties"/>
</bean>

Enabling Quartz scheduler in JMX

You need to configure the quartz scheduler properties to enable JMX.
That is typically setting the option "org.quartz.scheduler.jmx.export" to a true value in the configuration file.

From Camel 2.13 onwards Camel will automatic set this option to true, unless explicit disabled.

Starting the Quartz scheduler

Available as of Camel 2.4

The Quartz component offers an option to let the Quartz scheduler be started delayed, or not auto started at all.

Parameter

Default

Type

Description

startDelayedSeconds

0

int

Camel 2.4: Seconds to wait before starting the quartz scheduler.

autoStartScheduler

true

boolean

Camel 2.4: Whether or not the scheduler should be auto started.

To do this you can configure this in Spring XML as follows

<bean id="quartz" class="org.apache.camel.component.quartz.QuartzComponent">
    <property name="startDelayedSeconds" value="5"/>
</bean>

Clustering

Available as of Camel 2.4

If you use Quartz in clustered mode, e.g. the JobStore is clustered. Then from Camel 2.4 onwards the Quartz component will not pause/remove triggers when a node is being stopped/shutdown. This allows the trigger to keep running on the other nodes in the cluster.

Note: When running in clustered node no checking is done to ensure unique job name/group for endpoints.

Message Headers

Camel adds the getters from the Quartz Execution Context as header values. The following headers are added:
calendar, fireTime, jobDetail, jobInstance, jobRuntTime, mergedJobDataMap, nextFireTime, previousFireTime, refireCount, result, scheduledFireTime, scheduler, trigger, triggerName, triggerGroup.

The fireTime header contains the java.util.Date of when the exchange was fired.

Using Cron Triggers

Quartz supports Cron-like expressions for specifying timers in a handy format. You can use these expressions in the cron URI parameter; though to preserve valid URI encoding we allow + to be used instead of spaces. Quartz provides a little tutorial on how to use cron expressions.

For example, the following will fire a message every five minutes starting at 12pm (noon) to 6pm on weekdays:

from("quartz://myGroup/myTimerName?cron=0+0/5+12-18+?+*+MON-FRI").to("activemq:Totally.Rocks");

which is equivalent to using the cron expression

0 0/5 12-18 ? * MON-FRI

The following table shows the URI character encodings we use to preserve valid URI syntax:

URI Character

Cron character

+

Space

Specifying time zone

Available as of Camel 2.8.1
The Quartz Scheduler allows you to configure time zone per trigger. For example to use a timezone of your country, then you can do as follows:

quartz://groupName/timerName?cron=0+0/5+12-18+?+*+MON-FRI&trigger.timeZone=Europe/Stockholm

The timeZone value is the values accepted by java.util.TimeZone.

In Camel 2.8.0 or older versions you would have to provide your custom String to java.util.TimeZone Type Converter to be able configure this from the endpoint uri.
From Camel 2.8.1 onwards we have included such a Type Converter in the camel-core.

Queue Component

Deprecated

To avoid confusion with JMS queues, this component is now deprecated in 1.1 onwards. Please use the SEDA component instead

The queue: component provides asynchronous SEDA behaviour so that messages are exchanged on a BlockingQueue and consumers are invoked in a seperate thread pool to the producer.

Note that queues are only visible within a single CamelContext. If you want to communicate across CamelContext instances such as to communicate across web applications, see the VM component.

Note also that this component has nothing to do with JMS, if you want a distributed SEA then try using either JMS or ActiveMQ or even MINA

URI format

queue:someName

Where someName can be any string to uniquely identify the endpoint within the current CamelContext

RMI Component

The rmi: component binds Exchanges to the RMI protocol (JRMP).

Since this binding is just using RMI, normal RMI rules still apply regarding what methods can be invoked. This component supports only Exchanges that carry a method invocation from an interface that extends the Remote interface. All parameters in the method should be either Serializable or Remote objects.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-rmi</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

rmi://rmi-regisitry-host:rmi-registry-port/registry-path[?options]

For example:

rmi://localhost:1099/path/to/service

You can append query options to the URI in the following format, ?option=value&option=value&...

Options

Name

Default Value

Description

method

null

You can set the name of the method to invoke.

remoteInterfaces

null

Its now possible to use this option from Camel 2.7: in the XML DSL. It can be a list of interface names separated by comma.

Using

To call out to an existing RMI service registered in an RMI registry, create a route similar to the following:

from("pojo:foo").to("rmi://localhost:1099/foo");

To bind an existing camel processor or service in an RMI registry, define an RMI endpoint as follows:

RmiEndpoint endpoint= (RmiEndpoint) endpoint("rmi://localhost:1099/bar");
endpoint.setRemoteInterfaces(ISay.class);
from(endpoint).to("pojo:bar");

Note that when binding an RMI consumer endpoint, you must specify the Remote interfaces exposed.

In XML DSL you can do as follows from Camel 2.7 onwards:

    <camel:route>
        <from uri="rmi://localhost:37541/helloServiceBean?remoteInterfaces=org.apache.camel.example.osgi.HelloService"/>
        <to uri="bean:helloServiceBean"/>
    </camel:route>

SEDA Component

The seda: component provides asynchronous SEDA behavior, so that messages are exchanged on a BlockingQueue and consumers are invoked in a separate thread from the producer.

Note that queues are only visible within a single CamelContext. If you want to communicate across CamelContext instances (for example, communicating between Web applications), see the VM component.

This component does not implement any kind of persistence or recovery, if the VM terminates while messages are yet to be processed. If you need persistence, reliability or distributed SEDA, try using either JMS or ActiveMQ.

Synchronous

The Direct component provides synchronous invocation of any consumers when a producer sends a message exchange.

URI format

seda:someName[?options]

Where someName can be any string that uniquely identifies the endpoint within the current CamelContext.

You can append query options to the URI in the following format: ?option=value&option=value&...

Options

Name

Since

Default

Description

size

  

The maximum capacity of the seda queue, i.e., the number of messages it can hold.

The default value in Camel 2.2 or older is 1000.

From Camel 2.3: the size is unbounded by default.

 

Note: Care should be taken when using this option. The size is determined by the value specified when the first endpoint is created. Each endpoint must therefore specify the same size.

From Camel 2.11: a validation is taken place to ensure if using mixed queue sizes for the same queue name, Camel would detect this and fail creating the endpoint.

concurrentConsumers

 

1

Number of concurrent threads processing exchanges.

waitForTaskToComplete

 

IfReplyExpected

Option to specify whether the caller should wait for the asynchronous task to complete before continuing.

The following options are supported:

  • Always

  • Never

  • IfReplyExpected

The first two values are self-explanatory.

The last value, IfReplyExpected, will only wait if the message is Request Reply based.

See Async messaging for more details.

timeout

 

30000

Timeout (in milliseconds) before a seda producer will stop waiting for an asynchronous task to complete.

See waitForTaskToComplete and Async for more details.

From Camel 2.2: you can now disable timeout by using 0 or a negative value.

multipleConsumers

2.2

false

Specifies whether multiple consumers are allowed. If enabled, you can use SEDA for Publish-Subscribe messaging. That is, you can send a message to the seda queue and have each consumer receive a copy of the message. When enabled, this option should be specified on every consumer endpoint.

limitConcurrentConsumers

2.3

true

Whether to limit the number of concurrentConsumers to the maximum of 500.

By default, an exception will be thrown if a seda endpoint is configured with a greater number. You can disable that check by turning this option off.

blockWhenFull

2.9

false

Whether a thread that sends messages to a full seda queue will block until the queue's capacity is no longer exhausted. By default, an exception will be thrown stating that the queue is full. By enabling this option, the calling thread will instead block and wait until the message can be accepted.

queueSize

2.9

 

Component only: the maximum size (capacity of the number of messages it can hold) of the seda queue.

This option is used when size is not specified.

pollTimeout

2.9.3

1000

Consumer only: the timeout used when polling. When a timeout occurs, the consumer can check whether it is allowed to continue running. Setting a lower value allows the consumer to react more quickly upon shutdown.

purgeWhenStopping

2.11.1

false

Whether to purge the task queue when stopping the consumer/route. This allows to stop faster, as any pending messages on the queue is discarded.

queue

2.12.0

null

Define the queue instance which will be used by seda endpoint

queueFactory

2.12.0

null

Define the QueueFactory which could create the queue for the seda endpoint

failIfNoConsumers

2.12.0

false

Whether the producer should fail by throwing an exception when sending to a seda queue with no active consumers.

Only one of the options discardIfNoConsumers and failIfNoConsumers can be enabled at the same time.

discardIfNoConsumers

2.16

false

Whether the producer should discard the message (do not add the message to the queue) when sending to a seda queue with no active consumers. 

Only one of the options discardIfNoConsumers and failIfNoConsumers can be enabled at the same time.

Choosing BlockingQueue implementation

Available as of Camel 2.12

xml<bean id="arrayQueue" class="java.util.ArrayBlockingQueue"> <constructor-arg index="0" value="10"> <!-- size --> <constructor-arg index="1" value="true"> <!-- fairness --> </bean> <!-- ... --> <from uri="seda:array?queue=#arrayQueue"/>

By default, the seda component instantiates a LinkedBlockingQueue. However, a different implementation can be chosen by specifying a custom  BlockingQueue implementation. When a custom implementation is configured the size option is ignored.

The list of available BlockingQueueFactory implementations includes:

  • LinkedBlockingQueueFactory
  • ArrayBlockingQueueFactory
  • PriorityBlockingQueueFactory

xml<bean id="priorityQueueFactory" class="org.apache.camel.component.seda.PriorityBlockingQueueFactory"> <property name="comparator"> <bean class="org.apache.camel.demo.MyExchangeComparator"/> </property> </bean> <!-- ...and later --> <from uri="seda:priority?queueFactory=#priorityQueueFactory&size=100"/> <!-- ... --> 

Use of Request Reply

The SEDA component supports using Request Reply, where the caller will wait for the Async route to complete. For instance:

javafrom("mina:tcp://0.0.0.0:9876?textline=true&sync=true") .to("seda:input"); from("seda:input") .to("bean:processInput") .to("bean:createResponse");

In the route above, we have a TCP listener on port 9876 that accepts incoming requests. The request is routed to the seda:input queue. As it is a Request Reply message, we wait for the response. When the consumer on the seda:input queue is complete, it copies the response to the original message response.

until 2.2: Works only with 2 endpoints

Using Request Reply over SEDA or VM only works with 2 endpoints. You cannot chain endpoints by sending to A -> B -> C etc. Only between A -> B. The reason is the implementation logic is fairly simple. To support 3+ endpoints makes the logic much more complex to handle ordering and notification between the waiting threads properly.

This has been improved in Camel 2.3, which allows you to chain as many endpoints as you like.

Concurrent consumers

By default, the SEDA endpoint uses a single consumer thread, but you can configure it to use concurrent consumer threads. So, instead of thread pools you can use:

javafrom("seda:stageName?concurrentConsumers=5") .process(...)

As for the difference between the two, note a thread pool can increase/shrink dynamically at runtime depending on load, whereas the number of concurrent consumers is always fixed.

Thread pools

Be aware that adding a thread pool to a seda endpoint by doing something like:

javafrom("seda:stageName") .thread(5) .process(...)

Can wind up with two BlockQueues: one from the seda endpoint, and one from the workqueue of the thread pool, which may not be what you want. Instead, you might wish to configure a Direct endpoint with a thread pool, which can process messages both synchronously and asynchronously. For example:

javafrom("direct:stageName") .thread(5) .process(...)

You can also directly configure number of threads that process messages on a seda endpoint using the concurrentConsumers option.

Sample

In the route below we use the SEDA queue to send the request to this asynchronous queue to be able to send a fire-and-forget message for further processing in another thread, and return a constant reply in this thread to the original caller.INLINE{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/seda/SedaAsyncRouteTest.java}Here we send a Hello World message and expects the reply to be OK.INLINE{snippet:id=e2|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/seda/SedaAsyncRouteTest.java}The Hello World message will be consumed from the seda queue from another thread for further processing. Since this is from a unit test, it will be sent to a mock endpoint where we can do assertions in the unit test.

Using multipleConsumers

Available as of Camel 2.2

In this example we have defined two consumers and registered them as spring beans.INLINE{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/example/fooEventRoute.xml}Since we have specified multipleConsumers=true on the seda foo endpoint we can have those two consumers receive their own copy of the message as a kind of pub-sub style messaging.

As the beans are part of an unit test they simply send the message to a mock endpoint. Note the use of @Consume to consume from the seda queue.INLINE{snippet:id=e1|lang=java|url=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/example/FooEventConsumer.java}

Extracting Queue Information.

If needed, information such as queue size, etc. can be obtained without using JMX in this fashion:

javaSedaEndpoint seda = context.getEndpoint("seda:xxxx"); int size = seda.getExchanges().size();

Endpoint See Also

String Template

The string-template: component allows you to process a message using a String Template. This can be ideal when using Templating to generate responses for requests.

Maven users will need to add the following dependency to their pom.xml for this component:

xml<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-stringtemplate</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>

URI Format

string-template:templateName[?options]

Where templateName is the classpath-local URI of the template to invoke; or the complete URL of the remote template.

You can append query options to the URI in the following format, ?option=value&option=value&...

Options

confluenceTableSmall

Option

Default

Description

contentCache

false

Cache for the resource content when its loaded.
Note: as of Camel 2.9 cached resource content can be cleared via JMX using the endpoint's clearContentCache operation.

delimiterStart

null

From Camel 2.11.1: configuring the variable start delimiter

delimiterStop

null

From Camel 2.11.1: configuring the variable end delimiter

Headers

Camel will store a reference to the resource in the message header with key, org.apache.camel.stringtemplate.resource. The Resource is an org.springframework.core.io.Resource object.

Hot-Reloading

The string template resource is by default hot-reloadable for both file and classpath resources (expanded jar). If you set contentCache=true, Camel will load the resource just once, disabling hot-reloading. This scenario can be used in production when the resource never changes.

StringTemplate Attributes

Camel will provide exchange information as attributes (just a java.util.Map) to the string template. The Exchange is transferred as:

confluenceTableSmall

Key

Value

exchange

The Exchange itself.

headers

The headers of the IN message.

camelContext

The Camel Context.

request

The IN message.

in

The IN message.

body

The IN message body.

out

The OUT message (only for InOut message exchange pattern).

response

The OUT message (only for InOut message exchange pattern).

From Camel 2.14: you can define the custom context map by setting the message header CamelStringTemplateVariableMap, as shown below:

javaMap<String, Object> variableMap = new HashMap<String, Object>(); Map<String, Object> headersMap = new HashMap<String, Object>(); headersMap.put("name", "Willem"); variableMap.put("headers", headersMap); variableMap.put("body", "Monday"); variableMap.put("exchange", exchange); exchange.getIn().setHeader("CamelStringTemplateVariableMap", variableMap);

Samples

For example you could use a string template as follows in order to formulate a response to a message:

from("activemq:My.Queue") .to("string-template:com/acme/MyResponse.tm");

The Email Sample

In this sample we want to use a string template to send an order confirmation email. The email template is laid out in StringTemplate as:
This example works for camel 2.11.0. If your camel version is less than 2.11.0, the variables should be started and ended with $.

Dear <headers.lastName>, <headers.firstName> Thanks for the order of <headers.item>. Regards Camel Riders Bookstore <body>

And the java code is as follows:{snippet:id=e1|lang=java|url=camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateLetterTest.java}Endpoint See Also

Test Component

Testing of distributed and asynchronous processing is notoriously difficult. The Mock, Test and DataSet endpoints work great with the Camel Testing Framework to simplify your unit and integration testing using Enterprise Integration Patterns and Camel's large range of Components together with the powerful Bean Integration.

The test component extends the Mock component to support pulling messages from another endpoint on startup to set the expected message bodies on the underlying Mock endpoint. That is, you use the test endpoint in a route and messages arriving on it will be implicitly compared to some expected messages extracted from some other location.

So you can use, for example, an expected set of message bodies as files. This will then set up a properly configured Mock endpoint, which is only valid if the received messages match the number of expected messages and their message payloads are equal.

Maven users will need to add the following dependency to their pom.xml for this component when using Camel 2.8 or older:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

From Camel 2.9: the Test component is provided directly in camel-core.

URI format

test:expectedMessagesEndpointUri

Where expectedMessagesEndpointUri refers to some other Component URI that the expected message bodies are pulled from before starting the test.

URI Options

Name

Default Value

Description

anyOrder

false

Camel 2.17: Whether the expected messages should arrive in the same order, or in any order.

delimiter

\n|\r

Camel 2.17: The delimiter to use when split=true. The delimiter can be a regular expression.

split

false

Camel 2.17: If true messages loaded from the test endpoint will be split using the defined delimiter.For example to use a file endpoint to load a file where each line is an expected message. 

timeout

2000

Camel 2.12: The timeout to use when polling for message bodies from the URI.

Example

For example, you could write a test case as follows:

from("seda:someEndpoint")
  .to("test:file://data/expectedOutput?noop=true");

If your test then invokes the MockEndpoint.assertIsSatisfied(camelContext) method, your test case will perform the necessary assertions.

To see how you can set other expectations on the test endpoint, see the Mock component.

Error formatting macro: include: java.lang.NullPointerException

Validation Component

The Validation component performs XML validation of the message body using the JAXP Validation API and based on any of the supported XML schema languages, which defaults to XML Schema

Note that the Jing component also supports the following useful schema languages:

The MSV component also supports RelaxNG XML Syntax.

URI format

validator:someLocalOrRemoteResource

Where someLocalOrRemoteResource is some URL to a local resource on the classpath or a full URL to a remote resource or resource on the file system which contains the XSD to validate against. For example:

Maven users will need to add the following dependency to their pom.xml for this component when using Camel 2.8 or older:

xml<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>

From Camel 2.9 onwards the Validation component is provided directly in the camel-core.

Options

confluenceTableSmall

Option

Default

Description

resourceResolverFactoryDefaultValidatorResourceResolverFactoryCamel 2.17: Reference to a org.apache.camel.component.validator.ValidatorResourceResolverFactory which creates a resource resolver per endpoint. The default implementation creates an instance of org.apache.camel.component.validator.DefaultLSResourceResolver per endpoint which creates the default resource resolver org.apache.camel.component.validator.DefaultLSResourceResolver. The default resource resolver reads the schema files from the classpath and the file system. This option instead of the option resourceResolver shall be used when the resource resolver depends on the resource URI of the root schema document specified in the endpoint; for example, if you want to extend the default resource resolver. This option is also available on the validator component, so that you can set the resource resolver factory only once for all endpoints.

resourceResolver

null

Camel 2.9: Reference to a org.w3c.dom.ls.LSResourceResolver in the Registry.

useDom

false

Whether DOMSource/DOMResult or SaxSource/SaxResult should be used by the validator.

useSharedSchema

true

Camel 2.3: Whether the Schema instance should be shared or not. This option is introduced to work around a JDK 1.6.x bug. Xerces should not have this issue.

failOnNullBody

true

Camel 2.9.5/2.10.3: Whether to fail if no body exists.

headerName

null

Camel 2.11: To validate against a header instead of the message body.

failOnNullHeader

true

Camel 2.11: Whether to fail if no header exists when validating against a header.

Example

The following example shows how to configure a route from endpoint direct:start which then goes to one of two endpoints, either mock:valid or mock:invalid based on whether or not the XML matches the given schema (which is supplied on the classpath).{snippet:id=example|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/validator/camelContext.xml}

Advanced: JMX method clearCachedSchema

Since Camel 2.17, you can force that the cached schema in the validator endpoint is cleared and reread with the next process call with the JMX operation clearCachedSchema. You can also use this method to programmatically clear the cache. This method is available on the ValidatorEndpoint class.

Advanced: Global Option "CamelXmlValidatorAccessExternalDTD"

Since Camel 2.19, 2.18.3, and  2.17.6 the default schema factory no longer allows reading external DTDs and external DTD entities. To achieve the old behavior where it was possible to access external DTDs and DTDs entities you can set the CamelContext global option  "CamelXmlValidatorAccessExternalDTD" to "true". Prior to 2.19 global options where called properties.

Endpoint See Also

Velocity

The velocity: component allows you to process a message using an Apache Velocity template. This can be ideal when using Templating to generate responses for requests.

Maven users will need to add the following dependency to their pom.xml for this component:

xml<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-velocity</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>

URI format

velocity:templateName[?options]

Where templateName is the classpath-local URI of the template to invoke; or the complete URL of the remote template (eg: file://folder/myfile.vm).

You can append query options to the URI in the following format, ?option=value&option=value&...

Options

confluenceTableSmall

Option

Default

Description

loaderCache

true

Velocity based file loader cache.

contentCache

true

Cache for the resource content when it is loaded.
Note : as of Camel 2.9 cached resource content can be cleared via JMX using the endpoint's clearContentCache operation.

encoding

null

Character encoding of the resource content.

propertiesFile

null

New option in Camel 2.1: The URI of the properties file which is used for VelocityEngine initialization.

Message Headers

The velocity component sets a couple headers on the message (you can't set these yourself and from Camel 2.1 velocity component will not set these headers which will cause some side effect on the dynamic template support):

confluenceTableSmall

Header

Description

CamelVelocityResourceUri

The templateName as a String object.

CamelVelocitySupplementalContext

Camel 2.16: To add additional information to the used VelocityContext. The value of this header should be a Map with key/values that will added (override any existing key with the same name).
This can be used to pre setup some common key/values you want to reuse in your velocity endpoints.

Headers set during the Velocity evaluation are returned to the message and added as headers. Then its kinda possible to return values from Velocity to the Message.

For example, to set the header value of fruit in the Velocity template .tm:

$in.setHeader("fruit", "Apple")

The fruit header is now accessible from the message.out.headers.

Velocity Context

Camel will provide exchange information in the Velocity context (just a Map). The Exchange is transfered as:

confluenceTableSmall

key

value

exchange

The Exchange itself.

exchange.properties

The Exchange properties.

headers

The headers of the In message.

camelContext

The Camel Context instance.

request

The In message.

in

The In message.

body

The In message body.

out

The Out message (only for InOut message exchange pattern).

response

The Out message (only for InOut message exchange pattern).

Since Camel-2.14, you can setup a custom Velocity Context yourself by setting the message header CamelVelocityContext just like this

java VelocityContext velocityContext = new VelocityContext(variableMap); exchange.getIn().setHeader("CamelVelocityContext", velocityContext);

 

Hot reloading

The Velocity template resource is, by default, hot reloadable for both file and classpath resources (expanded jar). If you set contentCache=true, Camel will only load the resource once, and thus hot reloading is not possible. This scenario can be used in production, when the resource never changes.

Dynamic templates

Available as of Camel 2.1
Camel provides two headers by which you can define a different resource location for a template or the template content itself. If any of these headers is set then Camel uses this over the endpoint configured resource. This allows you to provide a dynamic template at runtime.

confluenceTableSmall

Header

Type

Description

CamelVelocityResourceUri

String

Camel 2.1: A URI for the template resource to use instead of the endpoint configured.

CamelVelocityTemplate

String

Camel 2.1: The template to use instead of the endpoint configured.

Samples

For example you could use something like

from("activemq:My.Queue"). to("velocity:com/acme/MyResponse.vm");

To use a Velocity template to formulate a response to a message for InOut message exchanges (where there is a JMSReplyTo header).

If you want to use InOnly and consume the message and send it to another destination, you could use the following route:

from("activemq:My.Queue"). to("velocity:com/acme/MyResponse.vm"). to("activemq:Another.Queue");

And to use the content cache, e.g. for use in production, where the .vm template never changes:

from("activemq:My.Queue"). to("velocity:com/acme/MyResponse.vm?contentCache=true"). to("activemq:Another.Queue");

And a file based resource:

from("activemq:My.Queue"). to("velocity:file://myfolder/MyResponse.vm?contentCache=true"). to("activemq:Another.Queue");

In Camel 2.1 it's possible to specify what template the component should use dynamically via a header, so for example:

from("direct:in"). setHeader("CamelVelocityResourceUri").constant("path/to/my/template.vm"). to("velocity:dummy");

In Camel 2.1 it's possible to specify a template directly as a header the component should use dynamically via a header, so for example:

from("direct:in"). setHeader("CamelVelocityTemplate").constant("Hi this is a velocity template that can do templating ${body}"). to("velocity:dummy");

The Email Sample

In this sample we want to use Velocity templating for an order confirmation email. The email template is laid out in Velocity as:

Dear ${headers.lastName}, ${headers.firstName} Thanks for the order of ${headers.item}. Regards Camel Riders Bookstore ${body}

And the java code:{snippet:id=e1|lang=java|url=camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityLetterTest.java}Endpoint See Also

VM Component

The vm: component provides asynchronous SEDA behavior, exchanging messages on a BlockingQueue and invoking consumers in a separate thread pool.

This component differs from the Seda component in that VM supports communication across CamelContext instances - so you can use this mechanism to communicate across web applications (provided that camel-core.jar is on the system/boot classpath).

VM is an extension to the Seda component.

URI format

vm:queueName[?options]

Where queueName can be any string to uniquely identify the endpoint within the JVM (or at least within the classloader that loaded camel-core.jar)

You can append query options to the URI in the following format: ?option=value&option=value&...

Before Camel 2.3 - Same URI must be used for both producer and consumer

An exactly identical VM endpoint URI must be used for both the producer and the consumer endpoint. Otherwise, Camel will create a second VM endpoint despite that the queueName portion of the URI is identical. For example:

from("direct:foo").to("vm:bar?concurrentConsumers=5");

from("vm:bar?concurrentConsumers=5").to("file://output");

Notice that we have to use the full URI, including options in both the producer and consumer.

In Camel 2.4 this has been fixed so that only the queue name must match. Using the queue name bar, we could rewrite the previous exmple as follows:

from("direct:foo").to("vm:bar");

from("vm:bar?concurrentConsumers=5").to("file://output");

Options

See the Seda component for options and other important usage details as the same rules apply to the Vm component.

Samples

In the route below we send exchanges across CamelContext instances to a VM queue named order.email:

from("direct:in").bean(MyOrderBean.class).to("vm:order.email");

And then we receive exchanges in some other Camel context (such as deployed in another .war application):

from("vm:order.email").bean(MyOrderEmailSender.class);

XMPP Component

The xmpp: component implements an XMPP (Jabber) transport.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-xmpp</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

xmpp://[login@]hostname[:port][/participant][?Options]

The component supports both room based and private person-person conversations.
The component supports both producer and consumer (you can get messages from XMPP or send messages to XMPP). Consumer mode supports rooms starting.

You can append query options to the URI in the following format, ?option=value&option=value&...

Options

Name

Description

room

If this option is specified, the component will connect to MUC (Multi User Chat). Usually, the domain name for MUC is different from the login domain. For example, if you are superman@jabber.org and want to join the krypton room, then the room URL is krypton@conference.jabber.org. Note the conference part.
It is not a requirement to provide the full room JID. If the room parameter does not contain the @ symbol, the domain part will be discovered and added by Camel

user

User name (without server name). If not specified, anonymous login will be attempted.

password

Password.

resource

XMPP resource. The default is Camel.

createAccount

If true, an attempt to create an account will be made. Default is false.

participant

JID (Jabber ID) of person to receive messages. room parameter has precedence over participant.

nickname

Use nickname when joining room. If room is specified and nickname is not, user will be used for the nickname.

serviceName

The name of the service you are connecting to. For Google Talk, this would be gmail.com.

testConnectionOnStartup

Camel 2.11 Specifies whether to test the connection on startup. This is used to ensure that the XMPP client has a valid connection to the XMPP server when the route starts. Camel throws an exception on startup if a connection cannot be established. When this option is set to false, Camel will attempt to establish a "lazy" connection when needed by a producer, and will poll for a consumer connection until the connection is established. Default is true.

connectionPollDelay

Camel 2.11 The amount of time in seconds between polls to verify the health of the XMPP connection, or between attempts to establish an initial consumer connection. Camel will try to re-establish a connection if it has become inactive. Default is 10 seconds.

pubsubCamel 2.15 Accept pubsub packets on input, default is false
docCamel 2.15 Set a doc header on the IN message containing a Document form of the incoming packet; default is true if presence or pubsub are true, otherwise false
connectionConfigurationCamel 2.18: To use an existing connection configuration

Headers and setting Subject or Language

Camel sets the message IN headers as properties on the XMPP message. You can configure a HeaderFilterStategy if you need custom filtering of headers.
The Subject and Language of the XMPP message are also set if they are provided as IN headers.

Examples

User superman to join room krypton at jabber server with password, secret:

xmpp://superman@jabber.org/?room=krypton@conference.jabber.org&password=secret

User superman to send messages to joker:

xmpp://superman@jabber.org/joker@jabber.org?password=secret

Routing example in Java:

from("timer://kickoff?period=10000").
setBody(constant("I will win!\n Your Superman.")).
to("xmpp://superman@jabber.org/joker@jabber.org?password=secret");

Consumer configuration, which writes all messages from joker into the queue, evil.talk.

from("xmpp://superman@jabber.org/joker@jabber.org?password=secret").
to("activemq:evil.talk");

Consumer configuration, which listens to room messages:

from("xmpp://superman@jabber.org/?password=secret&room=krypton@conference.jabber.org").
to("activemq:krypton.talk");

Room in short notation (no domain part):

from("xmpp://superman@jabber.org/?password=secret&room=krypton").
to("activemq:krypton.talk");

When connecting to the Google Chat service, you'll need to specify the serviceName as well as your credentials:

from("direct:start").
  to("xmpp://talk.google.com:5222/touser@gmail.com?serviceName=gmail.com&user=fromuser&password=secret").
  to("mock:result");

 

XQuery

The xquery: component allows you to process a message using an XQuery template. This can be ideal when using Templating to generate respopnses for requests.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-saxon</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

xquery:templateName[?options]

Where templateName is the classpath-local URI of the template to invoke; or the complete URL of the remote template.

For example you could use something like this:

from("activemq:My.Queue").
  to("xquery:com/acme/mytransform.xquery");

To use an XQuery template to formulate a response to a message for InOut message exchanges (where there is a JMSReplyTo header).

If you want to use InOnly, consume the message, and send it to another destination, you could use the following route:

from("activemq:My.Queue").
  to("xquery:com/acme/mytransform.xquery").
  to("activemq:Another.Queue");

XSLT

The xslt: component allows you to process a message using an XSLT template. This can be ideal when using Templating to generate respopnses for requests.

URI format

xslt:templateName[?options]

Where templateName is the classpath-local URI of the template to invoke; or the complete URL of the remote template. Refer to the Spring Documentation for more detail of the URI syntax

You can append query options to the URI in the following format, ?option=value&option=value&...

Here are some example URIs

URI

Description

xslt:com/acme/mytransform.xsl

refers to the file com/acme/mytransform.xsl on the classpath

xslt:file:///foo/bar.xsl

refers to the file /foo/bar.xsl

xslt:http://acme.com/cheese/foo.xsl

refers to the remote http resource

Maven users will need to add the following dependency to their pom.xml for this component when using Camel 2.8 or older:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

From Camel 2.9 onwards the XSLT component is provided directly in the camel-core.

Options

Name

Default Value

Description

converter

null

Option to override default XmlConverter. Will lookup for the converter in the Registry. The provided converted must be of type org.apache.camel.converter.jaxp.XmlConverter.

transformerFactory

null

Option to override default TransformerFactory. Will lookup for the transformerFactory in the Registry. The provided transformer factory must be of type javax.xml.transform.TransformerFactory.

transformerFactoryClass

null

Option to override default TransformerFactory. Will create a TransformerFactoryClass instance and set it to the converter.

uriResolverFactory

DefaultXsltUriResolverFactory

Camel 2.17:  Reference to a org.apache.camel.component.xslt.XsltUriResolverFactory which creates an URI resolver per endpoint.The default implementation returns an instance of org.apache.camel.component.xslt.DefaultXsltUriResolverFactory which creates the default URI resolver org.apache.camel.builder.xml.XsltUriResolver per endpoint. The default URI resolver reads XSLT documents from the classpath and the file system. This option instead of the option uriResolver shall be used when the URI resolver depends on the resource URI of the root XSLT document specified in the endpoint; for example, if you want to extend the default URI resolver. This option is also available on the XSLT component, so that you can set the resource resolver factory only once for all endpoints.

uriResolver

null

Camel 2.3: Allows you to use a custom javax.xml.transformation.URIResolver. Camel will by default use its own implementation org.apache.camel.builder.xml.XsltUriResolver which is capable of loading from classpath.

resultHandlerFactory

null

Camel 2.3: Allows you to use a custom org.apache.camel.builder.xml.ResultHandlerFactory which is capable of using custom org.apache.camel.builder.xml.ResultHandler types.

failOnNullBody

true

Camel 2.3: Whether or not to throw an exception if the input body is null.

deleteOutputFile

false

Camel 2.6: If you have output=file then this option dictates whether or not the output file should be deleted when the Exchange is done processing. For example suppose the output file is a temporary file, then it can be a good idea to delete it after use.

output

string

Camel 2.3: Option to specify which output type to use. Possible values are: string, bytes, DOM, file. The first three options are all in memory based, where as file is streamed directly to a java.io.File. For file you must specify the filename in the IN header with the key Exchange.XSLT_FILE_NAME which is also CamelXsltFileName. Also any paths leading to the filename must be created beforehand, otherwise an exception is thrown at runtime.

contentCache

true

Camel 2.6: Cache for the resource content (the stylesheet file) when it is loaded. If set to false Camel will reload the stylesheet file on each message processing. This is good for development.
Note: from Camel 2.9 a cached stylesheet can be forced to reload at runtime via JMX using the clearCachedStylesheet operation.

allowStAX

 

Camel 2.8.3/2.9: Whether to allow using StAX as the javax.xml.transform.Source. The option is default false in Camel 2.11.3/2.12.2 or older. And default true in Camel 2.11.4/2.12.3 onwards.

transformerCacheSize

0

Camel 2.9.3/2.10.1: The number of javax.xml.transform.Transformer object that are cached for reuse to avoid calls to Template.newTransformer().

saxon

false

Camel 2.11: Whether to use Saxon as the transformerFactoryClass. If enabled then the class net.sf.saxon.TransformerFactoryImpl. You would need to add Saxon to the classpath.

saxonExtensionFunctions

null

Camel 2.17: Allows to configure one or more custom net.sf.saxon.lib.ExtensionFunctionDefinition. You would need to add Saxon to the classpath. By setting this option, saxon option will be turned out automatically.

errorListener

 

Camel 2.14: Allows to configure to use a custom javax.xml.transform.ErrorListener. Beware when doing this then the default error listener which captures any errors or fatal errors and store information on the Exchange as properties is not in use. So only use this option for special use-cases.

entityResolver Camel 2.18: To use a custom org.xml.sax.EntityResolver with javax.xml.transform.sax.SAXSource.

Using XSLT endpoints

For example you could use something like

from("activemq:My.Queue").
  to("xslt:com/acme/mytransform.xsl");

To use an XSLT template to formulate a response for a message for InOut message exchanges (where there is a JMSReplyTo header).

If you want to use InOnly and consume the message and send it to another destination you could use the following route:

from("activemq:My.Queue").
  to("xslt:com/acme/mytransform.xsl").
  to("activemq:Another.Queue");

Getting Parameters into the XSLT to work with

By default, all headers are added as parameters which are available in the XSLT.
To do this you will need to declare the parameter so it is then useable.

<setHeader headerName="myParam"><constant>42</constant></setHeader>
<to uri="xslt:MyTransform.xsl"/>

And the XSLT just needs to declare it at the top level for it to be available:

<xsl: ...... >

   <xsl:param name="myParam"/>
  
    <xsl:template ...>

Spring XML versions

To use the above examples in Spring XML you would use something like

  <camelContext xmlns="http://activemq.apache.org/camel/schema/spring">
    <route>
      <from uri="activemq:My.Queue"/>
      <to uri="xslt:org/apache/camel/spring/processor/example.xsl"/>
      <to uri="activemq:Another.Queue"/>
    </route>
  </camelContext>

There is a test case along with its Spring XML if you want a concrete example.

Using xsl:include

Camel 2.2 or older
If you use xsl:include in your XSL files then in Camel 2.2 or older it uses the default javax.xml.transform.URIResolver which means it can only lookup files from file system, and its does that relative from the JVM starting folder.

For example this include:

<xsl:include href="staff_template.xsl"/>

Will lookup the staff_tempkalte.xsl file from the starting folder where the application was started.

Camel 2.3 or newer
Now Camel provides its own implementation of URIResolver which allows Camel to load included files from the classpath and more intelligent than before.

For example this include:

<xsl:include href="staff_template.xsl"/>

Will now be located relative from the starting endpoint, which for example could be:

.to("xslt:org/apache/camel/component/xslt/staff_include_relative.xsl")

Which means Camel will locate the file in the classpath as org/apache/camel/component/xslt/staff_template.xsl.
This allows you to use xsl include and have xsl files located in the same folder such as we do in the example org/apache/camel/component/xslt.

You can use the following two prefixes classpath: or file: to instruct Camel to look either in classpath or file system. If you omit the prefix then Camel uses the prefix from the endpoint configuration. If that neither has one, then classpath is assumed.

You can also refer back in the paths such as

    <xsl:include href="../staff_other_template.xsl"/>

Which then will resolve the xsl file under org/apache/camel/component.

Using xsl:include and default prefix

When using xsl:include such as:

<xsl:include href="staff_template.xsl"/>

Then in Camel 2.10.3 and older, then Camel will use "classpath:" as the default prefix, and load the resource from the classpath. This works for most cases, but if you configure the starting resource to load from file,

.to("xslt:file:etc/xslt/staff_include_relative.xsl")

.. then you would have to prefix all your includes with "file:" as well.

<xsl:include href="file:staff_template.xsl"/>

From Camel 2.10.4 onwards we have made this easier as Camel will use the prefix from the endpoint configuration as the default prefix. So from Camel 2.10.4 onwards you can do:

<xsl:include href="staff_template.xsl"/>

Which will load the staff_template.xsl resource from the file system, as the endpoint was configured with "file:" as prefix.
You can still though explicit configure a prefix, and then mix and match. And have both file and classpath loading. But that would be unusual, as most people either use file or classpath based resources.

Using Saxon extension functions

Since Saxon 9.2, writing extension functions has been supplemented by a new mechanism, referred to as integrated extension functions you can now easily use camel:

 

- Java example:

SimpleRegistry registry = new SimpleRegistry();
registry.put("function1", new MyExtensionFunction1());
registry.put("function2", new MyExtensionFunction2());

CamelContext context = new DefaultCamelContext(registry);
context.addRoutes(new RouteBuilder() {
    @Override
    public void configure() throws Exception {
        from("direct:start")
            .to("xslt:org/apache/camel/component/xslt/extensions/extensions.xslt?saxonExtensionFunctions=#function1,#function2");
    }
});

 

Spring example:

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:extensions"/>
    <to uri="xslt:org/apache/camel/component/xslt/extensions/extensions.xslt?saxonExtensionFunctions=#function1,#function2"/>
  </route>
</camelContext>


<bean id="function1" class="org.apache.camel.component.xslt.extensions.MyExtensionFunction1"/>
<bean id="function2" class="org.apache.camel.component.xslt.extensions.MyExtensionFunction2"/>

 

 

Dynamic stylesheets

To provide a dynamic stylesheet at runtime you can define a dynamic URI. See How to use a dynamic URI in to() for more information.

Available as of Camel 2.9 (removed in 2.11.4, 2.12.3 and 2.13.0)
Camel provides the CamelXsltResourceUri header which you can use to define a stylesheet to use instead of what is configured on the endpoint URI. This allows you to provide a dynamic stylesheet at runtime.

Accessing warnings, errors and fatalErrors from XSLT ErrorListener

Available as of Camel 2.14

From Camel 2.14 onwards, any warning/error or fatalError is stored on the current Exchange as a property with the keys Exchange.XSLT_ERRORExchange.XSLT_FATAL_ERROR, or Exchange.XSLT_WARNING which allows end users to get hold of any errors happening during transformation.

For example in the stylesheet below, we want to terminate if a staff has an empty dob field. And to include a custom error message using xsl:message.

  <xsl:template match="/">
    <html>
      <body>
        <xsl:for-each select="staff/programmer">
          <p>Name: <xsl:value-of select="name"/><br />
            <xsl:if test="dob=''">
              <xsl:message terminate="yes">Error: DOB is an empty string!</xsl:message>
            </xsl:if>
          </p>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>

This information is not available on the Exchange stored as an Exception that contains the message in the getMessage() method on the exception. The exception is stored on the Exchange as a warning with the key Exchange.XSLT_WARNING.

Notes on using XSLT and Java Versions

Here are some observations from Sameer, a Camel user, which he kindly shared with us:

In case anybody faces issues with the XSLT endpoint please review these points.

I was trying to use an xslt endpoint for a simple transformation from one xml to another using a simple xsl. The output xml kept appearing (after the xslt processor in the route) with outermost xml tag with no content within.

No explanations show up in the DEBUG logs. On the TRACE logs however I did find some error/warning indicating that the XMLConverter bean could no be initialized.

After a few hours of cranking my mind, I had to do the following to get it to work (thanks to some posts on the users forum that gave some clue):

1. Use the transformerFactory option in the route ("xslt:my-transformer.xsl?transformerFactory=tFactory") with the tFactory bean having bean defined in the spring context for class="org.apache.xalan.xsltc.trax.TransformerFactoryImpl".
2. Added the Xalan jar into my maven pom.

My guess is that the default xml parsing mechanism supplied within the JDK (I am using 1.6.0_03) does not work right in this context and does not throw up any error either. When I switched to Xalan this way it works. This is not a Camel issue, but might need a mention on the xslt component page.

Another note, jdk 1.6.0_03 ships with JAXB 2.0 while Camel needs 2.1. One workaround is to add the 2.1 jar to the jre/lib/endorsed directory for the jvm or as specified by the container.

Hope this post saves newbie Camel riders some time.

  • No labels