Component AppendixThere now follows the documentation on each Camel component. ActiveMQ ComponentThe ActiveMQ component allows messages to be sent to a JMS Queue or Topic or messages to be consumed from a JMS Queue or Topic using Apache ActiveMQ. This component is based on JMS Component and uses Spring's JMS support for declarative transactions, using Spring's JmsTemplate for sending and a MessageListenerContainer for consuming. All the options from the JMS component also applies for this component. To use this component make sure you have the activemq.jar or activemq-core.jar on your classpath along with any Camel dependencies such as camel-core.jar, camel-spring.jar and camel-jms.jar.
URI formatactivemq:[queue:|topic:]destinationName Where destinationName is an ActiveMQ queue or topic name. By default, the destinationName is interpreted as a queue name. For example, to connect to the queue, FOO.BAR, use: activemq:FOO.BAR You can include the optional queue: prefix, if you prefer: activemq:queue:FOO.BAR To connect to a topic, you must include the topic: prefix. For example, to connect to the topic, Stocks.Prices, use: activemq:topic:Stocks.Prices OptionsSee Options on the JMS component as all these options also apply for this component. Configuring the Connection FactoryThis test case shows how to add an ActiveMQComponent to the CamelContext using the activeMQComponent() method while specifying the brokerURL used to connect to ActiveMQ. camelContext.addComponent("activemq", activeMQComponent("vm://localhost?broker.persistent=false")); Configuring the Connection Factory using Spring XMLYou can configure the ActiveMQ broker URL on the ActiveMQComponent as follows <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <camelContext xmlns="http://camel.apache.org/schema/spring"> </camelContext> <bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent"> <property name="brokerURL" value="tcp://somehost:61616"/> </bean> </beans> Using connection poolingWhen sending to an ActiveMQ broker using Camel it's recommended to use a pooled connection factory to efficiently handle pooling of JMS connections, sessions and producers. This is documented on the ActiveMQ Spring Support page. You can grab ActiveMQ's org.apache.activemq.pool.PooledConnectionFactory with Maven: <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.6.0</version> </dependency> And then setup the activemq Camel component as follows: <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> </bean> <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" init-method="start" destroy-method="stop"> <property name="maxConnections" value="8" /> <property name="connectionFactory" ref="jmsConnectionFactory" /> </bean> <bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration"> <property name="connectionFactory" ref="pooledConnectionFactory"/> <property name="concurrentConsumers" value="10"/> </bean> <bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent"> <property name="configuration" ref="jmsConfig"/> </bean>
The PooledConnectionFactory will then create a connection pool with up to 8 connections in use at the same time. Each connection can be shared by many sessions. There is an option named maxActive you can use to configure the maximum number of sessions per connection; the default value is 500. From ActiveMQ 5.7 onwards the option has been renamed to better reflect its purpose, being named as maxActiveSessionPerConnection. Notice the concurrentConsumers is set to a higher value than maxConnections is. This is okay, as each consumer is using a session, and as a session can share the same connection, we are in the safe. In this example we can have 8 * 500 = 4000 active sessions at the same time. Invoking MessageListener POJOs in a Camel routeThe ActiveMQ component also provides a helper Type Converter from a JMS MessageListener to a Processor. This means that the Bean component is capable of invoking any JMS MessageListener bean directly inside any route. So for example you can create a MessageListener in JMS like this: public class MyListener implements MessageListener { public void onMessage(Message jmsMessage) { // ... } } Then use it in your Camel route as follows
from("file://foo/bar").
bean(MyListener.class);
That is, you can reuse any of the Camel Components and easily integrate them into your JMS MessageListener POJO! Using ActiveMQ Destination OptionsAvailable as of ActiveMQ 5.6 You can configure the Destination Options in the endpoint uri, using the "destination." prefix. For example to mark a consumer as exclusive, and set its prefetch size to 50, you can do as follows: <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="file://src/test/data?noop=true"/> <to uri="activemq:queue:foo"/> </route> <route> <!-- use consumer.exclusive ActiveMQ destination option, notice we have to prefix with destination. --> <from uri="activemq:foo?destination.consumer.exclusive=true&destination.consumer.prefetchSize=50"/> <to uri="mock:results"/> </route> </camelContext> Consuming Advisory MessagesActiveMQ can generate Advisory messages which are put in topics that you can consume. Such messages can help you send alerts in case you detect slow consumers or to build statistics (number of messages/produced per day, etc.) The following Spring DSL example shows you how to read messages from a topic. The below route starts by reading the topic ActiveMQ.Advisory.Connection. To watch another topic, simply change the name according to the name provided in ActiveMQ Advisory Messages documentation. The parameter mapJmsMessage=false allows for converting the org.apache.activemq.command.ActiveMqMessage object from the jms queue. Next, the body received is converted into a String for the purposes of this example and a carriage return is added. Finally, the string is added to a file <route> <from uri="activemq:topic:ActiveMQ.Advisory.Connection?mapJmsMessage=false" /> <convertBodyTo type="java.lang.String"/> <transform> <simple>${in.body} </simple> </transform> <to uri="file://data/activemq/?fileExist=Append&fileName=advisoryConnection-${date:now:yyyyMMdd}.txt" /> </route> If you consume a message on a queue, you should see the following files under the data/activemq folder : advisoryConnection-20100312.txt and containing string:
ActiveMQMessage {commandId = 0, responseRequired = false,
messageId = ID:dell-charles-3258-1268399815140
-1:0:0:0:221, originalDestination = null, originalTransactionId = null,
producerId = ID:dell-charles-3258-1268399815140-1:0:0:0,
destination = topic://ActiveMQ.Advisory.Connection, transactionId = null,
expiration = 0, timestamp = 0, arrival = 0, brokerInTime = 1268403383468,
brokerOutTime = 1268403383468, correlationId = null, replyTo = null,
persistent = false, type = Advisory, priority = 0, groupID = null, groupSequence = 0,
targetConsumerId = null, compressed = false, userID = null, content = null,
marshalledProperties = org.apache.activemq.util.ByteSequence@17e2705,
dataStructure = ConnectionInfo {commandId = 1, responseRequired = true,
connectionId = ID:dell-charles-3258-1268399815140-2:50,
clientId = ID:dell-charles-3258-1268399815140-14:0, userName = , password = *****,
brokerPath = null, brokerMasterConnector = false, manageable = true,
clientMaster = true}, redeliveryCounter = 0, size = 0, properties =
{originBrokerName=master, originBrokerId=ID:dell-charles-3258-1268399815140-0:0,
originBrokerURL=vm://master}, readOnlyProperties = true, readOnlyBody = true,
droppable = false}
Getting Component JARYou will need this dependency
ActiveMQ is an extension of the JMS component released with the ActiveMQ project. <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-camel</artifactId> <version>5.6.0</version> </dependency> See AlsoActiveMQ Journal ComponentThe ActiveMQ Journal Component allows messages to be stored in a rolling log file and then consumed from that log file. The journal aggregates and batches up concurrent writes so that the overhead of writing and waiting for the disk sync is relatively constant regardless of how many concurrent writes are being done. Therefore, this component supports and encourages you to use multiple concurrent producers to the same journal endpoint. Each journal endpoint uses a different log file and therefore write batching (and the associated performance boost) does not occur between multiple endpoints. This component only supports one active consumer on the endpoint. After the message is processed by the consumer's processor, the log file is marked and only subsequent messages in the log file will get delivered to consumers. URI formatactivemq.journal:directoryName[?options] So for example, to send to the journal located in the /tmp/data directory you would use the following URI: activemq.journal:/tmp/data Options
You can append query options to the URI in the following format, ?option=value&option=value&... Expected Exchange Data TypesThe consumer of a Journal endpoint generates DefaultExchange objects with the in message :
The producer to a Journal endpoint expects an Exchange with an In message where the body can be converted to a ByteSequence or a byte[]. See AlsoAMQPThe amqp: component supports the AMQP protocol using the Client API of the Qpid project. 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 formatamqp:[queue:|topic:]destinationName[?options] You can specify all of the various configuration options of the JMS component after the destination name. See AlsoSQS ComponentAvailable as of Camel 2.6 The sqs component supports sending and receiving messages to Amazon's SQS service.
URI Format
aws-sqs://queue-name[?options]
The queue will be created if they don't already exists. URI Options
Batch ConsumerThis component implements the Batch Consumer. This allows you for instance to know how many messages exists in this batch and for instance let the Aggregator aggregate this number of messages. UsageMessage headers set by the SQS producer
Message headers set by the SQS consumer
Advanced AmazonSQS configurationIf your Camel Application is running behind a firewall or if you need to have more control over the AmazonSQS instance configuration, you can create your own instance: AWSCredentials awsCredentials = new BasicAWSCredentials("myAccessKey", "mySecretKey"); ClientConfiguration clientConfiguration = new ClientConfiguration(); clientConfiguration.setProxyHost("http://myProxyHost"); clientConfiguration.setProxyPort(8080); AmazonSQS client = new AmazonSQSClient(awsCredentials, clientConfiguration); registry.bind("client", client); and refer to it in your Camel aws-sqs component configuration: from("aws-sqs://MyQueue?amazonSQSClient=#client&delay=5000&maxMessagesPerPoll=5") .to("mock:result"); DependenciesMaven users will need to add the following dependency to their pom.xml. pom.xml <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-aws</artifactId> <version>${camel-version}</version> </dependency> where ${camel-version} must be replaced by the actual version of Camel (2.6 or higher). See AlsoAtom ComponentThe atom: component is used for polling Atom feeds. Camel will poll the feed every 60 seconds by default. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-atom</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI format
atom://atomUri[?options]
Where atomUri is the URI to the Atom feed to poll. Options
You can append query options to the URI in the following format, ?option=value&option=value&... Exchange data formatCamel will set the In body on the returned Exchange with the entries. Depending on the splitEntries flag Camel will either return one Entry or a List<Entry>.
Camel can set the Feed object on the In header (see feedHeader option to disable this): Message HeadersCamel atom uses these headers.
SamplesIn this sample we poll James Strachan's blog.
from("atom://http://macstrac.blogspot.com/feeds/posts/default").to("seda:feeds");
In this sample we want to filter only good blogs we like to a SEDA queue. The sample also shows how to setup Camel standalone, not running in any Container or using Spring. // This is the CamelContext that is the heart of Camel private CamelContext context; protected CamelContext createCamelContext() throws Exception { // First we register a blog service in our bean registry SimpleRegistry registry = new SimpleRegistry(); registry.put("blogService", new BlogService()); // Then we create the camel context with our bean registry context = new DefaultCamelContext(registry); // Then we add all the routes we need using the route builder DSL syntax context.addRoutes(createMyRoutes()); return context; } /** * This is the route builder where we create our routes using the Camel DSL */ protected RouteBuilder createMyRoutes() throws Exception { return new RouteBuilder() { public void configure() throws Exception { // We pool the atom feeds from the source for further processing in the seda queue // we set the delay to 1 second for each pool as this is a unit test also and we can // not wait the default poll interval of 60 seconds. // Using splitEntries=true will during polling only fetch one Atom Entry at any given time. // As the feed.atom file contains 7 entries, using this will require 7 polls to fetch the entire // content. When Camel have reach the end of entries it will refresh the atom feed from URI source // and restart - but as Camel by default uses the UpdatedDateFilter it will only deliver new // blog entries to "seda:feeds". So only when James Straham updates his blog with a new entry // Camel will create an exchange for the seda:feeds. from("atom:file:src/test/data/feed.atom?splitEntries=true&consumer.delay=1000").to("seda:feeds"); // From the feeds we filter each blot entry by using our blog service class from("seda:feeds").filter().method("blogService", "isGoodBlog").to("seda:goodBlogs"); // And the good blogs is moved to a mock queue as this sample is also used for unit testing // this is one of the strengths in Camel that you can also use the mock endpoint for your // unit tests from("seda:goodBlogs").to("mock:result"); } }; } /** * This is the actual junit test method that does the assertion that our routes is working as expected */ @Test public void testFiltering() throws Exception { // create and start Camel context = createCamelContext(); context.start(); // Get the mock endpoint MockEndpoint mock = context.getEndpoint("mock:result", MockEndpoint.class); // There should be at least two good blog entries from the feed mock.expectedMinimumMessageCount(2); // Asserts that the above expectations is true, will throw assertions exception if it failed // Camel will default wait max 20 seconds for the assertions to be true, if the conditions // is true sooner Camel will continue mock.assertIsSatisfied(); // stop Camel after use context.stop(); } /** * Services for blogs */ public class BlogService { /** * Tests the blogs if its a good blog entry or not */ public boolean isGoodBlog(Exchange exchange) { Entry entry = exchange.getIn().getBody(Entry.class); String title = entry.getTitle(); // We like blogs about Camel boolean good = title.toLowerCase().contains("camel"); return good; } } See AlsoBean ComponentThe bean: component binds beans to Camel message exchanges. URI formatbean:beanID[?options] Where beanID can be any string which is used to look up the bean in the Registry Options
You can append query options to the URI in the following format, ?option=value&option=value&... UsingThe 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); Once an endpoint has been registered, you can build Camel routes that use it to process exchanges. // lets add simple route 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 = ProxyHelper.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 endpointCamel 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. 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 syntaxJava 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 BindingHow 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. See Also
Bean Validation ComponentAvailable as of Camel 2.3 The Validation component performs bean validation of the message body using the Java Bean Validation API (JSR 303). Camel uses the reference implementation, which is Hibernate Validator. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-bean-validator</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI formatbean-validator:something[?options] or
bean-validator://something[?options]
Where something must be present to provide a valid url URI Options
ServiceMix4/OSGi Deployment.The bean-validator when deployed in an OSGi environment requires a little help to accommodate the resource loading specified in JSR303, this was fixed in Servicemix-Specs 1.6-SNAPSHOT. ExampleAssumed we have a java bean with the following annotations Car.java public class Car { @NotNull private String manufacturer; @NotNull @Size(min = 5, max = 14, groups = OptionalChecks.class) private String licensePlate; // getter and setter } and an interface definition for our custom validation group OptionalChecks.java public interface OptionalChecks { } with the following Camel route, only the @NotNull constraints on the attributes manufacturer and licensePlate will be validated (Camel uses the default group javax.validation.groups.Default). from("direct:start") .to("bean-validator://x") .to("mock:end") If you want to check the constraints from the group OptionalChecks, you have to define the route like this from("direct:start") .to("bean-validator://x?group=OptionalChecks") .to("mock:end") If you want to check the constraints from both groups, you have to define a new interface first AllChecks.java
@GroupSequence({Default.class, OptionalChecks.class})
public interface AllChecks {
}
and then your route definition should looks like this from("direct:start") .to("bean-validator://x?group=AllChecks") .to("mock:end") And if you have to provide your own message interpolator, traversable resolver and constraint validator factory, you have to write a route like this <bean id="myMessageInterpolator" class="my.ConstraintValidatorFactory" /> <bean id="myTraversableResolver" class="my.TraversableResolver" /> <bean id="myConstraintValidatorFactory" class="my.ConstraintValidatorFactory" /> from("direct:start") .to("bean-validator://x?group=AllChecks&messageInterpolator=#myMessageInterpolator &traversableResolver=#myTraversableResolver&constraintValidatorFactory=#myConstraintValidatorFactory") .to("mock:end") It's also possible to describe your constraints as XML and not as Java annotations. In this case, you have to provide the file META-INF/validation.xml which could looks like this validation.xml <?xml version="1.0" encoding="UTF-8"?> <validation-config xmlns="http://jboss.org/xml/ns/javax/validation/configuration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration"> <default-provider>org.hibernate.validator.HibernateValidator</default-provider> <message-interpolator>org.hibernate.validator.engine.ResourceBundleMessageInterpolator</message-interpolator> <traversable-resolver>org.hibernate.validator.engine.resolver.DefaultTraversableResolver</traversable-resolver> <constraint-validator-factory>org.hibernate.validator.engine.ConstraintValidatorFactoryImpl</constraint-validator-factory> <constraint-mapping>/constraints-car.xml</constraint-mapping> </validation-config> and the constraints-car.xml file constraints-car.xml <?xml version="1.0" encoding="UTF-8"?> <constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd" xmlns="http://jboss.org/xml/ns/javax/validation/mapping"> <default-package>org.apache.camel.component.bean.validator</default-package> <bean class="CarWithoutAnnotations" ignore-annotations="true"> <field name="manufacturer"> <constraint annotation="javax.validation.constraints.NotNull" /> </field> <field name="licensePlate"> <constraint annotation="javax.validation.constraints.NotNull" /> <constraint annotation="javax.validation.constraints.Size"> <groups> <value>org.apache.camel.component.bean.validator.OptionalChecks</value> </groups> <element name="min">5</element> <element name="max">14</element> </constraint> </field> </bean> </constraint-mappings> See AlsoBrowse ComponentThe Browse 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 formatbrowse:someName Where someName can be any string to uniquely identify the endpoint. SampleIn the route below, we insert a browse: component to be able to browse the Exchanges that are passing through: from("activemq:order.in").to("browse:orderReceived").to("bean:processOrder"); We can now inspect the received exchanges from within the Java code:
private CamelContext context;
public void inspectRecievedOrders() {
BrowsableEndpoint browse = context.getEndpoint("browse: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();
...
}
}
See AlsoCache ComponentAvailable as of Camel 2.1 The cache component enables you to perform caching operations using EHCache as the Cache Implementation. The cache itself is created on demand or if a cache of that name already exists then it is simply utilized with its original settings. This component supports producer and event based consumer endpoints. The Cache consumer is an event based consumer and can be used to listen and respond to specific cache activities. If you need to perform selections from a pre-existing cache, use the processors defined for the cache component. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-cache</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI format
cache://cacheName[?options]
You can append query options to the URI in the following format, ?option=value&option=#beanRef&... Options
Sending/Receiving Messages to/from the cacheMessage Headers up to Camel 2.7
Message Headers Camel 2.8+
The CamelCacheAdd and CamelCacheUpdate operations support additional headers:
Cache ProducerSending data to the cache involves the ability to direct payloads in exchanges to be stored in a pre-existing or created-on-demand cache. The mechanics of doing this involve
Cache ConsumerReceiving data from the cache involves the ability of the CacheConsumer to listen on a pre-existing or created-on-demand Cache using an event Listener and receive automatic notifications when any cache activity take place (i.e CamelCacheGet/CamelCacheUpdate/CamelCacheDelete/CamelCacheDeleteAll). Upon such an activity taking place
Cache ProcessorsThere are a set of nice processors with the ability to perform cache lookups and selectively replace payload content at the
Cache Usage SamplesExample 1: Configuring the cachefrom("cache://MyApplicationCache" + "?maxElementsInMemory=1000" + "&memoryStoreEvictionPolicy=" + "MemoryStoreEvictionPolicy.LFU" + "&overflowToDisk=true" + "&eternal=true" + "&timeToLiveSeconds=300" + "&timeToIdleSeconds=true" + "&diskPersistent=true" + "&diskExpiryThreadIntervalSeconds=300") Example 2: Adding keys to the cacheRouteBuilder builder = new RouteBuilder() { public void configure() { from("direct:start") .setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD)) .setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")) .to("cache://TestCache1") } }; Example 2: Updating existing keys in a cacheRouteBuilder builder = new RouteBuilder() { public void configure() { from("direct:start") .setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_UPDATE)) .setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")) .to("cache://TestCache1") } }; Example 3: Deleting existing keys in a cacheRouteBuilder builder = new RouteBuilder() { public void configure() { from("direct:start") .setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_DELETE)) .setHeader(CacheConstants.CACHE_KEY", constant("Ralph_Waldo_Emerson")) .to("cache://TestCache1") } }; Example 4: Deleting all existing keys in a cacheRouteBuilder builder = new RouteBuilder() { public void configure() { from("direct:start") .setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_DELETEALL)) .to("cache://TestCache1"); } }; Example 5: Notifying any changes registering in a Cache to Processors and other ProducersRouteBuilder builder = new RouteBuilder() { public void configure() { from("cache://TestCache1") .process(new Processor() { public void process(Exchange exchange) throws Exception { String operation = (String) exchange.getIn().getHeader(CacheConstants.CACHE_OPERATION); String key = (String) exchange.getIn().getHeader(CacheConstants.CACHE_KEY); Object body = exchange.getIn().getBody(); // Do something } }) } }; Example 6: Using Processors to selectively replace payload with cache valuesRouteBuilder builder = new RouteBuilder() { public void configure() { //Message Body Replacer from("cache://TestCache1") .filter(header(CacheConstants.CACHE_KEY).isEqualTo("greeting")) .process(new CacheBasedMessageBodyReplacer("cache://TestCache1","farewell")) .to("direct:next"); //Message Token replacer from("cache://TestCache1") .filter(header(CacheConstants.CACHE_KEY).isEqualTo("quote")) .process(new CacheBasedTokenReplacer("cache://TestCache1","novel","#novel#")) .process(new CacheBasedTokenReplacer("cache://TestCache1","author","#author#")) .process(new CacheBasedTokenReplacer("cache://TestCache1","number","#number#")) .to("direct:next"); //Message XPath replacer from("cache://TestCache1"). .filter(header(CacheConstants.CACHE_KEY).isEqualTo("XML_FRAGMENT")) .process(new CacheBasedXPathReplacer("cache://TestCache1","book1","/books/book1")) .process (new CacheBasedXPathReplacer("cache://TestCache1","book2","/books/book2")) .to("direct:next"); } }; Example 7: Getting an entry from the Cachefrom("direct:start") // Prepare headers .setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_GET)) .setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")). .to("cache://TestCache1"). // Check if entry was not found .choice().when(header(CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNull()). // If not found, get the payload and put it to cache .to("cxf:bean:someHeavyweightOperation"). .setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD)) .setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")) .to("cache://TestCache1") .end() .to("direct:nextPhase"); Example 8: Checking for an entry in the CacheNote: The CHECK command tests existence of an entry in the cache but doesn't place a message in the body. from("direct:start") // Prepare headers .setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_CHECK)) .setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")). .to("cache://TestCache1"). // Check if entry was not found .choice().when(header(CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNull()). // If not found, get the payload and put it to cache .to("cxf:bean:someHeavyweightOperation"). .setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD)) .setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")) .to("cache://TestCache1") .end(); Management of EHCacheEHCache has its own statistics and management from JMX. Here's a snippet on how to expose them via JMX in a Spring application context: <bean id="ehCacheManagementService" class="net.sf.ehcache.management.ManagementService" init-method="init" lazy-init="false"> <constructor-arg> <bean class="net.sf.ehcache.CacheManager" factory-method="getInstance"/> </constructor-arg> <constructor-arg> <bean class="org.springframework.jmx.support.JmxUtils" factory-method="locateMBeanServer"/> </constructor-arg> <constructor-arg value="true"/> <constructor-arg value="true"/> <constructor-arg value="true"/> <constructor-arg value="true"/> </bean> Of course you can do the same thing in straight Java: ManagementService.registerMBeans(CacheManager.getInstance(), mbeanServer, true, true, true, true); You can get cache hits, misses, in-memory hits, disk hits, size stats this way. You can also change CacheConfiguration parameters on the fly. Cache replication Camel 2.8+The Camel Cache component is able to distribute a cache across server nodes using several different replication mechanisms including: RMI, JGroups, JMS and Cache Server. There are two different ways to make it work: 1. You can configure ehcache.xml manually OR 2. You can configure these three options:
Configuring Camel Cache replication using the first option is a bit of hard work as you have to configure all caches separately. So in a situation when the all names of caches are not known, using ehcache.xml is not a good idea. The second option is much better when you want to use many different caches as you do not need to define options per cache. This is because replication options are set per CacheManager and per CacheEndpoint. Also it is the only way when cache names are not know at the development phase.
Example: JMS cache replicationJMS replication is the most powerful and secured replication method. Used together with Camel Cache replication makes it also rather simple. Class ComponentAvailable as of Camel 2.4 The class: component binds beans to Camel message exchanges. It works in the same way as the Bean component but instead of looking up beans from a Registry it creates the bean based on the class name. URI formatclass:className[?options] Where className is the fully qualified class name to create and use as bean. Options
You can append query options to the URI in the following format, ?option=value&option=value&... UsingYou simply use the class component just as the Bean component but by specifying the fully qualified classname instead.
from("direct:start").to("class:org.apache.camel.component.bean.MyFooBean").to("mock:result");
You can also specify which method to invoke on the MyFooBean, for example hello:
from("direct:start").to("class:org.apache.camel.component.bean.MyFooBean?method=hello").to("mock:result");
Setting properties on the created instanceIn the endpoint uri you can specify properties to set on the created instance, for example if it has a setPrefix method:
from("direct:start")
.to("class:org.apache.camel.component.bean.MyPrefixBean?prefix=Bye")
.to("mock:result");
And you can also use the # syntax to refer to properties to be looked up in the Registry.
from("direct:start")
.to("class:org.apache.camel.component.bean.MyPrefixBean?cool=#foo")
.to("mock:result");
Which will lookup a bean from the Registry with the id foo and invoke the setCool method on the created instance of the MyPrefixBean class.
See AlsoCometd ComponentThe cometd: component is a transport for working with the jetty implementation of the cometd/bayeux protocol. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-cometd</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI format
cometd://host:port/channelName[?options]
The channelName represents a topic that can be subscribed to by the Camel endpoints. Examplescometd://localhost:8080/service/mychannel cometds://localhost:8443/service/mychannel where cometds: represents an SSL configured endpoint. See this blog entry by David Greco who contributed this component to Apache Camel, for a full sample. Options
You can append query options to the URI in the following format, ?option=value&option=value&... Here is some examples on How to pass the parameters For file (for webapp resources located in the Web Application directory --> cometd://localhost:8080?resourceBase=file./webapp AuthenticationAvailable as of Camel 2.8 You can configure custom SecurityPolicy and Extension's to the CometdComponent which allows you to use authentication as documented here Setting up SSL for Cometd ComponentUsing the JSSE Configuration UtilityAs of Camel 2.9, the Cometd 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 Cometd component. Programmatic configuration of the componentKeyStoreParameters ksp = new KeyStoreParameters(); ksp.setResource("/users/home/server/keystore.jks"); ksp.setPassword("keystorePassword"); KeyManagersParameters kmp = new KeyManagersParameters(); kmp.setKeyStore(ksp); kmp.setKeyPassword("keyPassword"); TrustManagersParameters tmp = new TrustManagersParameters(); tmp.setKeyStore(ksp); SSLContextParameters scp = new SSLContextParameters(); scp.setKeyManagers(kmp); scp.setTrustManagers(tmp); CometdComponent commetdComponent = getContext().getComponent("cometds", CometdComponent.class); commetdComponent.setSslContextParameters(scp); Spring DSL based configuration of endpoint
...
<camel:sslContextParameters
id="sslContextParameters">
<camel:keyManagers
keyPassword="keyPassword">
<camel:keyStore
resource="/users/home/server/keystore.jks"
password="keystorePassword"/>
</camel:keyManagers>
<camel:trustManagers>
<camel:keyStore
resource="/users/home/server/keystore.jks"
password="keystorePassword"/>
</camel:keyManagers>
</camel:sslContextParameters>...
...
<to uri="cometds://127.0.0.1:443/service/test?baseResource=file:./target/test-classes/webapp&timeout=240000&interval=0&maxInterval=30000&multiFrameInterval=1500&jsonCommented=true&logLevel=2&sslContextParameters=#sslContextParameters"/>...
See AlsoContext ComponentAvailable as of Camel 2.7 The context component allows you to create new Camel Components from a CamelContext with a number of routes which is then treated as a black box, allowing you to refer to the local endpoints within the component from other CamelContexts. It is similar to the Routebox component in idea, though the Context component tries to be really simple for end users; just a simple convention over configuration approach to refer to local endpoints inside the CamelContext Component. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-context</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI formatcontext:camelContextId:localEndpointName[?options] Or you can omit the "context:" prefix. camelContextId:localEndpointName[?options]
You can append query options to the URI in the following format, ?option=value&option=value&... ExampleIn this example we'll create a black box context, then we'll use it from another CamelContext. Defining the context componentFirst you need to create a CamelContext, add some routes in it, start it and then register the CamelContext into the Registry (JNDI, Spring, Guice or OSGi etc). This can be done in the usual Camel way from this test case (see the createRegistry() method); this example shows Java and JNDI being used... // lets create our black box as a camel context and a set of routes DefaultCamelContext blackBox = new DefaultCamelContext(registry); blackBox.setName("blackBox"); blackBox.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { // receive purchase orders, lets process it in some way then send an invoice // to our invoice endpoint from("direct:purchaseOrder"). setHeader("received").constant("true"). to("direct:invoice"); } }); blackBox.start(); registry.bind("accounts", blackBox); Notice in the above route we are using pure local endpoints (direct and seda). Also note we expose this CamelContext using the accounts ID. We can do the same thing in Spring via <camelContext id="accounts" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:purchaseOrder"/> ... <to uri="direct:invoice"/> </route> </camelContext> Using the context componentThen in another CamelContext we can then refer to this "accounts black box" by just sending to accounts:purchaseOrder and consuming from accounts:invoice. If you prefer to be more verbose and explicit you could use context:accounts:purchaseOrder or even context:accounts:direct://purchaseOrder if you prefer. But using logical endpoint URIs is preferred as it hides the implementation detail and provides a simple logical naming scheme. For example if we wish to then expose this accounts black box on some middleware (outside of the black box) we can do things like... <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <!-- consume from an ActiveMQ into the black box --> <from uri="activemq:Accounts.PurchaseOrders"/> <to uri="accounts:purchaseOrders"/> </route> <route> <!-- lets send invoices from the black box to a different ActiveMQ Queue --> <from uri="accounts:invoice"/> <to uri="activemq:UK.Accounts.Invoices"/> </route> </camelContext> Naming endpointsA context component instance can have many public input and output endpoints that can be accessed from outside it's CamelContext. When there are many it is recommended that you use logical names for them to hide the middleware as shown above. However when there is only one input, output or error/dead letter endpoint in a component we recommend using the common posix shell names in, out and err Crypto component for Digital SignaturesAvailable as of Camel 2.3 With Camel cryptographic endpoints and Java's Cryptographic extension it is easy to create Digital Signatures for Exchanges. Camel provides a pair of flexible endpoints which get used in concert to create a signature for an exchange in one part of the exchange's workflow and then verify the signature in a later part of the workflow. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-crypto</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> IntroductionDigital signatures make use of Asymmetric Cryptographic techniques to sign messages. From a (very) high level, the algorithms use pairs of complimentary keys with the special property that data encrypted with one key can only be decrypted with the other. One, the private key, is closely guarded and used to 'sign' the message while the other, public key, is shared around to anyone interested in verifying the signed messages. Messages are signed by using the private key to encrypting a digest of the message. This encrypted digest is transmitted along with the message. On the other side the verifier recalculates the message digest and uses the public key to decrypt the the digest in the signature. If both digests match the verifier knows only the holder of the private key could have created the signature. Camel uses the Signature service from the Java Cryptographic Extension to do all the heavy cryptographic lifting required to create exchange signatures. The following are some excellent resources for explaining the mechanics of Cryptography, Message digests and Digital Signatures and how to leverage them with the JCE.
URI formatAs mentioned Camel provides a pair of crypto endpoints to create and verify signatures crypto:sign:name[?options] crypto:verify:name[?options]
In order to correctly function, the sign and verify process needs a pair of keys to be shared, signing requiring a PrivateKey and verifying a PublicKey (or a Certificate containing one). Using the JCE it is very simple to generate these key pairs but it is usually most secure to use a KeyStore to house and share your keys. The DSL is very flexible about how keys are supplied and provides a number of mechanisms. Note a crypto:sign endpoint is typically defined in one route and the complimentary crypto:verify in another, though for simplicity in the examples they appear one after the other. It goes without saying that both signing and verifying should be configured identically. Options
Using1) Raw keysThe most basic way to way to sign and verify an exchange is with a KeyPair as follows. from("direct:keypair").to("crypto:sign://basic?privateKey=#myPrivateKey", "crypto:verify://basic?publicKey=#myPublicKey", "mock:result"); The same can be achieved with the Spring XML Extensions using references to keys <route> <from uri="direct:keypair"/> <to uri="crypto:sign://basic?privateKey=#myPrivateKey" /> <to uri="crypto:verify://basic?publicKey=#myPublicKey" /> <to uri="mock:result"/> </route> 2) KeyStores and Aliases.The JCE provides a very versatile keystore concept for housing pairs of private keys and certificates, keeping them encrypted and password protected. They can be retrieved by applying an alias to the retrieval APIs. There are a number of ways to get keys and Certificates into a keystore, most often this is done with the external 'keytool' application. This is a good example of using keytool to create a KeyStore with a self signed Cert and Private key. The examples use a Keystore with a key and cert aliased by 'bob'. The password for the keystore and the key is 'letmein' The following shows how to use a Keystore via the Fluent builders, it also shows how to load and initialize the keystore. from("direct:keystore").to("crypto:sign://keystore?keystore=#keystore&alias=bob&password=letmein", "crypto:verify://keystore?keystore=#keystore&alias=bob", "mock:result"); Again in Spring a ref is used to lookup an actual keystore instance. <route> <from uri="direct:keystore"/> <to uri="crypto:sign://keystore?keystore=#keystore&alias=bob&password=letmein" /> <to uri="crypto:verify://keystore?keystore=#keystore&alias=bob" /> <to uri="mock:result"/> </route> 3) Changing JCE Provider and AlgorithmChanging the Signature algorithm or the Security provider is a simple matter of specifying their names. You will need to also use Keys that are compatible with the algorithm you choose. KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(512, new SecureRandom()); keyPair = keyGen.generateKeyPair(); PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); // we can set the keys explicitly on the endpoint instances. context.getEndpoint("crypto:sign://rsa?algorithm=MD5withRSA", DigitalSignatureEndpoint.class).setPrivateKey(privateKey); context.getEndpoint("crypto:verify://rsa?algorithm=MD5withRSA", DigitalSignatureEndpoint.class).setPublicKey(publicKey); from("direct:algorithm").to("crypto:sign://rsa?algorithm=MD5withRSA", "crypto:verify://rsa?algorithm=MD5withRSA", "mock:result"); from("direct:provider").to("crypto:sign://provider?privateKey=#myPrivateKey&provider=SUN", "crypto:verify://provider?publicKey=#myPublicKey&provider=SUN", "mock:result"); or <route> <from uri="direct:algorithm"/> <to uri="crypto:sign://rsa?algorithm=MD5withRSA&privateKey=#rsaPrivateKey" /> <to uri="crypto:verify://rsa?algorithm=MD5withRSA&publicKey=#rsaPublicKey" /> <to uri="mock:result"/> </route> <route> <from uri="direct:provider"/> <to uri="crypto:sign://provider?privateKey=#myPrivateKey&provider=SUN" /> <to uri="crypto:verify://provider?publicKey=#myPublicKey&provider=SUN" /> <to uri="mock:result"/> </route> 4) Changing the Signature Mesasge HeaderIt may be desirable to change the message header used to store the signature. A different header name can be specified in the route definition as follows from("direct:signature-header").to("crypto:sign://another?privateKey=#myPrivateKey&signatureHeader=AnotherDigitalSignature", "crypto:verify://another?publicKey=#myPublicKey&signatureHeader=AnotherDigitalSignature", "mock:result"); or <route> <from uri="direct:signature-header"/> <to uri="crypto:sign://another?privateKey=#myPrivateKey&signatureHeader=AnotherDigitalSignature" /> <to uri="crypto:verify://another?publicKey=#myPublicKey&signatureHeader=AnotherDigitalSignature" /> <to uri="mock:result"/> </route> 5) Changing the buffersizeIn case you need to update the size of the buffer... from("direct:buffersize").to("crypto:sign://buffer?privateKey=#myPrivateKey&buffersize=1024", "crypto:verify://buffer?publicKey=#myPublicKey&buffersize=1024", "mock:result"); or <route> <from uri="direct:buffersize" /> <to uri="crypto:sign://buffer?privateKey=#myPrivateKey&buffersize=1024" /> <to uri="crypto:verify://buffer?publicKey=#myPublicKey&buffersize=1024" /> <to uri="mock:result"/> </route> 6) Supplying Keys dynamically.When using a Recipient list or similar EIP the recipient of an exchange can vary dynamically. Using the same key across all recipients may be neither feasible nor desirable. It would be useful to be able to specify signature keys dynamically on a per-exchange basis. The exchange could then be dynamically enriched with the key of its target recipient prior to signing. To facilitate this the signature mechanisms allow for keys to be supplied dynamically via the message headers below
from("direct:headerkey-sign").to("crypto:sign://alias"); from("direct:headerkey-verify").to("crypto:verify://alias", "mock:result"); or <route> <from uri="direct:headerkey-sign"/> <to uri="crypto:sign://headerkey" /> </route> <route> <from uri="direct:headerkey-verify"/> <to uri="crypto:verify://headerkey" /> <to uri="mock:result"/> </route> Even better would be to dynamically supply a keystore alias. Again the alias can be supplied in a message header
from("direct:alias-sign").to("crypto:sign://alias?keystore=#keystore"); from("direct:alias-verify").to("crypto:verify://alias?keystore=#keystore", "mock:result"); or <route> <from uri="direct:alias-sign"/> <to uri="crypto:sign://alias?keystore=#keystore" /> </route> <route> <from uri="direct:alias-verify"/> <to uri="crypto:verify://alias?keystore=#keystore" /> <to uri="mock:result"/> </route> The header would be set as follows Exchange unsigned = getMandatoryEndpoint("direct:alias-sign").createExchange(); unsigned.getIn().setBody(payload); unsigned.getIn().setHeader(DigitalSignatureConstants.KEYSTORE_ALIAS, "bob"); unsigned.getIn().setHeader(DigitalSignatureConstants.KEYSTORE_PASSWORD, "letmein".toCharArray()); template.send("direct:alias-sign", unsigned); Exchange signed = getMandatoryEndpoint("direct:alias-sign").createExchange(); signed.getIn().copyFrom(unsigned.getOut()); signed.getIn().setHeader(KEYSTORE_ALIAS, "bob"); template.send("direct:alias-verify", signed); See Also
CXF Component
The cxf: component provides integration with Apache CXF for connecting to JAX-WS services hosted in CXF.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cxf</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
URI formatcxf:bean:cxfEndpoint[?options] Where cxfEndpoint represents a bean ID that references a bean in the Spring bean registry. With this URI format, most of the endpoint details are specified in the bean definition.
cxf://someAddress[?options]
Where someAddress specifies the CXF endpoint's address. With this URI format, most of the endpoint details are specified using options. For either style above, you can append options to the URI as follows: cxf:bean:cxfEndpoint?wsdlURL=wsdl/hello_world.wsdl&dataFormat=PAYLOAD Options
The serviceName and portName are QNames, so if you provide them be sure to prefix them with their {namespace} as shown in the examples above. The descriptions of the dataformats
You can determine the data format mode of an exchange by retrieving the exchange property, CamelCXFDataFormat. The exchange key constant is defined in org.apache.camel.component.cxf.CxfConstants.DATA_FORMAT_PROPERTY. How to enable CXF's LoggingOutInterceptor in MESSAGE modeCXF's LoggingOutInterceptor outputs outbound message that goes on the wire to logging system (Java Util Logging). Since the LoggingOutInterceptor is in PRE_STREAM phase (but PRE_STREAM phase is removed in MESSAGE mode), you have to configure LoggingOutInterceptor to be run during the WRITE phase. The following is an example. <bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"> <!-- it really should have been user-prestream but CXF does have such phase! --> <constructor-arg value="target/write"/> </bean> <cxf:cxfEndpoint id="serviceEndpoint" address="http://localhost:${CXFTestSupport.port2}/LoggingInterceptorInMessageModeTest/helloworld" serviceClass="org.apache.camel.component.cxf.HelloService"> <cxf:outInterceptors> <ref bean="loggingOutInterceptor"/> </cxf:outInterceptors> <cxf:properties> <entry key="dataFormat" value="MESSAGE"/> </cxf:properties> </cxf:cxfEndpoint> Description of relayHeaders optionThere are in-band and out-of-band on-the-wire headers from the perspective of a JAXWS WSDL-first developer. The in-band headers are headers that are explicitly defined as part of the WSDL binding contract for an endpoint such as SOAP headers. The out-of-band headers are headers that are serialized over the wire, but are not explicitly part of the WSDL binding contract. Headers relaying/filtering is bi-directional. When a route has a CXF endpoint and the developer needs to have on-the-wire headers, such as SOAP headers, be relayed along the route to be consumed say by another JAXWS endpoint, then relayHeaders should be set to true, which is the default value. Available only in POJO modeThe relayHeaders=true express an intent to relay the headers. The actual decision on whether a given header is relayed is delegated to a pluggable instance that implements the MessageHeadersRelay interface. A concrete implementation of MessageHeadersRelay will be consulted to decide if a header needs to be relayed or not. There is already an implementation of SoapMessageHeadersRelay which binds itself to well-known SOAP name spaces. Currently only out-of-band headers are filtered, and in-band headers will always be relayed when relayHeaders=true. If there is a header on the wire, whose name space is unknown to the runtime, then a fall back DefaultMessageHeadersRelay will be used, which simply allows all headers to be relayed. The relayHeaders=false setting asserts that all headers in-band and out-of-band will be dropped. You can plugin your own MessageHeadersRelay implementations overriding or adding additional ones to the list of relays. In order to override a preloaded relay instance just make sure that your MessageHeadersRelay implementation services the same name spaces as the one you looking to override. Also note, that the overriding relay has to service all of the name spaces as the one you looking to override, or else a runtime exception on route start up will be thrown as this would introduce an ambiguity in name spaces to relay instance mappings. <cxf:cxfEndpoint ...> <cxf:properties> <entry key="org.apache.camel.cxf.message.headers.relays"> <list> <ref bean="customHeadersRelay"/> </list> </entry> </cxf:properties> </cxf:cxfEndpoint> <bean id="customHeadersRelay" class="org.apache.camel.component.cxf.soap.headers.CustomHeadersRelay"/> Take a look at the tests that show how you'd be able to relay/drop headers here: Changes since Release 2.0
You can find more advanced examples which show how to provide interceptors , properties and handlers here: NOTE <cxf:cxfEndpoint id="testEndpoint" address="http://localhost:9000/router" serviceClass="org.apache.camel.component.cxf.HelloService" endpointName="s:PortName" serviceName="s:ServiceName" xmlns:s="http://www.example.com/test"> <cxf:properties> <entry key="dataFormat" value="MESSAGE"/> <entry key="setDefaultBus" value="true"/> </cxf:properties> </cxf:cxfEndpoint> Configuring the CXF Endpoints with Apache Aries Blueprint.Since camel 2.8 there is support for utilizing aries blueprint dependency injection for your CXF endpoints. Example <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0" xmlns:camel-cxf="http://camel.apache.org/schema/blueprint/cxf" xmlns:cxfcore="http://cxf.apache.org/blueprint/core" xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> <camel-cxf:cxfEndpoint id="routerEndpoint" address="http://localhost:9001/router" serviceClass="org.apache.servicemix.examples.cxf.HelloWorld"> <camel-cxf:properties> <entry key="dataFormat" value="MESSAGE"/> </camel-cxf:properties> </camel-cxf:cxfEndpoint> <camel-cxf:cxfEndpoint id="serviceEndpoint" address="http://localhost:9000/SoapContext/SoapPort" serviceClass="org.apache.servicemix.examples.cxf.HelloWorld"> </camel-cxf:cxfEndpoint> <camelContext xmlns="http://camel.apache.org/schema/blueprint"> <route> <from uri="routerEndpoint"/> <to uri="log:request"/> </route> </camelContext> </blueprint> Currently the endpoint element is the first supported CXF namespacehandler. You can also use the bean references just as in spring <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0" xmlns:jaxws="http://cxf.apache.org/blueprint/jaxws" xmlns:cxf="http://cxf.apache.org/blueprint/core" xmlns:camel="http://camel.apache.org/schema/blueprint" xmlns:camelcxf="http://camel.apache.org/schema/blueprint/cxf" xsi:schemaLocation=" http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd http://cxf.apache.org/blueprint/jaxws http://cxf.apache.org/schemas/blueprint/jaxws.xsd http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd "> <camelcxf:cxfEndpoint id="reportIncident" address="/camel-example-cxf-blueprint/webservices/incident" wsdlURL="META-INF/wsdl/report_incident.wsdl" serviceClass="org.apache.camel.example.reportincident.ReportIncidentEndpoint"> </camelcxf:cxfEndpoint> <bean id="reportIncidentRoutes" class="org.apache.camel.example.reportincident.ReportIncidentRoutes" /> <camelContext xmlns="http://camel.apache.org/schema/blueprint"> <routeBuilder ref="reportIncidentRoutes"/> </camelContext> </blueprint> How to make the camel-cxf component use log4j instead of java.util.loggingCXF's default logger is java.util.logging. If you want to change it to log4j, proceed as follows. Create a file, in the classpath, named META-INF/cxf/org.apache.cxf.logger. This file should contain the fully-qualified name of the class, org.apache.cxf.common.logging.Log4jLogger, with no comments, on a single line. How to let camel-cxf response message with xml start documentIf you are using some soap client such as PHP, you will get this kind of error, because CXF doesn't add the XML start document "<?xml version="1.0" encoding="utf-8"?>" Error:sendSms: SoapFault exception: [Client] looks like we got no XML document in [...] To resolved this issue, you just need to tell StaxOutInterceptor to write the XML start document for you. public class WriteXmlDeclarationInterceptor extends AbstractPhaseInterceptor<SoapMessage> { public WriteXmlDeclarationInterceptor() { super(Phase.PRE_STREAM); addBefore(StaxOutInterceptor.class.getName()); } public void handleMessage(SoapMessage message) throws Fault { message.put("org.apache.cxf.stax.force-start-document", Boolean.TRUE); } } You can add a customer interceptor like this and configure it into you camel-cxf endpont <cxf:cxfEndpoint id="routerEndpoint" address="http://localhost:${CXFTestSupport.port2}/CXFGreeterRouterTest/CamelContext/RouterPort" serviceClass="org.apache.hello_world_soap_http.GreeterImpl" skipFaultLogging="true"> <cxf:outInterceptors> <!-- This interceptor will force the CXF server send the XML start document to client --> <bean class="org.apache.camel.component.cxf.WriteXmlDeclarationInterceptor"/> </cxf:outInterceptors> <cxf:properties> <!-- Set the publishedEndpointUrl which could override the service address from generated WSDL as you want --> <entry key="publishedEndpointUrl" value="http://www.simple.com/services/test" /> </cxf:properties> </cxf:cxfEndpoint> Or adding a message header for it like this if you are using Camel 2.4. // set up the response context which force start document Map<String, Object> map = new HashMap<String, Object>(); map.put("org.apache.cxf.stax.force-start-document", Boolean.TRUE); exchange.getOut().setHeader(Client.RESPONSE_CONTEXT, map); How to consume a message from a camel-cxf endpoint in POJO data formatThe camel-cxf endpoint consumer POJO data format is based on the cxf invoker, so the message header has a property with the name of CxfConstants.OPERATION_NAME and the message body is a list of the SEI method parameters. public class PersonProcessor implements Processor { private static final transient Logger LOG = LoggerFactory.getLogger(PersonProcessor.class); @SuppressWarnings("unchecked") public void process(Exchange exchange) throws Exception { LOG.info("processing exchange in camel"); BindingOperationInfo boi = (BindingOperationInfo)exchange.getProperty(BindingOperationInfo.class.toString()); if (boi != null) { LOG.info("boi.isUnwrapped" + boi.isUnwrapped()); } // Get the parameters list which element is the holder. MessageContentsList msgList = (MessageContentsList)exchange.getIn().getBody(); Holder<String> personId = (Holder<String>)msgList.get(0); Holder<String> ssn = (Holder<String>)msgList.get(1); Holder<String> name = (Holder<String>)msgList.get(2); if (personId.value == null || personId.value.length() == 0) { LOG.info("person id 123, so throwing exception"); // Try to throw out the soap fault message org.apache.camel.wsdl_first.types.UnknownPersonFault personFault = new org.apache.camel.wsdl_first.types.UnknownPersonFault(); personFault.setPersonId(""); org.apache.camel.wsdl_first.UnknownPersonFault fault = new org.apache.camel.wsdl_first.UnknownPersonFault("Get the null value of person name", personFault); // Since camel has its own exception handler framework, we can't throw the exception to trigger it // We just set the fault message in the exchange for camel-cxf component handling and return exchange.getOut().setFault(true); exchange.getOut().setBody(fault); return; } name.value = "Bonjour"; ssn.value = "123"; LOG.info("setting Bonjour as the response"); // Set the response message, first element is the return value of the operation, // the others are the holders of method parameters exchange.getOut().setBody(new Object[] {null, personId, ssn, name}); } } How to prepare the message for the camel-cxf endpoint in POJO data formatThe camel-cxf endpoint producer is based on the cxf client API. First you need to specify the operation name in the message header, then add the method parameters to a list, and initialize the message with this parameter list. The response message's body is a messageContentsList, you can get the result from that list. If you want to get the object array from the message body, you can get the body using message.getbody(Object[].class), as follows: Exchange senderExchange = new DefaultExchange(context, ExchangePattern.InOut); final List<String> params = new ArrayList<String>(); // Prepare the request message for the camel-cxf procedure params.add(TEST_MESSAGE); senderExchange.getIn().setBody(params); senderExchange.getIn().setHeader(CxfConstants.OPERATION_NAME, ECHO_OPERATION); Exchange exchange = template.send("direct:EndpointA", senderExchange); org.apache.camel.Message out = exchange.getOut(); // The response message's body is an MessageContentsList which first element is the return value of the operation, // If there are some holder parameters, the holder parameter will be filled in the reset of List. // The result will be extract from the MessageContentsList with the String class type MessageContentsList result = (MessageContentsList)out.getBody(); LOG.info("Received output text: " + result.get(0)); Map<String, Object> responseContext = CastUtils.cast((Map<?, ?>)out.getHeader(Client.RESPONSE_CONTEXT)); assertNotNull(responseContext); assertEquals("We should get the response context here", "UTF-8", responseContext.get(org.apache.cxf.message.Message.ENCODING)); assertEquals("Reply body on Camel is wrong", "echo " + TEST_MESSAGE, result.get(0)); How to deal with the message for a camel-cxf endpoint in PAYLOAD data formatPAYLOAD means that you process the payload message from the SOAP envelope. You can use the Header.HEADER_LIST as the key to set or get the SOAP headers and use the List<Element> to set or get SOAP body elements. protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { from(simpleEndpointURI + "&dataFormat=PAYLOAD").to("log:info").process(new Processor() { @SuppressWarnings("unchecked") public void process(final Exchange exchange) throws Exception { CxfPayload<SoapHeader> requestPayload = exchange.getIn().getBody(CxfPayload.class); List<Source> inElements = requestPayload.getBodySources(); List<Source> outElements = new ArrayList<Source>(); // You can use a customer toStringConverter to turn a CxfPayLoad message into String as you want String request = exchange.getIn().getBody(String.class); XmlConverter converter = new XmlConverter(); String documentString = ECHO_RESPONSE; Element in = new XmlConverter().toDOMElement(inElements.get(0)); // Just check the element namespace if (!in.getNamespaceURI().equals(ELEMENT_NAMESPACE)) { throw new IllegalArgumentException("Wrong element namespace"); } if (in.getLocalName().equals("echoBoolean")) { documentString = ECHO_BOOLEAN_RESPONSE; checkRequest("ECHO_BOOLEAN_REQUEST", request); } else { documentString = ECHO_RESPONSE; checkRequest("ECHO_REQUEST", request); } Document outDocument = converter.toDOMDocument(documentString); outElements.add(new DOMSource(outDocument.getDocumentElement())); // set the payload header with null CxfPayload<SoapHeader> responsePayload = new CxfPayload<SoapHeader>(null, outElements, null); exchange.getOut().setBody(responsePayload); } }); } }; } How to get and set SOAP headers in POJO modePOJO means that the data format is a "list of Java objects" when the Camel-cxf endpoint produces or consumes Camel exchanges. Even though Camel expose message body as POJOs in this mode, Camel-cxf still provides access to read and write SOAP headers. However, since CXF interceptors remove in-band SOAP headers from Header list after they have been processed, only out-of-band SOAP headers are available to Camel-cxf in POJO mode. The following example illustrate how to get/set SOAP headers. Suppose we have a route that forwards from one Camel-cxf endpoint to another. That is, SOAP Client -> Camel -> CXF service. We can attach two processors to obtain/insert SOAP headers at (1) before request goes out to the CXF service and (2) before response comes back to the SOAP Client. Processor (1) and (2) in this example are InsertRequestOutHeaderProcessor and InsertResponseOutHeaderProcessor. Our route looks like this: <route> <from uri="cxf:bean:routerRelayEndpointWithInsertion"/> <process ref="InsertRequestOutHeaderProcessor" /> <to uri="cxf:bean:serviceRelayEndpointWithInsertion"/> <process ref="InsertResponseOutHeaderProcessor" /> </route> SOAP headers are propagated to and from Camel Message headers. The Camel message header name is "org.apache.cxf.headers.Header.list" which is a constant defined in CXF (org.apache.cxf.headers.Header.HEADER_LIST). The header value is a List of CXF SoapHeader objects (org.apache.cxf.binding.soap.SoapHeader). The following snippet is the InsertResponseOutHeaderProcessor (that insert a new SOAP header in the response message). The way to access SOAP headers in both InsertResponseOutHeaderProcessor and InsertRequestOutHeaderProcessor are actually the same. The only difference between the two processors is setting the direction of the inserted SOAP header. public static class InsertResponseOutHeaderProcessor implements Processor { public void process(Exchange exchange) throws Exception { // You should be able to get the header if exchange is routed from camel-cxf endpoint List<SoapHeader> soapHeaders = CastUtils.cast((List<?>)exchange.getIn().getHeader(Header.HEADER_LIST)); if (soapHeaders == null) { // we just create a new soap headers in case the header is null soapHeaders = new ArrayList<SoapHeader>(); } // Insert a new header String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><outofbandHeader " + "xmlns=\"http://cxf.apache.org/outofband/Header\" hdrAttribute=\"testHdrAttribute\" " + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" soap:mustUnderstand=\"1\">" + "<name>New_testOobHeader</name><value>New_testOobHeaderValue</value></outofbandHeader>"; SoapHeader newHeader = new SoapHeader(soapHeaders.get(0).getName(), DOMUtils.readXml(new StringReader(xml)).getDocumentElement()); // make sure direction is OUT since it is a response message. newHeader.setDirection(Direction.DIRECTION_OUT); //newHeader.setMustUnderstand(false); soapHeaders.add(newHeader); } } How to get and set SOAP headers in PAYLOAD modeWe've already shown how to access SOAP message (CxfPayload object) in PAYLOAD mode (See "How to deal with the message for a camel-cxf endpoint in PAYLOAD data format"). Once you obtain a CxfPayload object, you can invoke the CxfPayload.getHeaders() method that returns a List of DOM Elements (SOAP headers). from(getRouterEndpointURI()).process(new Processor() { @SuppressWarnings("unchecked") public void process(Exchange exchange) throws Exception { CxfPayload<SoapHeader> payload = exchange.getIn().getBody(CxfPayload.class); List<Source> elements = payload.getBodySources(); assertNotNull("We should get the elements here", elements); assertEquals("Get the wrong elements size", 1, elements.size()); Element el = new XmlConverter().toDOMElement(elements.get(0)); elements.set(0, new DOMSource(el)); assertEquals("Get the wrong namespace URI", "http://camel.apache.org/pizza/types", el.getNamespaceURI()); List<SoapHeader> headers = payload.getHeaders(); assertNotNull("We should get the headers here", headers); assertEquals("Get the wrong headers size", headers.size(), 1); assertEquals("Get the wrong namespace URI", ((Element)(headers.get(0).getObject())).getNamespaceURI(), "http://camel.apache.org/pizza/types"); } }) .to(getServiceEndpointURI()); SOAP headers are not available in MESSAGE modeSOAP headers are not available in MESSAGE mode as SOAP processing is skipped. How to throw a SOAP Fault from CamelIf you are using a camel-cxf endpoint to consume the SOAP request, you may need to throw the SOAP Fault from the camel context. SOAP_FAULT = new SoapFault(EXCEPTION_MESSAGE, SoapFault.FAULT_CODE_CLIENT);
Element detail = SOAP_FAULT.getOrCreateDetail();
Document doc = detail.getOwnerDocument();
Text tn = doc.createTextNode(DETAIL_TEXT);
detail.appendChild(tn);
Then throw it as you like from(routerEndpointURI).setFaultBody(constant(SOAP_FAULT)); If your CXF endpoint is working in the MESSAGE data format, you could set the the SOAP Fault message in the message body and set the response code in the message header. from(routerEndpointURI).process(new Processor() { public void process(Exchange exchange) throws Exception { Message out = exchange.getOut(); // Set the message body with the out.setBody(this.getClass().getResourceAsStream("SoapFaultMessage.xml")); // Set the response code here out.setHeader(org.apache.cxf.message.Message.RESPONSE_CODE, new Integer(500)); } }); Same for using POJO data format. You can set the SOAPFault on the out body and also indicate it's a fault by calling Message.setFault(true): from("direct:start").onException(SoapFault.class).maximumRedeliveries(0).handled(true) .process(new Processor() { public void process(Exchange exchange) throws Exception { SoapFault fault = exchange .getProperty(Exchange.EXCEPTION_CAUGHT, SoapFault.class); exchange.getOut().setFault(true); exchange.getOut().setBody(fault); } }).end().to(serviceURI); How to propagate a camel-cxf endpoint's request and response contextcxf client API provides a way to invoke the operation with request and response context. If you are using a camel-cxf endpoint producer to invoke the outside web service, you can set the request context and get response context with the following code:
CxfExchange exchange = (CxfExchange)template.send(getJaxwsEndpointUri(), new Processor() {
public void process(final Exchange exchange) {
final List<String> params = new ArrayList<String>();
params.add(TEST_MESSAGE);
// Set the request context to the inMessage
Map<String, Object> requestContext = new HashMap<String, Object>();
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, JAXWS_SERVER_ADDRESS);
exchange.getIn().setBody(params);
exchange.getIn().setHeader(Client.REQUEST_CONTEXT , requestContext);
exchange.getIn().setHeader(CxfConstants.OPERATION_NAME, GREET_ME_OPERATION);
}
});
org.apache.camel.Message out = exchange.getOut();
// The output is an object array, the first element of the array is the return value
Object\[\] output = out.getBody(Object\[\].class);
LOG.info("Received output text: " + output\[0\]);
// Get the response context form outMessage
Map<String, Object> responseContext = CastUtils.cast((Map)out.getHeader(Client.RESPONSE_CONTEXT));
assertNotNull(responseContext);
assertEquals("Get the wrong wsdl opertion name", "{http://apache.org/hello_world_soap_http}greetMe",
responseContext.get("javax.xml.ws.wsdl.operation").toString());
Attachment SupportPOJO Mode: Both SOAP with Attachment and MTOM are supported (see example in Payload Mode for enabling MTOM). However, SOAP with Attachment is not tested. Since attachments are marshalled and unmarshalled into POJOs, users typically do not need to deal with the attachment themself. Attachments are propagated to Camel message's attachments since 2.1. So, it is possible to retreive attachments by Camel Message API DataHandler Message.getAttachment(String id)
. Payload Mode: MTOM is supported since 2.1. Attachments can be retrieved by Camel Message APIs mentioned above. SOAP with Attachment (SwA) is supported and attachments can be retrieved since 2.5. SwA is the default (same as setting the CXF endpoint property "mtom-enabled" to false). To enable MTOM, set the CXF endpoint property "mtom-enabled" to true. (I believe you can only do it with Spring.) <cxf:cxfEndpoint id="routerEndpoint" address="http://localhost:${CXFTestSupport.port1}/CxfMtomRouterPayloadModeTest/jaxws-mtom/hello" wsdlURL="mtom.wsdl" serviceName="ns:HelloService" endpointName="ns:HelloPort" xmlns:ns="http://apache.org/camel/cxf/mtom_feature"> <cxf:properties> <!-- enable mtom by setting this property to true --> <entry key="mtom-enabled" value="true"/> <!-- set the camel-cxf endpoint data fromat to PAYLOAD mode --> <entry key="dataFormat" value="PAYLOAD"/> </cxf:properties> You can produce a Camel message with attachment to send to a CXF endpoint in Payload mode. Exchange exchange = context.createProducerTemplate().send("direct:testEndpoint", new Processor() { public void process(Exchange exchange) throws Exception { exchange.setPattern(ExchangePattern.InOut); List<Source> elements = new ArrayList<Source>(); elements.add(new DOMSource(DOMUtils.readXml(new StringReader(MtomTestHelper.REQ_MESSAGE)).getDocumentElement())); CxfPayload<SoapHeader> body = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(), elements, null); exchange.getIn().setBody(body); exchange.getIn().addAttachment(MtomTestHelper.REQ_PHOTO_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.REQ_PHOTO_DATA, "application/octet-stream"))); exchange.getIn().addAttachment(MtomTestHelper.REQ_IMAGE_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.requestJpeg, "image/jpeg"))); } }); // process response CxfPayload<SoapHeader> out = exchange.getOut().getBody(CxfPayload.class); Assert.assertEquals(1, out.getBody().size()); Map<String, String> ns = new HashMap<String, String>(); ns.put("ns", MtomTestHelper.SERVICE_TYPES_NS); ns.put("xop", MtomTestHelper.XOP_NS); XPathUtils xu = new XPathUtils(ns); Element oute = new XmlConverter().toDOMElement(out.getBody().get(0)); Element ele = (Element)xu.getValue("//ns:DetailResponse/ns:photo/xop:Include", oute, XPathConstants.NODE); String photoId = ele.getAttribute("href").substring(4); // skip "cid:" ele = (Element)xu.getValue("//ns:DetailResponse/ns:image/xop:Include", oute, XPathConstants.NODE); String imageId = ele.getAttribute("href").substring(4); // skip "cid:" DataHandler dr = exchange.getOut().getAttachment(photoId); Assert.assertEquals("application/octet-stream", dr.getContentType()); MtomTestHelper.assertEquals(MtomTestHelper.RESP_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream())); dr = exchange.getOut().getAttachment(imageId); Assert.assertEquals("image/jpeg", dr.getContentType()); BufferedImage image = ImageIO.read(dr.getInputStream()); Assert.assertEquals(560, image.getWidth()); Assert.assertEquals(300, image.getHeight()); You can also consume a Camel message received from a CXF endpoint in Payload mode. public static class MyProcessor implements Processor { @SuppressWarnings("unchecked") public void process(Exchange exchange) throws Exception { CxfPayload<SoapHeader> in = exchange.getIn().getBody(CxfPayload.class); // verify request assertEquals(1, in.getBody().size()); Map<String, String> ns = new HashMap<String, String>(); ns.put("ns", MtomTestHelper.SERVICE_TYPES_NS); ns.put("xop", MtomTestHelper.XOP_NS); XPathUtils xu = new XPathUtils(ns); Element body = new XmlConverter().toDOMElement(in.getBody().get(0)); Element ele = (Element)xu.getValue("//ns:Detail/ns:photo/xop:Include", body, XPathConstants.NODE); String photoId = ele.getAttribute("href").substring(4); // skip "cid:" assertEquals(MtomTestHelper.REQ_PHOTO_CID, photoId); ele = (Element)xu.getValue("//ns:Detail/ns:image/xop:Include", body, XPathConstants.NODE); String imageId = ele.getAttribute("href").substring(4); // skip "cid:" assertEquals(MtomTestHelper.REQ_IMAGE_CID, imageId); DataHandler dr = exchange.getIn().getAttachment(photoId); assertEquals("application/octet-stream", dr.getContentType()); MtomTestHelper.assertEquals(MtomTestHelper.REQ_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream())); dr = exchange.getIn().getAttachment(imageId); assertEquals("image/jpeg", dr.getContentType()); MtomTestHelper.assertEquals(MtomTestHelper.requestJpeg, IOUtils.readBytesFromStream(dr.getInputStream())); // create response List<Source> elements = new ArrayList<Source>(); elements.add(new DOMSource(DOMUtils.readXml(new StringReader(MtomTestHelper.RESP_MESSAGE)).getDocumentElement())); CxfPayload<SoapHeader> sbody = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(), elements, null); exchange.getOut().setBody(sbody); exchange.getOut().addAttachment(MtomTestHelper.RESP_PHOTO_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.RESP_PHOTO_DATA, "application/octet-stream"))); exchange.getOut().addAttachment(MtomTestHelper.RESP_IMAGE_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.responseJpeg, "image/jpeg"))); } } Message Mode: Attachments are not supported as it does not process the message at all. Streaming Support in PAYLOAD modeIn 2.8.2, the camel-cxf component now supports streaming of incoming messages when using PAYLOAD mode. Previously, the incoming messages would have been completely DOM parsed. For large messages, this is time consuming and uses a significant amount of memory. Starting in 2.8.2, the incoming messages can remain as a javax.xml.transform.Source while being routed and, if nothing modifies the payload, can then be directly streamed out to the target destination. For common "simple proxy" use cases (example: from("cxf:...").to("cxf:...")), this can provide very significant performance increases as well as significantly lowered memory requirements. However, there are cases where streaming may not be appropriate or desired. Due to the streaming nature, invalid incoming XML may not be caught until later in the processing chain. Also, certain actions may require the message to be DOM parsed anyway (like WS-Security or message tracing and such) in which case the advantages of the streaming is limited. At this point, there are two ways to control the streaming:
See AlsoCXF Bean ComponentThe cxfbean: component allows other Camel endpoints to send exchange and invoke Web service bean objects. (Currently, it only supports JAXRS, JAXWS(new to camel2.1) annotated service bean.)
URI formatcxfbean:serviceBeanRef Where serviceBeanRef is a registry key to look up the service bean object. If serviceBeanRef references a List object, elements of the List are the service bean objects accepted by the endpoint. Options
Headers
A Working SampleThis sample shows how to create a route that starts a Jetty HTTP server. The route sends requests to a CXF Bean and invokes a JAXRS annotated service. First, create a route as follows. The from endpoint is a Jetty HTTP endpoint that is listening on port 9000. Notice that the matchOnUriPrefix option must be set to true because RESTful request URI will not match the endpoint's URI http://localhost:9000 exactly. <route> <from ref="ep1" /> <to uri="cxfbean:customerServiceBean" /> <to uri="mock:endpointA" /> </route> The to endpoint is a CXF Bean with bean name customerServiceBean. The name will be looked up from the registry. Next, we make sure our service bean is available in Spring registry. We create a bean definition in the Spring configuration. In this example, we create a List of service beans (of one element). We could have created just a single bean without a List. <util:list id="customerServiceBean"> <bean class="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService" /> </util:list> <bean class="org.apache.camel.wsdl_first.PersonImpl" id="jaxwsBean" /> That's it. Once the route is started, the web service is ready for business. A HTTP client can make a request and receive response. CXFRS Component
The cxfrs: component provides integration with Apache CXF for connecting to JAX-RS services hosted in CXF. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-cxf</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI format
cxfrs://address?options
Where address represents the CXF endpoint's address cxfrs:bean:rsEndpoint Where rsEndpoint represents the spring bean's name which presents the CXFRS client or server For either style above, you can append options to the URI as follows: cxfrs:bean:cxfEndpoint?resourceClasses=org.apache.camel.rs.Example Options
You can also configure the CXF REST endpoint through the spring configuration. Since there are lots of difference between the CXF REST client and CXF REST Server, we provide different configuration for them. How to configure the REST endpoint in CamelIn camel-cxf schema file, there are two elements for the REST endpoint definition. cxf:rsServer for REST consumer, cxf:rsClient for REST producer. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://camel.apache.org/schema/cxf" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd "> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/> <!-- Defined the real JAXRS back end service --> <jaxrs:server id="restService" address="http://localhost:${CXFTestSupport.port2}/CxfRsRouterTest/rest" staticSubresourceResolution="true"> <jaxrs:serviceBeans> <ref bean="customerService"/> </jaxrs:serviceBeans> </jaxrs:server> <!-- bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.JSONProvider"/--> <bean id="customerService" class="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService" /> <!-- Defined the server endpoint to create the cxf-rs consumer --> <cxf:rsServer id="rsServer" address="http://localhost:${CXFTestSupport.port1}/CxfRsRouterTest/route" serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService" loggingFeatureEnabled="true" loggingSizeLimit="20" skipFaultLogging="true"/> <!-- Defined the client endpoint to create the cxf-rs consumer --> <cxf:rsClient id="rsClient" address="http://localhost:${CXFTestSupport.port2}/CxfRsRouterTest/rest" serviceClass="org.apache.camel.component.cxf.jaxrs.testbean.CustomerService" loggingFeatureEnabled="true" skipFaultLogging="true"/> <!-- The camel route context --> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="cxfrs://bean://rsServer"/> <!-- We can remove this configure as the CXFRS producer is using the HttpAPI by default --> <setHeader headerName="CamelCxfRsUsingHttpAPI"> <constant>True</constant> </setHeader> <to uri="cxfrs://bean://rsClient"/> </route> </camelContext> </beans> Consuming a REST Request - Simple Binding StyleAvailable as of Camel 2.11 The Default binding style is rather low-level, requiring the user to manually process the MessageContentsList object coming into the route. Thus, it tightly couples the route logic with the method signature and parameter indices of the JAX-RS operation. Somewhat inelegant, difficult and error-prone. In contrast, the SimpleConsumer binding style performs the following mappings, in order to make the request data more accessible to you within the Camel Message:
Additionally, the following rules apply to the Response mapping:
Enabling the Simple Binding StyleThis binding style can be activated by setting the bindingStyle parameter in the consumer endpoint to value SimpleConsumer: from("cxf:bean:rsServer?bindingStyle=SimpleConsumer") .to("log:TEST?showAll=true"); Examples of request binding with different method signaturesBelow is a list of method signatures along with the expected result from the Simple binding. public Response doAction(BusinessObject request); public Response doAction(BusinessObject request, @HeaderParam("abcd") String abcd, @QueryParam("defg") String defg); public Response doAction(@HeaderParam("abcd") String abcd, @QueryParam("defg") String defg); public Response doAction(@Multipart(value="body1") BusinessObject request, @Multipart(value="body2") BusinessObject request2); public Response doAction(InputStream abcd); public Response doAction(DataHandler abcd); More examples of the Simple Binding StyleGiven a JAX-RS resource class with this method:
@POST @Path("/customers/{type}")
public Response newCustomer(Customer customer, @PathParam("type") String type, @QueryParam("active") @DefaultValue("true") boolean active) {
return null;
}
Serviced by the following route:
from("cxf:bean:rsServer?bindingStyle=SimpleConsumer")
.recipientList(simple("direct:${header.operationName}"));
from("direct:newCustomer")
.log("Request: type=${header.type}, active=${header.active}, customerData=${body}");
The following HTTP request with XML payload (given that the Customer DTO is JAXB-annotated):
POST /customers/gold?active=true
Payload:
<Customer>
<fullName>Raul Kripalani</fullName>
<country>Spain</country>
<project>Apache Camel</project>
</Customer>
Will print the message:
Request: type=gold, active=true, customerData=<Customer.toString() representation>
For more examples on how to process requests and write responses can be found here. Consuming a REST Request - Default Binding StyleCXF JAXRS front end implements the JAXRS(JSR311) API, so we can export the resources classes as a REST service. And we leverage the CXF Invoker API to turn a REST request into a normal Java object method invocation. Here is an example of a CXFRS route... private static final String CXF_RS_ENDPOINT_URI = "cxfrs://http://localhost:" + CXT + "/rest?resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerServiceResource"; protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() { errorHandler(new NoErrorHandlerBuilder()); from(CXF_RS_ENDPOINT_URI).process(new Processor() { public void process(Exchange exchange) throws Exception { Message inMessage = exchange.getIn(); // Get the operation name from in message String operationName = inMessage.getHeader(CxfConstants.OPERATION_NAME, String.class); if ("getCustomer".equals(operationName)) { String httpMethod = inMessage.getHeader(Exchange.HTTP_METHOD, String.class); assertEquals("Get a wrong http method", "GET", httpMethod); String path = inMessage.getHeader(Exchange.HTTP_PATH, String.class); // The parameter of the invocation is stored in the body of in message String id = inMessage.getBody(String.class); if ("/customerservice/customers/126".equals(path)) { Customer customer = new Customer(); customer.setId(Long.parseLong(id)); customer.setName("Willem"); // We just put the response Object into the out message body exchange.getOut().setBody(customer); } else { if ("/customerservice/customers/400".equals(path)) { // We return the remote client IP address this time org.apache.cxf.message.Message cxfMessage = inMessage.getHeader(CxfConstants.CAMEL_CXF_MESSAGE, org.apache.cxf.message.Message.class); ServletRequest request = (ServletRequest) cxfMessage.get("HTTP.REQUEST"); String remoteAddress = request.getRemoteAddr(); Response r = Response.status(200).entity("The remoteAddress is " + remoteAddress).build(); exchange.getOut().setBody(r); return; } if ("/customerservice/customers/123".equals(path)) { // send a customer response back Response r = Response.status(200).entity("customer response back!").build(); exchange.getOut().setBody(r); return; } if ("/customerservice/customers/456".equals(path)) { Response r = Response.status(404).entity("Can't found the customer with uri " + path).build(); throw new WebApplicationException(r); } else { throw new RuntimeCamelException("Can't found the customer with uri " + path); } } } if ("updateCustomer".equals(operationName)) { assertEquals("Get a wrong customer message header", "header1;header2", inMessage.getHeader("test")); String httpMethod = inMessage.getHeader(Exchange.HTTP_METHOD, String.class); assertEquals("Get a wrong http method", "PUT", httpMethod); Customer customer = inMessage.getBody(Customer.class); assertNotNull("The customer should not be null.", customer); // Now you can do what you want on the customer object assertEquals("Get a wrong customer name.", "Mary", customer.getName()); // set the response back exchange.getOut().setBody(Response.ok().build()); } } }); } }; } And the corresponding resource class used to configure the endpoint...
@Path("/customerservice/") public interface CustomerServiceResource { @GET @Path("/customers/{id}/") Customer getCustomer(@PathParam("id") String id); @PUT @Path("/customers/") Response updateCustomer(Customer customer); } How to invoke the REST service through camel-cxfrs producerCXF JAXRS front end implements a proxy based client API, with this API you can invoke the remote REST service through a proxy. Here is an example Exchange exchange = template.send("direct://proxy", new Processor() { public void process(Exchange exchange) throws Exception { exchange.setPattern(ExchangePattern.InOut); Message inMessage = exchange.getIn(); setupDestinationURL(inMessage); // set the operation name inMessage.setHeader(CxfConstants.OPERATION_NAME, "getCustomer"); // using the proxy client API inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.FALSE); // set a customer header inMessage.setHeader("key", "value"); // set the parameters , if you just have one parameter // camel will put this object into an Object[] itself inMessage.setBody("123"); } }); // get the response message Customer response = (Customer) exchange.getOut().getBody(); assertNotNull("The response should not be null ", response); assertEquals("Get a wrong customer id ", String.valueOf(response.getId()), "123"); assertEquals("Get a wrong customer name", response.getName(), "John"); assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); assertEquals("Get a wrong header value", "value", exchange.getOut().getHeader("key")); CXF JAXRS front end also provides a http centric client API, You can also invoke this API from camel-cxfrs producer. You need to specify the HTTP_PATH and Http method and let the the producer know to use the http centric client by using the URI option httpClientAPI or set the message header with CxfConstants.CAMEL_CXF_RS_USING_HTTP_API. You can turn the response object to the type class that you specify with CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS. Exchange exchange = template.send("direct://http", new Processor() { public void process(Exchange exchange) throws Exception { exchange.setPattern(ExchangePattern.InOut); Message inMessage = exchange.getIn(); setupDestinationURL(inMessage); // using the http central client API inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.TRUE); // set the Http method inMessage.setHeader(Exchange.HTTP_METHOD, "GET"); // set the relative path inMessage.setHeader(Exchange.HTTP_PATH, "/customerservice/customers/123"); // Specify the response class , cxfrs will use InputStream as the response object type inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Customer.class); // set a customer header inMessage.setHeader("key", "value"); // since we use the Get method, so we don't need to set the message body inMessage.setBody(null); } }); // get the response message Customer response = (Customer) exchange.getOut().getBody(); assertNotNull("The response should not be null ", response); assertEquals("Get a wrong customer id ", String.valueOf(response.getId()), "123"); assertEquals("Get a wrong customer name", response.getName(), "John"); assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); assertEquals("Get a wrong header value", "value", exchange.getOut().getHeader("key")); From Camel 2.1, we also support to specify the query parameters from cxfrs URI for the CXFRS http centric client. Exchange exchange = template.send("cxfrs://http://localhost:" + getPort2() + "/" + getClass().getSimpleName() + "/testQuery?httpClientAPI=true&q1=12&q2=13"
Map<String, String> queryMap = new LinkedHashMap<String, String>(); queryMap.put("q1", "new"); queryMap.put("q2", "world"); inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_QUERY_MAP, queryMap); DataSet ComponentTesting 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 DataSet component provides a mechanism to easily perform load & soak testing of your system. It works by allowing you to create DataSet instances both as a source of messages and as a way to assert that the data set is received. Camel will use the throughput logger when sending dataset's. URI formatdataset:name[?options] Where name is used to find the DataSet instance in the Registry Camel ships with a support implementation of org.apache.camel.component.dataset.DataSet, the org.apache.camel.component.dataset.DataSetSupport class, that can be used as a base for implementing your own DataSet. Camel also ships with a default implementation, the org.apache.camel.component.dataset.SimpleDataSet that can be used for testing. Options
You can append query options to the URI in the following format, ?option=value&option=value&... Configuring DataSetCamel will lookup in the Registry for a bean implementing the DataSet interface. So you can register your own DataSet as: <bean id="myDataSet" class="com.mycompany.MyDataSet"> <property name="size" value="100"/> </bean> ExampleFor example, to test that a set of messages are sent to a queue and then consumed from the queue without losing any messages: // send the dataset to a queue from("dataset:foo").to("activemq:SomeQueue"); // now lets test that the messages are consumed correctly from("activemq:SomeQueue").to("dataset:foo"); The above would look in the Registry to find the foo DataSet instance which is used to create the messages. Then you create a DataSet implementation, such as using the SimpleDataSet as described below, configuring things like how big the data set is and what the messages look like etc. Properties on SimpleDataSet
See AlsoDb4o ComponentAvailable as of Camel 2.5 The db4o: component allows you to work with db4o NoSQL database. The camel-db4o library is provided by the Camel Extra project which hosts all *GPL related components for Camel. Sending to the endpointSending POJO object to the db4o endpoint adds and saves object into the database. The body of the message is assumed to be a POJO that has to be saved into the db40 database store. Consuming from the endpointConsuming messages removes (or updates) POJO objects in the database. This allows you to use a Db4o datastore as a logical queue; consumers take messages from the queue and then delete them to logically remove them from the queue. If you do not wish to delete the object when it has been processed, you can specify consumeDelete=false on the URI. This will result in the POJO being processed each poll. URI formatdb4o:className[?options] You can append query options to the URI in the following format, ?option=value&option=value&... Options
See AlsoDirect ComponentThe direct: component provides direct, synchronous invocation of any consumers when a producer sends a message exchange.
URI formatdirect:someName[?options] Where someName can be any string to uniquely identify the endpoint Options
You can append query options to the URI in the following format, ?option=value&option=value&... SamplesIn 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"); 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"/> </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. See AlsoDNSAvailable as of Camel 2.7 This is an additional component for Camel to run DNS queries, using DNSJava. The component is a thin layer on top of DNSJava.
Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-dns</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI formatThe URI scheme for a DNS component is as follows
dns://operation
This component only supports producers. OptionsNone. Headers
ExamplesIP lookup
<route id="IPCheck">
<from uri="direct:start"/>
<to uri="dns:ip"/>
</route>
This looks up a domain's IP. For example, www.example.com resolves to 192.0.32.10. DNS lookup
<route id="IPCheck">
<from uri="direct:start"/>
<to uri="dns:lookup"/>
</route>
This returns a set of DNS records associated with a domain. DNS DigDig is a Unix command-line utility to run DNS queries.
<route id="IPCheck">
<from uri="direct:start"/>
<to uri="dns:dig"/>
</route>
The query must be provided in the header with key "dns.query". See AlsoEJB ComponentAvailable as of Camel 2.4 The ejb: component binds EJBs to Camel message exchanges. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-ejb</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI formatejb:ejbName[?options] Where ejbName can be any string which is used to look up the EJB in the Application Server JNDI Registry Options
You can append query options to the URI in the following format, ?option=value&option=value&... The EJB component extends the Bean component in which most of the details from the Bean component applies to this component as well. Bean BindingHow 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. ExamplesIn the following examples we use the Greater EJB which is defined as follows: GreaterLocal.java public interface GreaterLocal { String hello(String name); String bye(String name); } And the implementation GreaterImpl.java @Stateless public class GreaterImpl implements GreaterLocal { public String hello(String name) { return "Hello " + name; } public String bye(String name) { return "Bye " + name; } } Using Java DSLIn this example we want to invoke the hello method on the EJB. Since this example is based on an unit test using Apache OpenEJB we have to set a JndiContext on the EJB component with the OpenEJB settings. @Override protected CamelContext createCamelContext() throws Exception { CamelContext answer = new DefaultCamelContext(); // enlist EJB component using the JndiContext EjbComponent ejb = answer.getComponent("ejb", EjbComponent.class); ejb.setContext(createEjbContext()); return answer; } private static Context createEjbContext() throws NamingException { // here we need to define our context factory to use OpenEJB for our testing Properties properties = new Properties(); properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory"); return new InitialContext(properties); } Then we are ready to use the EJB in the Camel route: from("direct:start") // invoke the greeter EJB using the local interface and invoke the hello method .to("ejb:GreaterImplLocal?method=hello") .to("mock:result");
Using Spring XMLAnd this is the same example using Spring XML instead: Again since this is based on an unit test we need to setup the EJB component: <!-- setup Camel EJB component --> <bean id="ejb" class="org.apache.camel.component.ejb.EjbComponent"> <property name="properties" ref="jndiProperties"/> </bean> <!-- use OpenEJB context factory --> <p:properties id="jndiProperties"> <prop key="java.naming.factory.initial">org.apache.openejb.client.LocalInitialContextFactory</prop> </p:properties> Before we are ready to use EJB in the Camel routes: <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:start"/> <to uri="ejb:GreaterImplLocal?method=hello"/> <to uri="mock:result"/> </route> </camelContext> See AlsoEsperThe 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 formatesper:name[?options] When consuming from an Esper endpoint you must specify a pattern or eql statement to query the event stream. For example from("esper://cheese?pattern=every event=MyEvent(bar=5)"). to("activemq:Foo"); Options
You can append query options to the URI in the following format, ?option=value&option=value&... DemoThere is a demo which shows how to work with ActiveMQ, Camel and Esper in the Camel Extra project See AlsoEvent ComponentThe event: component provides access to the Spring ApplicationEvent objects. This allows you to publish ApplicationEvent objects to a Spring ApplicationContext or to consume them. You can then use Enterprise Integration Patterns to process them such as Message Filter. URI format
spring-event://default
See AlsoFile ComponentThe File component provides access to file systems, allowing files to be processed by any other Camel Components or messages from other components to be saved to disk. URI formatfile:directoryName[?options] or
file://directoryName[?options]
Where directoryName represents the underlying file directory. You can append query options to the URI in the following format, ?option=value&option=value&...
URI OptionsCommon
Consumer
Default behavior for file consumer
Producer
Default behavior for file producer
Move and Delete operationsAny move or delete operations is executed after (post command) the routing has completed; so during processing of the Exchange the file is still located in the inbox folder. Lets illustrate this with an example:
from("file://inbox?move=.done").to("bean:handleOrder");
When a file is dropped in the inbox folder, the file consumer notices this and creates a new FileExchange that is routed to the handleOrder bean. The bean then processes the File object. At this point in time the file is still located in the inbox folder. After the bean completes, and thus the route is completed, the file consumer will perform the move operation and move the file to the .done sub-folder. The move and preMove options should be a directory name, which can be either relative or absolute. If relative, the directory is created as a sub-folder from within the folder where the file was consumed. By default, Camel will move consumed files to the .camel sub-folder relative to the directory where the file was consumed. If you want to delete the file after processing, the route should be:
from("file://inobox?delete=true").to("bean:handleOrder");
We have introduced a pre move operation to move files before they are processed. This allows you to mark which files have been scanned as they are moved to this sub folder before being processed.
from("file://inbox?preMove=inprogress").to("bean:handleOrder");
You can combine the pre move and the regular move:
from("file://inbox?preMove=inprogress&move=.done").to("bean:handleOrder");
So in this situation, the file is in the inprogress folder when being processed and after it's processed, it's moved to the .done folder. Fine grained control over Move and PreMove optionThe move and preMove option is Expression-based, so we have the full power of the File Language to do advanced configuration of the directory and name pattern. So if we want to move the file into a backup folder with today's date as the pattern, we can do:
move=backup/${date:now:yyyyMMdd}/${file:name}
About moveFailedThe moveFailed option allows you to move files that could not be processed succesfully to another location such as a error folder of your choice. For example to move the files in an error folder with a timestamp you can use moveFailed=/error/${file:name.noext}-${date:now:yyyyMMddHHmmssSSS}.${file:ext}. See more examples at File Language Message HeadersThe following headers are supported by this component: File producer only
File consumer only
Batch ConsumerThis component implements the Batch Consumer. Exchange Properties, file consumer onlyAs the file consumer is BatchConsumer it supports batching the files it polls. By batching it means that Camel will add some properties to the Exchange so you know the number of files polled the current index in that order.
This allows you for instance to know how many files exists in this batch and for instance let the Aggregator2 aggregate this number of files. Using charsetAvailable as of Camel 2.9.3 from("file:inbox?charset=utf-8") .to("file:outbox?charset=iso-8859-1") You can also use the convertBodyTo in the route. In the example below we have still input files in utf-8 format, but we want to convert the file content to a byte array in iso-8859-1 format. And then let a bean process the data. Before writing the content to the outbox folder using the current charset. from("file:inbox?charset=utf-8") .convertBodyTo(byte[].class, "iso-8859-1") .to("bean:myBean") .to("file:outbox"); If you omit the charset on the consumer endpoint, then Camel does not know the charset of the file, and would by default use "UTF-8". However you can configure a JVM system property to override and use a different default encoding with the key org.apache.camel.default.charset. In the example below this could be a problem if the files is not in UTF-8 encoding, which would be the default encoding for read the files. from("file:inbox") .convertBodyTo(byte[].class, "iso-8859-1") .to("bean:myBean") .to("file:outbox"); You can also override and control the encoding dynamic when writing files, by setting a property on the exchange with the key Exchange.CHARSET_NAME. For example in the route below we set the property with a value from a message header. from("file:inbox") .convertBodyTo(byte[].class, "iso-8859-1") .to("bean:myBean") .setProperty(Exchange.CHARSET_NAME, header("someCharsetHeader")) .to("file:outbox"); We suggest to keep things simpler, so if you pickup files with the same encoding, and want to write the files in a specific encoding, then favor to use the charset option on the endpoints. Notice that if you have explicit configured a charset option on the endpoint, then that configuration is used, regardless of the Exchange.CHARSET_NAME property. If you have some issues then you can enable DEBUG logging on org.apache.camel.component.file, and Camel logs when it reads/write a file using a specific charset. from("file:inbox?charset=utf-8") .to("file:outbox?charset=iso-8859-1") And the logs: DEBUG GenericFileConverter - Read file /Users/davsclaus/workspace/camel/camel-core/target/charset/input/input.txt with charset utf-8 DEBUG FileOperations - Using Reader to write file: target/charset/output.txt with charset: iso-8859-1 Common gotchas with folder and filenamesWhen Camel is producing files (writing files) there are a few gotchas affecting how to set a filename of your choice. By default, Camel will use the message ID as the filename, and since the message ID is normally a unique generated ID, you will end up with filenames such as: ID-MACHINENAME-2443-1211718892437-1-0. If such a filename is not desired, then you must provide a filename in the CamelFileName message header. The constant, Exchange.FILE_NAME, can also be used. The sample code below produces files using the message ID as the filename: from("direct:report").to("file:target/reports"); To use report.txt as the filename you have to do: from("direct:report").setHeader(Exchange.FILE_NAME, constant("report.txt")).to( "file:target/reports"); ... the same as above, but with CamelFileName: from("direct:report").setHeader("CamelFileName", constant("report.txt")).to( "file:target/reports"); And a syntax where we set the filename on the endpoint with the fileName URI option. from("direct:report").to("file:target/reports/?fileName=report.txt"); Filename ExpressionFilename can be set either using the expression option or as a string-based File Language expression in the CamelFileName header. See the File Language for syntax and samples. Consuming files from folders where others drop files directlyBeware if you consume files from a folder where other applications write files directly. Take a look at the different readLock options to see what suits your use cases. The best approach is however to write to another folder and after the write move the file in the drop folder. However if you write files directly to the drop folder then the option changed could better detect whether a file is currently being written/copied as it uses a file changed algorithm to see whether the file size / modification changes over a period of time. The other read lock options rely on Java File API that sadly is not always very good at detecting this. You may also want to look at the doneFileName option, which uses a marker file (done) to signal when a file is done and ready to be consumed. Using done filesAvailable as of Camel 2.6 See also section writing done files below. If you want only to consume files when a done file exists, then you can use the doneFileName option on the endpoint.
from("file:bar?doneFileName=done");
Will only consume files from the bar folder, if a file name done exists in the same directory as the target files. Camel will automatically delete the done file when it's done consuming the files. From Camel 2.9.3 onwards Camel will not automatic delete the done file if noop=true is configured. However its more common to have one done file per target file. This means there is a 1:1 correlation. To do this you must use dynamic placeholders in the doneFileName option. Currently Camel supports the following two dynamic tokens: file:name and file:name.noext which must be enclosed in ${ }. The consumer only supports the static part of the done file name as either prefix or suffix (not both).
from("file:bar?doneFileName=${file:name}.done");
In this example only files will be polled if there exists a done file with the name file name.done. For example
You can also use a prefix for the done file, such as:
from("file:bar?doneFileName=ready-${file:name}");
Writing done filesAvailable as of Camel 2.6 After you have written af file you may want to write an additional done file as a kinda of marker, to indicate to others that the file is finished and has been written. To do that you can use the doneFileName option on the file producer endpoint.
.to("file:bar?doneFileName=done");
Will simply create a file named done in the same directory as the target file. However its more common to have one done file per target file. This means there is a 1:1 correlation. To do this you must use dynamic placeholders in the doneFileName option. Currently Camel supports the following two dynamic tokens: file:name and file:name.noext which must be enclosed in ${ }.
.to("file:bar?doneFileName=done-${file:name}");
Will for example create a file named done-foo.txt if the target file was foo.txt in the same directory as the target file.
.to("file:bar?doneFileName=${file:name}.done");
Will for example create a file named foo.txt.done if the target file was foo.txt in the same directory as the target file.
.to("file:bar?doneFileName=${file:name.noext}.done");
Will for example create a file named foo.done if the target file was foo.txt in the same directory as the target file. SamplesRead from a directory and write to another directory
from("file://inputdir/?delete=true").to("file://outputdir")
Read from a directory and write to another directory using a overrule dynamic name
from("file://inputdir/?delete=true").to("file://outputdir?overruleFile=copy-of-${file:name}")
Listen on a directory and create a message for each file dropped there. Copy the contents to the outputdir and delete the file in the inputdir. Reading recursively from a directory and writing to another
from("file://inputdir/?recursive=true&delete=true").to("file://outputdir")
Listen on a directory and create a message for each file dropped there. Copy the contents to the outputdir and delete the file in the inputdir. Will scan recursively into sub-directories. Will lay out the files in the same directory structure in the outputdir as the inputdir, including any sub-directories. inputdir/foo.txt inputdir/sub/bar.txt Will result in the following output layout: outputdir/foo.txt outputdir/sub/bar.txt Using flattenIf you want to store the files in the outputdir directory in the same directory, disregarding the source directory layout (e.g. to flatten out the path), you just add the flatten=true option on the file producer side:
from("file://inputdir/?recursive=true&delete=true").to("file://outputdir?flatten=true")
Will result in the following output layout: outputdir/foo.txt outputdir/bar.txt Reading from a directory and the default move operationCamel will by default move any processed file into a .camel subdirectory in the directory the file was consumed from.
from("file://inputdir/?recursive=true&delete=true").to("file://outputdir")
Affects the layout as follows: inputdir/foo.txt inputdir/sub/bar.txt after inputdir/.camel/foo.txt inputdir/sub/.camel/bar.txt outputdir/foo.txt outputdir/sub/bar.txt Read from a directory and process the message in javafrom("file://inputdir/").process(new Processor() { public void process(Exchange exchange) throws Exception { Object body = exchange.getIn().getBody(); // do some business logic with the input body } }); The body will be a File object that points to the file that was just dropped into the inputdir directory. Writing to filesCamel is of course also able to write files, i.e. produce files. In the sample below we receive some reports on the SEDA queue that we process before they are written to a directory. public void testToFile() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); mock.expectedFileExists("target/test-reports/report.txt"); template.sendBody("direct:reports", "This is a great report"); assertMockEndpointsSatisfied(); } protected JndiRegistry createRegistry() throws Exception { // bind our processor in the registry with the given id JndiRegistry reg = super.createRegistry(); reg.bind("processReport", new ProcessReport()); return reg; } protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { // the reports from the seda queue is processed by our processor // before they are written to files in the target/reports directory from("direct:reports").processRef("processReport").to("file://target/test-reports", "mock:result"); } }; } private static class ProcessReport implements Processor { public void process(Exchange exchange) throws Exception { String body = exchange.getIn().getBody(String.class); // do some business logic here // set the output to the file exchange.getOut().setBody(body); // set the output filename using java code logic, notice that this is done by setting // a special header property of the out exchange exchange.getOut().setHeader(Exchange.FILE_NAME, "report.txt"); } } Write to subdirectory using Exchange.FILE_NAMEUsing a single route, it is possible to write a file to any number of subdirectories. If you have a route setup as such:
<route>
<from uri="bean:myBean"/>
<to uri="file:/rootDirectory"/>
</route>
You can have myBean set the header Exchange.FILE_NAME to values such as: Exchange.FILE_NAME = hello.txt => /rootDirectory/hello.txt Exchange.FILE_NAME = foo/bye.txt => /rootDirectory/foo/bye.txt This allows you to have a single route to write files to multiple destinations. Using expression for filenamesIn this sample we want to move consumed files to a backup folder using today's date as a sub-folder name:
from("file://inbox?move=backup/${date:now:yyyyMMdd}/${file:name}").to("...");
See File Language for more samples. Avoiding reading the same file more than once (idempotent consumer)Camel supports Idempotent Consumer directly within the component so it will skip already processed files. This feature can be enabled by setting the idempotent=true option.
from("file://inbox?idempotent=true").to("...");
Camel uses the absolute file name as the idempotent key, to detect duplicate files. From Camel 2.11 onwards you can customize this key by using an expression in the idempotentKey option. For example to use both the name and the file size as the key <route> <from uri="file://inbox?idempotent=true&idempotentKey=${file:name}-${file-size}"/> <to uri="bean:processInbox"/> </route> By default Camel uses a in memory based store for keeping track of consumed files, it uses a least recently used cache holding up to 1000 entries. You can plugin your own implementation of this store by using the idempotentRepository option using the # sign in the value to indicate it's a referring to a bean in the Registry with the specified id. <!-- define our store as a plain spring bean --> <bean id="myStore" class="com.mycompany.MyIdempotentStore"/> <route> <from uri="file://inbox?idempotent=true&idempotentRepository=#myStore"/> <to uri="bean:processInbox"/> </route> Camel will log at DEBUG level if it skips a file because it has been consumed before:
DEBUG FileConsumer is idempotent and the file has been consumed before. Will skip this file: target\idempotent\report.txt
Using a file based idempotent repositoryIn this section we will use the file based idempotent repository org.apache.camel.processor.idempotent.FileIdempotentRepository instead of the in-memory based that is used as default. We configure our repository using Spring XML creating our file idempotent repository and define our file consumer to use our repository with the idempotentRepository using # sign to indicate Registry lookup: <!-- this is our file based idempotent store configured to use the .filestore.dat as file --> <bean id="fileStore" class="org.apache.camel.processor.idempotent.FileIdempotentRepository"> <!-- the filename for the store --> <property name="fileStore" value="target/fileidempotent/.filestore.dat"/> <!-- the max filesize in bytes for the file. Camel will trunk and flush the cache if the file gets bigger --> <property name="maxFileStoreSize" value="512000"/> <!-- the number of elements in our store --> <property name="cacheSize" value="250"/> </bean> <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="file://target/fileidempotent/?idempotent=true&idempotentRepository=#fileStore&move=done/${file:name}"/> <to uri="mock:result"/> </route> </camelContext> Using a JPA based idempotent repositoryIn this section we will use the JPA based idempotent repository instead of the in-memory based that is used as default. First we need a persistence-unit in META-INF/persistence.xml where we need to use the class org.apache.camel.processor.idempotent.jpa.MessageProcessed as model. <persistence-unit name="idempotentDb" transaction-type="RESOURCE_LOCAL"> <class>org.apache.camel.processor.idempotent.jpa.MessageProcessed</class> <properties> <property name="openjpa.ConnectionURL" value="jdbc:derby:target/idempotentTest;create=true"/> <property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver"/> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/> <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/> </properties> </persistence-unit> Then we need to setup a Spring jpaTemplate in the spring XML file: <!-- this is standard spring JPA configuration --> <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <!-- we use idempotentDB as the persitence unit name defined in the persistence.xml file --> <property name="persistenceUnitName" value="idempotentDb"/> </bean> And finally we can create our JPA idempotent repository in the spring XML file as well: <!-- we define our jpa based idempotent repository we want to use in the file consumer --> <bean id="jpaStore" class="org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository"> <!-- Here we refer to the spring jpaTemplate --> <constructor-arg index="0" ref="jpaTemplate"/> <!-- This 2nd parameter is the name (= a cateogry name). You can have different repositories with different names --> <constructor-arg index="1" value="FileConsumer"/> </bean> And yes then we just need to refer to the jpaStore bean in the file consumer endpoint using the idempotentRepository using the # syntax option: <route> <from uri="file://inbox?idempotent=true&idempotentRepository=#jpaStore"/> <to uri="bean:processInbox"/> </route> Filter using org.apache.camel.component.file.GenericFileFilterCamel supports pluggable filtering strategies. You can then configure the endpoint with such a filter to skip certain files being processed. In the sample we have built our own filter that skips files starting with skip in the filename: public class MyFileFilter<T> implements GenericFileFilter<T> { public boolean accept(GenericFile<T> file) { // we want all directories if (file.isDirectory()) { return true; } // we dont accept any files starting with skip in the name return !file.getFileName().startsWith("skip"); } } 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: <!-- define our sorter as a plain spring bean --> <bean id="myFilter" class="com.mycompany.MyFileSorter"/> <route> <from uri="file://inbox?filter=#myFilter"/> <to uri="bean:processInbox"/> </route> Filtering using ANT path matcher
The ANT path matcher 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 file paths is matched with the following rules:
The sample below demonstrates how to use it: <camelContext xmlns="http://camel.apache.org/schema/spring"> <template id="camelTemplate"/> <!-- use myFilter as filter to allow setting ANT paths for which files to scan for --> <endpoint id="myFileEndpoint" uri="file://target/antpathmatcher?recursive=true&filter=#myAntFilter"/> <route> <from ref="myFileEndpoint"/> <to uri="mock:result"/> </route> </camelContext> <!-- we use the antpath file filter to use ant paths for includes and exlucde --> <bean id="myAntFilter" class="org.apache.camel.component.file.AntPathMatcherGenericFileFilter"> <!-- include and file in the subfolder that has day in the name --> <property name="includes" value="**/subfolder/**/*day*"/> <!-- exclude all files with bad in name or .xml files. Use comma to seperate multiple excludes --> <property name="excludes" value="**/*bad*,**/*.xml"/> </bean> Sorting using ComparatorCamel supports pluggable sorting strategies. This strategy it to use the build in java.util.Comparator in Java. You can then configure the endpoint with such a comparator and have Camel sort the files before being processed. In the sample we have built our own comparator that just sorts by file name: public class MyFileSorter<T> implements Comparator<GenericFile<T>> { public int compare(GenericFile<T> o1, GenericFile<T> o2) { return o1.getFileName().compareToIgnoreCase(o2.getFileName()); } } And then we can configure our route using the sorter option to reference to our sorter (mySorter) we have defined in the spring XML file: <!-- define our sorter as a plain spring bean --> <bean id="mySorter" class="com.mycompany.MyFileSorter"/> <route> <from uri="file://inbox?sorter=#mySorter"/> <to uri="bean:processInbox"/> </route>
Sorting using sortByCamel supports pluggable sorting strategies. This strategy it to use the File Language to configure the sorting. The sortBy option is configured as follows: sortBy=group 1;group 2;group 3;... Where each group is separated with semi colon. In the simple situations you just use one group, so a simple example could be: sortBy=file:name This will sort by file name, you can reverse the order by prefixing reverse: to the group, so the sorting is now Z..A: sortBy=reverse:file:name As we have the full power of File Language we can use some of the other parameters, so if we want to sort by file size we do: sortBy=file:length You can configure to ignore the case, using ignoreCase: for string comparison, so if you want to use file name sorting but to ignore the case then we do: sortBy=ignoreCase:file:name You can combine ignore case and reverse, however reverse must be specified first: sortBy=reverse:ignoreCase:file:name In the sample below we want to sort by last modified file, so we do: sortBy=file:modifed And then we want to group by name as a 2nd option so files with same modifcation is sorted by name: sortBy=file:modifed;file:name Now there is an issue here, can you spot it? Well the modified timestamp of the file is too fine as it will be in milliseconds, but what if we want to sort by date only and then subgroup by name? sortBy=date:file:yyyyMMdd;file:name Yeah, that is pretty powerful, oh by the way you can also use reverse per group, so we could reverse the file names: sortBy=date:file:yyyyMMdd;reverse:file:name Using GenericFileProcessStrategyThe option processStrategy can be used to use a custom GenericFileProcessStrategy that allows you to implement your own begin, commit and rollback logic. So by implementing our own GenericFileProcessStrategy we can implement this as:
Using filterThe filter option allows you to implement a custom filter in Java code by implementing the org.apache.camel.component.file.GenericFileFilter interface. This interface has an accept method that returns a boolean. Return true to include the file, and false to skip the file. From Camel 2.10 onwards, there is a isDirectory method on GenericFile whether the file is a directory. This allows you to filter unwanted directories, to avoid traversing down unwanted directories. For example to skip any directories which starts with "skip" in the name, can be implemented as follows: public class MyDirectoryFilter<T> implements GenericFileFilter<T> { public boolean accept(GenericFile<T> file) { // remember the name due unit testing (should not be needed in regular use-cases) names.add(file.getFileName()); // we dont accept any files within directory starting with skip in the name if (file.isDirectory() && file.getFileName().startsWith("skip")) { return false; } return true; } } How to use the Camel error handler to deal with exceptions triggered outside the routing engineThe file and ftp consumers, will by default try to pickup files. Only if that is successful then a Camel Exchange can be created and passed in the Camel routing engine.
Here is such an example based upon an unit test. First we have a custom ExceptionHandler where you can see we deal with the exception by sending it to a Camel Endpoint named "direct:file-error": MyExceptionHandler /**
* Custom {@link ExceptionHandler} to be used on the file consumer, to send
* exceptions to a Camel route, to let Camel deal with the error.
*/
private static class MyExceptionHandler implements ExceptionHandler {
private ProducerTemplate template;
/**
* We use a producer template to send a message to the Camel route
*/
public void setTemplate(ProducerTemplate template) {
this.template = template;
}
@Override
public void handleException(Throwable exception) {
handleException(exception.getMessage(), exception);
}
@Override
public void handleException(String message, Throwable exception) {
handleException(exception.getMessage(), null, exception);
}
@Override
public void handleException(final String message, final Exchange originalExchange, final Throwable exception) {
// send the message to the special direct:file-error endpoint, which will trigger exception handling
//
template.send("direct:file-error", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
// set an exception on the message from the start so the error handling is triggered
exchange.setException(exception);
exchange.getIn().setBody(message);
}
});
}
}
Then we have a Camel route that uses the Camel routing error handler, which is the onException where we handle any IOException being thrown. Notice how we configure our custom MyExceptionHandler by using the consumer.exceptionHandler option to refer to #myExceptionHandler which is a id of the bean registered in the Registry. If using Spring XML or OSGi Blueprint, then that would be a <bean id="myExceptionHandler" class="com.foo.MyExceptionHandler"/>: Camel route with routing engine error handling @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { // to handle any IOException being thrown onException(IOException.class) .handled(true) .log("IOException occurred due: ${exception.message}") // as we handle the exception we can send it to direct:file-error, // where we could send out alerts or whatever we want .to("direct:file-error"); // special route that handles file errors from("direct:file-error") .log("File error route triggered to deal with exception ${exception?.class}") // as this is based on unit test just transform a message and send it to a mock .transform().simple("Error ${exception.message}") .to("mock:error"); // this is the file route that pickup files, notice how we use our custom exception handler on the consumer // the exclusiveReadLockStrategy is only configured because this is from an unit test, so we use that to simulate exceptions from("file:target/nospace?exclusiveReadLockStrategy=#myReadLockStrategy&consumer.exceptionHandler=#myExceptionHandler") .convertBodyTo(String.class) .to("mock:result"); } }; } The source code for this example can be seen here Using consumer.bridgeErrorHandlerAvailable as of Camel 2.10 If you want to use the Camel Error Handler to deal with any exception occurring in the file consumer, then you can enable the consumer.bridgeErrorHandler option as shown below: Using consumer.bridgeErrorHandler @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { // to handle any IOException being thrown onException(IOException.class) .handled(true) .log("IOException occurred due: ${exception.message}") .transform().simple("Error ${exception.message}") .to("mock:error"); // this is the file route that pickup files, notice how we bridge the consumer to use the Camel routing error handler // the exclusiveReadLockStrategy is only configured because this is from an unit test, so we use that to simulate exceptions from("file:target/nospace?exclusiveReadLockStrategy=#myReadLockStrategy&consumer.bridgeErrorHandler=true") .convertBodyTo(String.class) .to("mock:result"); } }; } So all you have to do is to enable this option, and the error handler in the route will take it from there. Debug loggingThis component has log level TRACE that can be helpful if you have problems. See AlsoFlatpack ComponentThe Flatpack component supports fixed width and delimited file parsing via the FlatPack library. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-flatpack</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI formatflatpack:[delim|fixed]:flatPackConfig.pzmap.xml[?options] Or for a delimited file handler with no configuration file just use flatpack:someName[?options] You can append query options to the URI in the following format, ?option=value&option=value&... URI Options
Examples
Message HeadersCamel will store the following headers on the IN message:
Message BodyThe component delivers the data in the IN message as a org.apache.camel.component.flatpack.DataSetList object that has converters for java.util.Map or java.util.List. For example to get the firstname from the sample below: Map row = exchange.getIn().getBody(Map.class); String firstName = row.get("FIRSTNAME"); However, you can also always get it as a List (even for splitRows=true). The same example: List data = exchange.getIn().getBody(List.class); Map row = (Map)data.get(0); String firstName = row.get("FIRSTNAME"); Header and Trailer recordsThe header and trailer notions in Flatpack are supported. However, you must use fixed record IDs:
The example below illustrates this fact that we have a header and a trailer. You can omit one or both of them if not needed.
<RECORD id="header" startPosition="1" endPosition="3" indicator="HBT">
<COLUMN name="INDICATOR" length="3"/>
<COLUMN name="DATE" length="8"/>
</RECORD>
<COLUMN name="FIRSTNAME" length="35" />
<COLUMN name="LASTNAME" length="35" />
<COLUMN name="ADDRESS" length="100" />
<COLUMN name="CITY" length="100" />
<COLUMN name="STATE" length="2" />
<COLUMN name="ZIP" length="5" />
<RECORD id="trailer" startPosition="1" endPosition="3" indicator="FBT">
<COLUMN name="INDICATOR" length="3"/>
<COLUMN name="STATUS" length="7"/>
</RECORD>
Using the endpointA common use case is sending a file to this endpoint for further processing in a separate route. For example: <camelContext xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="file://someDirectory"/> <to uri="flatpack:foo"/> </route> <route> <from uri="flatpack:foo"/> ... </route> </camelContext> You can also convert the payload of each message created to a Map for easy Bean Integration Flatpack DataFormatThe Flatpack component ships with the Flatpack data format that can be used to format between fixed width or delimited text messages to a List of rows as Map.
Notice: The Flatpack library does currently not support header and trailers for the marshal operation. OptionsThe data format has the following options:
UsageTo use the data format, simply instantiate an instance and invoke the marshal or unmarshal operation in the route builder: FlatpackDataFormat fp = new FlatpackDataFormat(); fp.setDefinition(new ClassPathResource("INVENTORY-Delimited.pzmap.xml")); ... from("file:order/in").unmarshal(df).to("seda:queue:neworder"); The sample above will read files from the order/in folder and unmarshal the input using the Flatpack configuration file INVENTORY-Delimited.pzmap.xml that configures the structure of the files. The result is a DataSetList object we store on the SEDA queue. FlatpackDataFormat df = new FlatpackDataFormat(); df.setDefinition(new ClassPathResource("PEOPLE-FixedLength.pzmap.xml")); df.setFixed(true); df.setIgnoreFirstRecord(false); from("seda:people").marshal(df).convertBodyTo(String.class).to("jms:queue:people"); In the code above we marshal the data from a Object representation as a List of rows as Maps. The rows as Map contains the column name as the key, and the the corresponding value. This structure can be created in Java code from e.g. a processor. We marshal the data according to the Flatpack format and convert the result as a String object and store it on a JMS queue. DependenciesTo use Flatpack in your camel routes you need to add the a dependency on camel-flatpack which implements this data format. If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions). <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-flatpack</artifactId> <version>x.x.x</version> </dependency> See AlsoFreeMarkerThe freemarker: component allows for processing a message using a FreeMarker 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: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-freemarker</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI formatfreemarker: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.ftl). You can append query options to the URI in the following format, ?option=value&option=value&... Options
HeadersHeaders set during the FreeMarker evaluation are returned to the message and added as headers. This provides a mechanism for the FreeMarker component to return values to the Message. An example: Set the header value of fruit in the FreeMarker template:
${request.setHeader('fruit', 'Apple')}
The header, fruit, is now accessible from the message.out.headers. FreeMarker ContextCamel will provide exchange information in the FreeMarker context (just a Map). The Exchange is transferred as:
Hot reloadingThe FreeMarker template resource is by default not hot reloadable for both file and classpath resources (expanded jar). If you set contentCache=false, then Camel will not cache the resource and hot reloading is thus enabled. This scenario can be used in development. Dynamic templatesCamel 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.
SamplesFor example you could use something like: from("activemq:My.Queue"). to("freemarker:com/acme/MyResponse.ftl"); To use a FreeMarker 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: from("activemq:My.Queue"). to("freemarker:com/acme/MyResponse.ftl"). to("activemq:Another.Queue"); And to disable the content cache, e.g. for development usage where the .ftl template should be hot reloaded: from("activemq:My.Queue"). to("freemarker:com/acme/MyResponse.ftl?contentCache=false"). to("activemq:Another.Queue"); And a file-based resource: from("activemq:My.Queue"). to("freemarker:file://myfolder/MyResponse.ftl?contentCache=false"). 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(FreemarkerConstants.FREEMARKER_RESOURCE_URI).constant("path/to/my/template.ftl"). to("freemarker:dummy"); The Email SampleIn this sample we want to use FreeMarker templating for an order confirmation email. The email template is laid out in FreeMarker as:
Dear ${headers.lastName}, ${headers.firstName}
Thanks for the order of ${headers.item}.
Regards Camel Riders Bookstore
${body}
And the java code: private Exchange createLetter() { Exchange exchange = context.getEndpoint("direct:a").createExchange(); Message msg = exchange.getIn(); msg.setHeader("firstName", "Claus"); msg.setHeader("lastName", "Ibsen"); msg.setHeader("item", "Camel in Action"); msg.setBody("PS: Next beer is on me, James"); return exchange; } @Test public void testFreemarkerLetter() throws Exception { MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedMessageCount(1); mock.expectedBodiesReceived("Dear Ibsen, Claus\n\nThanks for the order of Camel in Action." + "\n\nRegards Camel Riders Bookstore\nPS: Next beer is on me, James"); template.send("direct:a", createLetter()); mock.assertIsSatisfied(); } protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { from("direct:a") .to("freemarker:org/apache/camel/component/freemarker/letter.ftl") .to("mock:result"); } }; } See AlsoFTP/SFTP/FTPS ComponentThis 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: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-ftp</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency>
URI formatftp://[username@]hostname[:port]/directoryname[?options] sftp://[username@]hostname[:port]/directoryname[?options] ftps://[username@]hostname[:port]/directoryname[?options] Where directoryname represents the underlying directory. Can contain nested folders. If no username is provided, then anonymous login is attempted using no password. 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. URI OptionsThe options below are exclusive for the FTP component.
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. 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
Examplesftp://someone@someftpserver.com/public/upload/images/holiday2008?password=secret&binary=true
Default when consuming filesThe 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. limitationsThe option readLock can be used to force Camel not to consume files that is currently in the progress of 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. 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 HeadersThe following message headers can be used to affect the behavior of the component
In addition the FTP/FTPS consumer and producer will enrich the Camel Message with the following headers
About timeoutsThe two set of libraries (see top) has different API 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 DirectoryCamel 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:
from("ftp://someone@someserver.com?password=secret&localWorkDirectory=/tmp").to("file://inbox");
Stepwise changing directoriesCamel FTP can operate in two modes in terms of traversing directories when consuming files (eg downloading) or producing files (eg uploading)
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:
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=false230 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. SamplesIn 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. protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { // we use a delay of 60 minutes (eg. once pr. hour we poll the FTP server long delay = 60 * 60 * 1000L; // from the given FTP server we poll (= download) all the files // from the public/reports folder as BINARY types and store this as files // in a local directory. Camel will use the filenames from the FTPServer // notice that the FTPConsumer properties must be prefixed with "consumer." in the URL // the delay parameter is from the FileConsumer component so we should use consumer.delay as // the URI parameter name. The FTP Component is an extension of the File Component. from("ftp://tiger:scott@localhost/public/reports?binary=true&consumer.delay=" + delay). to("file://target/test-reports"); } }; } And the route using Spring DSL: <route> <from uri="ftp://scott@localhost/public/reports?password=tiger&binary=true&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 configurationfrom("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.GenericFileFilterCamel supports pluggable filtering strategies. This strategy it to use the build in 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. public class MyFileFilter<T> implements GenericFileFilter<T> { public boolean accept(GenericFile<T> file) { // we only want report files return file.getFileName().startsWith("report"); } } 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: <!-- define our sorter as a plain spring bean --> <bean id="myFilter" class="com.mycompany.MyFileFilter"/> <route> <from uri="ftp://someuser@someftpserver.com?password=secret&filter=#myFilter"/> <to uri="bean:processInbox"/> </route> Filtering using ANT path matcherThe 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 file paths are matched with the following rules:
The sample below demonstrates how to use it: <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/> <camelContext xmlns="http://camel.apache.org/schema/spring"> <template id="camelTemplate"/> <!-- use myFilter as filter to allow setting ANT paths for which files to scan for --> <endpoint id="myFTPEndpoint" uri="ftp://admin@localhost:${SpringFileAntPathMatcherRemoteFileFilterTest.ftpPort}/antpath?password=admin&recursive=true&delay=10000&initialDelay=2000&filter=#myAntFilter"/> <route> <from ref="myFTPEndpoint"/> <to uri="mock:result"/> </route> </camelContext> <!-- we use the AntPathMatcherRemoteFileFilter to use ant paths for includes and exclude --> <bean id="myAntFilter" class="org.apache.camel.component.file.AntPathMatcherGenericFileFilter"> <!-- include any files in the sub folder that has day in the name --> <property name="includes" value="**/subfolder/**/*day*"/> <!-- exclude all files with bad in name or .xml files. Use comma to separate multiple excludes --> <property name="excludes" value="**/*bad*,**/*.xml"/> </bean> Debug loggingThis component has log level TRACE that can be helpful if you have problems. See AlsoCamel Components for Google App Engine
The Camel components for Google App Engine (GAE) are part of the camel-gae project and provide connectivity to GAE's cloud computing services. They make the GAE cloud computing environment accessible to applications via Camel interfaces. Following this pattern for other cloud computing environments could make it easier to port Camel applications from one cloud computing provider to another. The following table lists the cloud computing services provided by Google and the supporting Camel components. The documentation of each component can be found by following the link in the Camel Component column.
Camel contextSetting up a SpringCamelContext on Google App Engine differs between Camel 2.1 and higher versions. The problem is that usage of the Camel-specific Spring configuration XML schema from the http://camel.apache.org/schema/spring namespace requires JAXB and Camel 2.1 depends on a Google App Engine SDK version that doesn't support JAXB yet. This limitation has been removed since Camel 2.2. JMX must be disabled in any case because the javax.management package isn't on the App Engine JRE whitelist. Camel 2.1camel-gae 2.1 comes with the following CamelContext implementations.
Both disable JMX before startup. The GaeSpringCamelContext additionally provides setter methods adding route builders as shown in the next example. appctx.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="camelContext" class="org.apache.camel.component.gae.context.GaeSpringCamelContext"> <property name="routeBuilder" ref="myRouteBuilder" /> </bean> <bean id="myRouteBuilder" class="org.example.MyRouteBuilder"> </bean> </beans> Alternatively, use the routeBuilders property of the GaeSpringCamelContext for setting a list of route builders. Using this approach, a SpringCamelContext can be configured on GAE without the need for JAXB. Camel 2.2 or higherWith Camel 2.2 or higher, applications can use the http://camel.apache.org/schema/spring namespace for configuring a SpringCamelContext but still need to disable JMX. Here's an example. appctx.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <camel:camelContext id="camelContext"> <camel:jmxAgent id="agent" disabled="true" /> <camel:routeBuilder ref="myRouteBuilder"/> </camel:camelContext> <bean id="myRouteBuilder" class="org.example.MyRouteBuilder"> </bean> </beans> The web.xmlRunning Camel on GAE requires usage of the CamelHttpTransportServlet from camel-servlet. The following example shows how to configure this servlet together with a Spring application context XML file. web.xml <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>CamelServlet</servlet-name> <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>appctx.xml</param-value> </init-param> </servlet> <!-- Mapping used for external requests --> <servlet-mapping> <servlet-name>CamelServlet</servlet-name> <url-pattern>/camel/*</url-pattern> </servlet-mapping> <!-- Mapping used for web hooks accessed by task queueing service. --> <servlet-mapping> <servlet-name>CamelServlet</servlet-name> <url-pattern>/worker/*</url-pattern> </servlet-mapping> </web-app> The location of the Spring application context XML file is given by the contextConfigLocation init parameter. The appctx.xml file must be on the classpath. The servlet mapping makes the Camel application accessible under http://<appname>.appspot.com/camel/... when deployed to Google App Engine where <appname> must be replaced by a real GAE application name. The second servlet mapping is used internally by the task queueing service for background processing via web hooks. This mapping is relevant for the gtask component and is explained there in more detail. Hazelcast ComponentAvailable as of Camel 2.7 The hazelcast: component allows you to work with the Hazelcast distributed data grid / cache. Hazelcast is a in memory data grid, entirely written in Java (single jar). It offers a great palette of different data stores like map, multi map (same key, n values), queue, list and atomic number. The main reason to use Hazelcast is its simple cluster support. If you have enabled multicast on your network you can run a cluster with hundred nodes with no extra configuration. Hazelcast can simply configured to add additional features like n copies between nodes (default is 1), cache persistence, network configuration (if needed), near cache, enviction and so on. For more information consult the Hazelcast documentation on http://www.hazelcast.com/docs.jsp. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-hazelcast</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI formathazelcast:[ map | multimap | queue | seda | set | atomicvalue | instance]:cachename[?options]
Sections
Usage of Mapmap cache producer - to("hazelcast:map:foo")If you want to store a value in a map you can use the map cache producer. The map cache producer provides 5 operations (put, get, update, delete, query). For the first 4 you have to provide the operation inside the "hazelcast.operation.type" header variable. In Java DSL you can use the constants from org.apache.camel.component.hazelcast.HazelcastConstants. Header Variables for the request message:
You can call the samples with: template.sendBodyAndHeader("direct:[put|get|update|delete|query]", "my-foo", HazelcastConstants.OBJECT_ID, "4711"); Sample for put:Java DSL: from("direct:put") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.PUT_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.MAP_PREFIX); Spring DSL: <route> <from uri="direct:put" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>put</constant> </setHeader> <to uri="hazelcast:map:foo" /> </route> Sample for get:Java DSL: from("direct:get") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.GET_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.MAP_PREFIX) .to("seda:out"); Spring DSL: <route> <from uri="direct:get" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>get</constant> </setHeader> <to uri="hazelcast:map:foo" /> <to uri="seda:out" /> </route> Sample for update:Java DSL: from("direct:update") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.UPDATE_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.MAP_PREFIX); Spring DSL: <route> <from uri="direct:update" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>update</constant> </setHeader> <to uri="hazelcast:map:foo" /> </route> Sample for delete:Java DSL: from("direct:delete") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.DELETE_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.MAP_PREFIX); Spring DSL: <route> <from uri="direct:delete" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>delete</constant> </setHeader> <to uri="hazelcast:map:foo" /> </route> Sample for queryJava DSL: from("direct:query") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.QUERY_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.MAP_PREFIX) .to("seda:out"); Spring DSL: <route> <from uri="direct:query" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>query</constant> </setHeader> <to uri="hazelcast:map:foo" /> <to uri="seda:out" /> </route> For the query operation Hazelcast offers a SQL like syntax to query your distributed map. String q1 = "bar > 1000"; template.sendBodyAndHeader("direct:query", null, HazelcastConstants.QUERY, q1); map cache consumer - from("hazelcast:map:foo")Hazelcast provides event listeners on their data grid. If you want to be notified if a cache will be manipulated, you can use the map consumer. There're 4 events: put, update, delete and envict. The event type will be stored in the "hazelcast.listener.action" header variable. The map consumer provides some additional information inside these variables: Header Variables inside the response message:
The object value will be stored within put and update actions inside the message body. Here's a sample: fromF("hazelcast:%sfoo", HazelcastConstants.MAP_PREFIX) .log("object...") .choice() .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.ADDED)) .log("...added") .to("mock:added") .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.ENVICTED)) .log("...envicted") .to("mock:envicted") .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.UPDATED)) .log("...updated") .to("mock:updated") .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.REMOVED)) .log("...removed") .to("mock:removed") .otherwise() .log("fail!"); Usage of Multi Mapmultimap cache producer - to("hazelcast:multimap:foo")A multimap is a cache where you can store n values to one key. The multimap producer provides 4 operations (put, get, removevalue, delete). Header Variables for the request message:
You can call the samples with: template.sendBodyAndHeader("direct:[put|get|update|delete|query]", "my-foo", HazelcastConstants.OBJECT_ID, "4711"); Sample for put:Java DSL: from("direct:put") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.PUT_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.MAP_PREFIX); Spring DSL: <route> <from uri="direct:put" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>put</constant> </setHeader> <to uri="hazelcast:map:foo" /> </route> Sample for get:Java DSL: from("direct:get") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.GET_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.MAP_PREFIX) .to("seda:out"); Spring DSL: <route> <from uri="direct:get" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>get</constant> </setHeader> <to uri="hazelcast:map:foo" /> <to uri="seda:out" /> </route> Sample for update:Java DSL: from("direct:update") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.UPDATE_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.MAP_PREFIX); Spring DSL: <route> <from uri="direct:update" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>update</constant> </setHeader> <to uri="hazelcast:map:foo" /> </route> Sample for delete:Java DSL: from("direct:delete") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.DELETE_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.MAP_PREFIX); Spring DSL: <route> <from uri="direct:delete" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>delete</constant> </setHeader> <to uri="hazelcast:map:foo" /> </route> Sample for queryJava DSL: from("direct:query") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.QUERY_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.MAP_PREFIX) .to("seda:out"); Spring DSL: <route> <from uri="direct:query" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>query</constant> </setHeader> <to uri="hazelcast:map:foo" /> <to uri="seda:out" /> </route> For the query operation Hazelcast offers a SQL like syntax to query your distributed map. String q1 = "bar > 1000"; template.sendBodyAndHeader("direct:query", null, HazelcastConstants.QUERY, q1); map cache consumer - from("hazelcast:map:foo")Hazelcast provides event listeners on their data grid. If you want to be notified if a cache will be manipulated, you can use the map consumer. There're 4 events: put, update, delete and envict. The event type will be stored in the "hazelcast.listener.action" header variable. The map consumer provides some additional information inside these variables: Header Variables inside the response message:
The object value will be stored within put and update actions inside the message body. Here's a sample: fromF("hazelcast:%sfoo", HazelcastConstants.MAP_PREFIX) .log("object...") .choice() .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.ADDED)) .log("...added") .to("mock:added") .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.ENVICTED)) .log("...envicted") .to("mock:envicted") .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.UPDATED)) .log("...updated") .to("mock:updated") .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.REMOVED)) .log("...removed") .to("mock:removed") .otherwise() .log("fail!"); Usage of Multi Mapmultimap cache producer - to("hazelcast:multimap:foo")A multimap is a cache where you can store n values to one key. The multimap producer provides 4 operations (put, get, removevalue, delete). Header Variables for the request message:
Sample for put:Java DSL: from("direct:put") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.PUT_OPERATION)) .to(String.format("hazelcast:%sbar", HazelcastConstants.MULTIMAP_PREFIX)); Spring DSL: <route> <from uri="direct:put" /> <log message="put.."/> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>put</constant> </setHeader> <to uri="hazelcast:multimap:foo" /> </route> Sample for removevalue:Java DSL: from("direct:removevalue") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.REMOVEVALUE_OPERATION)) .toF("hazelcast:%sbar", HazelcastConstants.MULTIMAP_PREFIX); Spring DSL: <route> <from uri="direct:removevalue" /> <log message="removevalue..."/> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>removevalue</constant> </setHeader> <to uri="hazelcast:multimap:foo" /> </route> To remove a value you have to provide the value you want to remove inside the message body. If you have a multimap object {key: "4711" values: { "my-foo", "my-bar"}} you have to put "my-foo" inside the message body to remove the "my-foo" value. Sample for get:Java DSL: from("direct:get") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.GET_OPERATION)) .toF("hazelcast:%sbar", HazelcastConstants.MULTIMAP_PREFIX) .to("seda:out"); Spring DSL: <route> <from uri="direct:get" /> <log message="get.."/> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>get</constant> </setHeader> <to uri="hazelcast:multimap:foo" /> <to uri="seda:out" /> </route> Sample for delete:Java DSL: from("direct:delete") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.DELETE_OPERATION)) .toF("hazelcast:%sbar", HazelcastConstants.MULTIMAP_PREFIX); Spring DSL: <route> <from uri="direct:delete" /> <log message="delete.."/> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>delete</constant> </setHeader> <to uri="hazelcast:multimap:foo" /> </route> you can call them in your test class with: template.sendBodyAndHeader("direct:[put|get|removevalue|delete]", "my-foo", HazelcastConstants.OBJECT_ID, "4711"); multimap cache consumer - from("hazelcast:multimap:foo")For the multimap cache this component provides the same listeners / variables as for the map cache consumer (except the update and enviction listener). The only difference is the multimap prefix inside the URI. Here is a sample: fromF("hazelcast:%sbar", HazelcastConstants.MULTIMAP_PREFIX) .log("object...") .choice() .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.ADDED)) .log("...added") .to("mock:added") //.when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.ENVICTED)) // .log("...envicted") // .to("mock:envicted") .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.REMOVED)) .log("...removed") .to("mock:removed") .otherwise() .log("fail!"); Header Variables inside the response message:
Eviction will be added as feature, soon (this is a Hazelcast issue).
Usage of QueueQueue producer – to(“hazelcast:queue:foo”)The queue producer provides 6 operations (add, put, poll, peek, offer, removevalue). Sample for add:from("direct:add") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.ADD_OPERATION)) .toF("hazelcast:%sbar", HazelcastConstants.QUEUE_PREFIX); Sample for put:from("direct:put") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.PUT_OPERATION)) .toF("hazelcast:%sbar", HazelcastConstants.QUEUE_PREFIX); Sample for poll:from("direct:poll") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.POLL_OPERATION)) .toF("hazelcast:%sbar", HazelcastConstants.QUEUE_PREFIX); Sample for peek:from("direct:peek") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.PEEK_OPERATION)) .toF("hazelcast:%sbar", HazelcastConstants.QUEUE_PREFIX); Sample for offer:from("direct:offer") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.OFFER_OPERATION)) .toF("hazelcast:%sbar", HazelcastConstants.QUEUE_PREFIX); Sample for removevalue:from("direct:removevalue") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.REMOVEVALUE_OPERATION)) .toF("hazelcast:%sbar", HazelcastConstants.QUEUE_PREFIX); Queue consumer – from(“hazelcast:queue:foo”)The queue consumer provides 2 operations (add, remove). fromF("hazelcast:%smm", HazelcastConstants.QUEUE_PREFIX) .log("object...") .choice() .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.ADDED)) .log("...added") .to("mock:added") .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.REMOVED)) .log("...removed") .to("mock:removed") .otherwise() .log("fail!"); Usage of ListList producer – to(“hazelcast:list:foo”)The list producer provides 4 operations (add, set, get, removevalue). Sample for add:from("direct:add") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.ADD_OPERATION)) .toF("hazelcast:%sbar", HazelcastConstants.LIST_PREFIX); Sample for get:from("direct:get") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.GET_OPERATION)) .toF("hazelcast:%sbar", HazelcastConstants.LIST_PREFIX) .to("seda:out"); Sample for setvalue:from("direct:set") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.SETVALUE_OPERATION)) .toF("hazelcast:%sbar", HazelcastConstants.LIST_PREFIX); Sample for removevalue:from("direct:removevalue") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.REMOVEVALUE_OPERATION)) .toF("hazelcast:%sbar", HazelcastConstants.LIST_PREFIX);
List consumer – from(“hazelcast:list:foo”)The list consumer provides 2 operations (add, remove). fromF("hazelcast:%smm", HazelcastConstants.LIST_PREFIX) .log("object...") .choice() .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.ADDED)) .log("...added") .to("mock:added") .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.REMOVED)) .log("...removed") .to("mock:removed") .otherwise() .log("fail!"); Usage of SEDASEDA component differs from the rest components provided. It implements a work-queue in order to support asynchronous SEDA architectures, similar to the core "SEDA" component. SEDA producer – to(“hazelcast:seda:foo”)The SEDA producer provides no operations. You only send data to the specified queue.
Java DSL : from("direct:foo") .to("hazelcast:seda:foo"); Spring DSL : <route> <from uri="direct:start" /> <to uri="hazelcast:seda:foo" /> </route> SEDA consumer – from(“hazelcast:seda:foo”)The SEDA consumer provides no operations. You only retrieve data from the specified queue.
Java DSL : from("hazelcast:seda:foo") .to("mock:result"); Spring DSL: <route> <from uri="hazelcast:seda:foo" /> <to uri="mock:result" /> </route> Usage of Atomic Number
atomic number producer - to("hazelcast:atomicnumber:foo")An atomic number is an object that simply provides a grid wide number (long). The operations for this producer are setvalue (set the number with a given value), get, increase (+1), decrease (-1) and destroy. Header Variables for the request message:
Sample for set:Java DSL: from("direct:set") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.SETVALUE_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.ATOMICNUMBER_PREFIX); Spring DSL: <route> <from uri="direct:set" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>setvalue</constant> </setHeader> <to uri="hazelcast:atomicvalue:foo" /> </route> Provide the value to set inside the message body (here the value is 10): template.sendBody("direct:set", 10); Sample for get:Java DSL: from("direct:get") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.GET_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.ATOMICNUMBER_PREFIX); Spring DSL: <route> <from uri="direct:get" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>get</constant> </setHeader> <to uri="hazelcast:atomicvalue:foo" /> </route> You can get the number with long body = template.requestBody("direct:get", null, Long.class);. Sample for increment:Java DSL: from("direct:increment") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.INCREMENT_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.ATOMICNUMBER_PREFIX); Spring DSL: <route> <from uri="direct:increment" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>increment</constant> </setHeader> <to uri="hazelcast:atomicvalue:foo" /> </route> The actual value (after increment) will be provided inside the message body. Sample for decrement:Java DSL: from("direct:decrement") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.DECREMENT_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.ATOMICNUMBER_PREFIX); Spring DSL: <route> <from uri="direct:decrement" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>decrement</constant> </setHeader> <to uri="hazelcast:atomicvalue:foo" /> </route> The actual value (after decrement) will be provided inside the message body. Sample for destroy
Java DSL: from("direct:destroy") .setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.DESTROY_OPERATION)) .toF("hazelcast:%sfoo", HazelcastConstants.ATOMICNUMBER_PREFIX); Spring DSL: <route> <from uri="direct:destroy" /> <!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" --> <setHeader headerName="hazelcast.operation.type"> <constant>destroy</constant> </setHeader> <to uri="hazelcast:atomicvalue:foo" /> </route> cluster support
instance consumer - from("hazelcast:instance:foo")Hazelcast makes sense in one single "server node", but it's extremly powerful in a clustered environment. The instance consumer fires if a new cache instance will join or leave the cluster. Here's a sample: fromF("hazelcast:%sfoo", HazelcastConstants.INSTANCE_PREFIX) .log("instance...") .choice() .when(header(HazelcastConstants.LISTENER_ACTION).isEqualTo(HazelcastConstants.ADDED)) .log("...added") .to("mock:added") .otherwise() .log("...removed") .to("mock:removed"); Each event provides the following information inside the message header: Header Variables inside the response message:
HDFS ComponentAvailable as of Camel 2.8 The hdfs component enables you to read and write messages from/to an HDFS file system. HDFS is the distributed file system at the heart of Hadoop. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-hdfs</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI format
hdfs://hostname[:port][/path][?options]
You can append query options to the URI in the following format, ?option=value&option=value&...
Options
KeyType and ValueType
BYTES is also used with everything else, for example, in Camel a file is sent around as an InputStream, int this case is written in a sequence file or a map file as a sequence of bytes. Splitting StrategyIn the current version of Hadoop opening a file in append mode is disabled since it's not enough reliable. So, for the moment, it's only possible to create new files. The Camel HDFS endpoint tries to solve this problem in this way:
where <ST> can be:
for example:
hdfs://localhost/tmp/simple-file?splitStrategy=IDLE:1000,BYTES:5
it means: a new file is created either when it has been idle for more than 1 second or if more than 5 bytes have been written. So, running hadoop fs -ls /tmp/simple-file you'll find the following files seg0, seg1, seg2, etc Controlling to close file streamAvailable as of Camel 2.10.4 When using the HDFS producer without a split strategy, then the file output stream is by default closed after the write. However you may want to keep the stream open, and only explicit close the stream later. For that you can use the header HdfsConstants.HDFS_CLOSE (value = "CamelHdfsClose") to control this. Setting this value to a boolean allows you to explicit control whether the stream should be closed or not. Notice this does not apply if you use a split strategy, as there is varios strategy that control when the stream is closed. Using this component in OSGiThis component is fully functional in an OSGi environment however, it requires some actions from the user. Hadoop uses the thread context class loader in order to load resources. Usually, the thread context classloader will be the bundle class loader of the bundle that contains the routes. So, the default configuration files need to be visible from the bundle class loader. A typical way to deal with it is to keep a copy of core-default.xml in your bundle root. That file can be found in the hadoop-common.jar. Hibernate ComponentThe hibernate: component allows you to work with databases using Hibernate as the object relational mapping technology to map POJOs to database tables. The camel-hibernate library is provided by the Camel Extra project which hosts all *GPL related components for Camel.
Sending to the endpointSending POJOs to the hibernate endpoint inserts entities into the database. The body of the message is assumed to be an entity bean that you have mapped to a relational table using the hibernate .hbm.xml files. If the body does not contain an entity bean, use a Message Translator in front of the endpoint to perform the necessary conversion first. Consuming from the endpointConsuming messages removes (or updates) entities 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 when it has been processed, 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 is consumed. URI formathibernate:[entityClassName][?options] For sending to the endpoint, the entityClassName is optional. If specified it is used to help use the type conversion 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
See AlsoHL7 ComponentThe hl7 component is used for working with the HL7 MLLP protocol and HL7 v2 messages using the HAPI library. This component supports the following:
Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-hl7</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> HL7 MLLP protocolHL7 is often used with the HL7 MLLP protocol that is a text based TCP socket based protocol. This component ships with a Mina Codec that conforms to the MLLP protocol so you can easily expose a HL7 listener that accepts HL7 requests over the TCP transport. To expose a HL7 listener service we reuse the existing mina/mina2 component where we just use the HL7MLLPCodec as codec. The HL7 MLLP codec has the following options:
Exposing a HL7 listenerIn our Spring XML file, we configure an endpoint to listen for HL7 requests using TCP:
<endpoint id="hl7listener" uri="mina:tcp://localhost:8888?sync=true&codec=#hl7codec"/>
<!-- Camel 2.11: uri="mina2:tcp... -->
Notice that we use TCP on localhost on port 8888. We use sync=true to indicate that this listener is synchronous and therefore will return a HL7 response to the caller. Then we setup mina to use our HL7 codec with codec=#hl7codec. Notice that hl7codec is just a Spring bean ID, so we could have named it mygreatcodecforhl7 or whatever. The codec is also set up in the Spring XML file:
<bean id="hl7codec" class="org.apache.camel.component.hl7.HL7MLLPCodec">
<property name="charset" value="iso-8859-1"/>
</bean>
Above we also configure the charset encoding to use (iso-8859-1). The endpoint hl7listener can then be used in a route as a consumer, as this Java DSL example illustrates:
from("hl7listener").to("patientLookupService");
This is a very simple route that will listen for HL7 and route it to a service named patientLookupService that is also a Spring bean ID we have configured in the Spring XML as:
<bean id="patientLookupService" class="com.mycompany.healthcare.service.PatientLookupService"/>
Another powerful feature of Camel is that we can have our business logic in POJO classes that is not tied to Camel as shown here: import ca.uhn.hl7v2.HL7Exception; import ca.uhn.hl7v2.model.Message; import ca.uhn.hl7v2.model.v24.segment.QRD; public class PatientLookupService { public Message lookupPatient(Message input) throws HL7Exception { QRD qrd = (QRD)input.get("QRD"); String patientId = qrd.getWhoSubjectFilter(0).getIDNumber().getValue(); // find patient data based on the patient id and create a HL7 model object with the response Message response = ... create and set response data return response } Notice that this class uses just imports from the HAPI library and not from Camel. HL7 Model using java.lang.StringThe HL7MLLP codec uses plain String as its data format. Camel uses its Type Converter to convert to/from strings to the HAPI HL7 model objects. However, you can use plain String objects if you prefer, for instance if you wish to parse the data yourself. See samples for such an example. HL7v2 Model using HAPIThe HL7v2 model uses Java objects from the HAPI library. Using this library, we can encode and decode from the EDI format (ER7) that is mostly used with HL7v2. The sample below is a request to lookup a patient with the patient ID 0101701234. MSH|^~\\&|MYSENDER|MYRECEIVER|MYAPPLICATION||200612211200||QRY^A19|1234|P|2.4 QRD|200612211200|R|I|GetPatient|||1^RD|0101701234|DEM|| Using the HL7 model we can work with the data as a ca.uhn.hl7v2.model.Message object. Message msg = exchange.getIn().getBody(Message.class); QRD qrd = (QRD)msg.get("QRD"); String patientId = qrd.getWhoSubjectFilter(0).getIDNumber().getValue(); If you know the message type in advance, you can be more type-safe:
QRY_A19 msg = exchange.getIn().getBody(QRY_A19.class);
String patientId = msg.getQRD().getWhoSubjectFilter(0).getIDNumber().getValue();
Camel has built-in type converters, so when this operation is invoked: Message msg = exchange.getIn().getBody(Message.class); Camel will convert the received HL7 data from String to Message. This is powerful when combined with the HL7 listener, then you as the end-user don't have to work with byte[], String or any other simple object formats. You can just use the HAPI HL7v2 model objects. HL7 DataFormatThe HL7 component ships with a HL7 data format that can be used to format between String and HL7 model objects.
To use the data format, simply instantiate an instance and invoke the marshal or unmarshal operation in the route builder: DataFormat hl7 = new HL7DataFormat(); ... from("direct:hl7in").marshal(hl7).to("jms:queue:hl7out"); In the sample above, the HL7 is marshalled from a HAPI Message object to a byte stream and put on a JMS queue. DataFormat hl7 = new HL7DataFormat(); ... from("jms:queue:hl7out").unmarshal(hl7).to("patientLookupService"); Here we unmarshal the byte stream into a HAPI Message object that is passed to our patient lookup service.
Notice there is a shorthand syntax in Camel for well-known data formats that is commonly used. from("direct:hl7in").marshal().hl7().to("jms:queue:hl7out"); from("jms:queue:hl7out").unmarshal().hl7().to("patientLookupService");
Message HeadersThe unmarshal operation adds these MSH fields as headers on the Camel message:
All headers are String types. If a header value is missing, its value is null. OptionsThe HL7 Data Format supports the following options:
DependenciesTo use HL7 in your Camel routes you'll need to add a dependency on camel-hl7 listed above, which implements this data format. The HAPI library since Version 0.6 has been split into a base library and several structure libraries, one for each HL7v2 message version:
By default camel-hl7 only references the HAPI base library. Applications are responsible for including structure libraries themselves. For example, if a application works with HL7v2 message versions 2.4 and 2.5 then the following dependencies must be added: <dependency> <groupId>ca.uhn.hapi</groupId> <artifactId>hapi-structures-v24</artifactId> <version>1.2</version> <!-- use the same version as your hapi-base version --> </dependency> <dependency> <groupId>ca.uhn.hapi</groupId> <artifactId>hapi-structures-v25</artifactId> <version>1.2</version> <!-- use the same version as your hapi-base version --> </dependency> Alternatively, an OSGi bundle containing the base library, all structures libraries and required dependencies (on the bundle classpath) can be downloaded from the central Maven repository. <dependency> <groupId>ca.uhn.hapi</groupId> <artifactId>hapi-osgi-base</artifactId> <version>1.2</version> </dependency> Terser language (Camel 2.11)HAPI provides a Terser class that provides access to fields using a commonly used terse location specification syntax. The Terser language allows to use this syntax to extract values from messages and to use them as expressions and predicates for filtering, content-based routing etc. Sample: import static org.apache.camel.component.hl7.HL7.terser; ... // extract patient ID from field QRD-8 in the QRY_A19 message above and put into message header from("direct:test1") .setHeader("PATIENT_ID",terser("QRD-8(0)-1")) .to("mock:test1"); // continue processing if extracted field equals a message header from("direct:test2") .filter(terser("QRD-8(0)-1") .isEqualTo(header("PATIENT_ID")) .to("mock:test2"); HL7 Validation predicate (Camel 2.11)Often it is preferable to parse a HL7v2 message and validate it against a HAPI ValidationContext in a separate step afterwards. Sample: import static org.apache.camel.component.hl7.HL7.messageConformsTo; import ca.uhn.hl7v2.validation.impl.DefaultValidation; ... // Use standard or define your own validation rules ValidationContext defaultContext = new DefaultValidation(); // Throws PredicateValidationException if message does not validate from("direct:test1").validate(messageConformsTo(defaultContext)).to("mock:test1"); HL7 Acknowledgement expression (Camel 2.11)A common task in HL7v2 processing is to generate an acknowledgement message as response to an incoming HL7v2 message, e.g. based on a validation result. The ack expression lets us accomplish this very elegantly: import static org.apache.camel.component.hl7.HL7.messageConformsTo; import static org.apache.camel.component.hl7.HL7.ack; import ca.uhn.hl7v2.validation.impl.DefaultValidation; ... // Use standard or define your own validation rules ValidationContext defaultContext = new DefaultValidation(); from("direct:test1") .onException(Exception.class) .handled(true) .transform(ack()) // auto-generates negative ack because of exception in Exchange .end() .validate(messageConformsTo(defaultContext)) // do something meaningful here ... // acknowledgement .transform(ack()) More SamplesIn the following example we send a HL7 request to a HL7 listener and retrieves a response. We use plain String types in this example: String line1 = "MSH|^~\\&|MYSENDER|MYRECEIVER|MYAPPLICATION||200612211200||QRY^A19|1234|P|2.4"; String line2 = "QRD|200612211200|R|I|GetPatient|||1^RD|0101701234|DEM||"; StringBuilder in = new StringBuilder(); in.append(line1); in.append("\n"); in.append(line2); String out = (String)template.requestBody("mina2:tcp://127.0.0.1:8888?sync=true&codec=#hl7codec", in.toString()); In the next sample, we want to route HL7 requests from our HL7 listener to our business logic. We have our business logic in a plain POJO that we have registered in the registry as hl7service = for instance using Spring and letting the bean id = hl7service. Our business logic is a plain POJO only using the HAPI library so we have these operations defined: public class MyHL7BusinessLogic { // This is a plain POJO that has NO imports whatsoever on Apache Camel. // its a plain POJO only importing the HAPI library so we can much easier work with the HL7 format. public Message handleA19(Message msg) throws Exception { // here you can have your business logic for A19 messages assertTrue(msg instanceof QRY_A19); // just return the same dummy response return createADR19Message(); } public Message handleA01(Message msg) throws Exception { // here you can have your business logic for A01 messages assertTrue(msg instanceof ADT_A01); // just return the same dummy response return createADT01Message(); } } Then we set up the Camel routes using the RouteBuilder as follows: DataFormat hl7 = new HL7DataFormat(); // we setup or HL7 listener on port 8888 (using the hl7codec) and in sync mode so we can return a response from("mina2:tcp://127.0.0.1:8888?sync=true&codec=#hl7codec") // we use the HL7 data format to unmarshal from HL7 stream to the HAPI Message model // this ensures that the camel message has been enriched with hl7 specific headers to // make the routing much easier (see below) .unmarshal(hl7) // using choice as the content base router .choice() // where we choose that A19 queries invoke the handleA19 method on our hl7service bean .when(header("CamelHL7TriggerEvent").isEqualTo("A19")) .beanRef("hl7service", "handleA19") .to("mock:a19") // and A01 should invoke the handleA01 method on our hl7service bean .when(header("CamelHL7TriggerEvent").isEqualTo("A01")).to("mock:a01") .beanRef("hl7service", "handleA01") .to("mock:a19") // other types should go to mock:unknown .otherwise() .to("mock:unknown") // end choice block .end() // marshal response back .marshal(hl7); Notice that we use the HL7 DataFormat to enrich our Camel Message with the MSH fields preconfigured on the Camel Message. This lets us much more easily define our routes using the fluent builders. Sample using plain String objectsIn this sample we use plain String objects as the data format, that we send, process and receive. As the sample is part of a unit test, there is some code for assertions, but you should be able to understand what happens. First we send the plain string, Hello World, to the HL7MLLPCodec and receive the response as a plain string, Bye World. MockEndpoint mock = getMockEndpoint("mock:result"); mock.expectedBodiesReceived("Bye World"); // send plain hello world as String Object out = template.requestBody("mina2:tcp://127.0.0.1:8888?sync=true&codec=#hl7codec", "Hello World"); assertMockEndpointsSatisfied(); // and the response is also just plain String assertEquals("Bye World", out); Here we process the incoming data as plain String and send the response also as plain String: from("mina2:tcp://127.0.0.1:8888?sync=true&codec=#hl7codec") .process(new Processor() { public void process(Exchange exchange) throws Exception { // use plain String as message format String body = exchange.getIn().getBody(String.class); assertEquals("Hello World", body); // return the response as plain string exchange.getOut().setBody("Bye World"); } }) .to("mock:result"); See AlsoHTTP ComponentThe 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: <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 formathttp:hostname[:port][/resourceUri][?param1=value1][¶m2=value2] Will by default use port 80 for HTTP and 443 for HTTPS.
ExamplesCall the url with the body using POST and return response as out message. If body is null call URL using GET and return response as out message
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.
URI parameters can either be set directly on the endpoint URI or as a header
Set the HTTP request method to POST
HttpEndpoint Options
Authentication and ProxyThe following authentication options can also be set on the HttpEndpoint:
When using authentication you must provide the choice of method for the authMethod or authProxyMethod options. 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. HttpComponent Options
HttpConfiguration contains all the options listed in the table above under the section HttpConfiguration - Setting Authentication and Proxy. Message Headers
The header name above are constants. For the spring DSL you have to use the value of the constant instead of the name. Message BodyCamel 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 codeCamel will handle according to the HTTP response code:
HttpOperationFailedExceptionThis exception contains the following information:
Calling using GET or POSTThe following algorithm is used to determine if either GET or POST HTTP method should be used: How to get access to HttpServletRequest and HttpServletResponseYou can get access to these two using the Camel type converter system using HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class); HttpServletRequest response = exchange.getIn().getBody(HttpServletResponse.class); Using client timeout - SO_TIMEOUTSee the unit test in this link More ExamplesConfiguring a Proxy
There is also support for proxy authentication via the proxyUsername and proxyPassword options. Using proxy settings outside of URI
Options on Endpoint will override options on the context. Configuring charsetIf you are using POST to send data you can configure the charset
setProperty(Exchange.CHARSET_NAME, "iso-8859-1");
Sample with scheduled pollThe sample polls the Google homepage every 10 seconds and write the page to the file message.html: from("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 CodeYou can get the HTTP response code from the HTTP component by getting the value from the Out message header with HttpProducer.HTTP_RESPONSE_CODE. Exchange 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(HttpProducer.HTTP_RESPONSE_CODE, Integer.class); Using throwExceptionOnFailure=false to get any response backIn the route below we want to route a message that we enrich with data returned from a remote HTTP call. As we want any response from the remote server, we set the throwExceptionOnFailure option to 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. // We set throwExceptionOnFailure to false to let Camel return any response from the remove HTTP server without thrown // HttpOperationFailedException in case of failures. // This allows us to handle all responses in the aggregation strategy where we can check the HTTP response code // and decide what to do. As this is based on an unit test we assert the code is 404 from("direct:start").enrich("http://localhost:{{port}}/myserver?throwExceptionOnFailure=false&user=Camel", new AggregationStrategy() { public Exchange aggregate(Exchange original, Exchange resource) { // get the response code Integer code = resource.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class); assertEquals(404, code.intValue()); return resource; } }).to("mock:result"); // this is our jetty server where we simulate the 404 from("jetty://http://localhost:{{port}}/myserver") .process(new Processor() { public void process(Exchange exchange) throws Exception { exchange.getOut().setBody("Page not found"); exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404); } }); Disabling CookiesTo disable cookies you can set the HTTP Client to ignore cookies by adding this URI option: Advanced UsageIf 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 MaxConnectionsPerHostThe HTTP Component has a org.apache.commons.httpclient.HttpConnectionManager where you can configure various global configuration for the given component. 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. <bean id="http" class="org.apache.camel.component.http.HttpComponent"> <property name="camelContext" ref="camel"/> <property name="httpConnectionManager" ref="myHttpConnectionManager"/> </bean> <bean id="myHttpConnectionManager" class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager"> <property name="params" ref="myHttpConnectionManagerParams"/> </bean> <bean id="myHttpConnectionManagerParams" class="org.apache.commons.httpclient.params.HttpConnectionManagerParams"> <property name="defaultMaxConnectionsPerHost" value="5"/> </bean> And then we can just use it as we normally do in our routes: <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring" trace="true"> <route> <from uri="direct:start"/> <to uri="http://www.google.com"/> <to uri="mock:result"/> </route> </camelContext> Using preemptive authenticationAn end user reported that he had problem with authenticating with HTTPS. The problem was eventually resolved when he discovered the HTTPS server did not return a HTTP code 401 Authorization Required. The solution was to set the following URI option: httpClient.authenticationPreemptive=true Accepting self signed certificates from remote serverSee 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 ClientUsing the JSSE Configuration UtilityAs of 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. KeyStoreParameters 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 DirectlyBasically 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: Protocol 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 route builder class you can hook it up like so: HttpComponent 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: <bean id="myHttpClientConfigurer" class="my.https.HttpClientConfigurer"> </bean> <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. See AlsoiBATISThe ibatis: component allows you to query, poll, insert, update and delete data in a relational database using Apache iBATIS.
Maven users will need to add the following dependency to their pom.xml for this component: <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 formatibatis: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. Options
Message HeadersCamel will populate the result message, either IN or OUT with a header with the operationName used:
Message BodyThe 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. SamplesFor 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: <!-- 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 IBatisWhen 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: from("direct:start") .to("ibatis:selectAccountById?statementType=QueryForObject") .to("mock:result"); 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: from("direct:start") .to("ibatis:selectAllAccounts?statementType=QueryForList") .to("mock:result"); And the same for UPDATE, where we can send an Account object as IN body to iBatis: from("direct:start") .to("ibatis:updateAccount?statementType=Update") .to("mock:result"); Scheduled polling exampleSince 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:
from("timer://pollTheDatabase?delay=30000").to("ibatis:selectAllAccounts?statementType=QueryForList").to("activemq:queue:allAccounts");
And the iBatis SQL map file used: <!-- Select with no parameters using the result map for Account class. --> <select id="selectAllAccounts" resultMap="AccountResult"> select * from ACCOUNT </select> Using onConsumeThis 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. from("ibatis:selectUnprocessedAccounts?consumer.onConsume=consumeAccount").to("mock:results"); And the statements in the sqlmap file: <select id="selectUnprocessedAccounts" resultMap="AccountResult"> select * from ACCOUNT where PROCESSED = false </select> <update id="consumeAccount" parameterClass="Account"> update ACCOUNT set PROCESSED = true where ACC_ID = #id# </update> See AlsoIRC ComponentThe 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 formatirc: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
SSL SupportUsing the JSSE Configuration UtilityAs 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 endpointKeyStoreParameters 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 optionsYou 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 keysAvailable 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 See AlsoJasypt componentAvailable as of Camel 2.5 Jasypt is a simplified encryption library which makes encryption and decryption easy. Camel integrates with Jasypt to allow sensitive information in Properties files to be encrypted. By dropping camel-jasypt on the classpath those encrypted values will automatic be decrypted on-the-fly by Camel. This ensures that human eyes can't easily spot sensitive information such as usernames and passwords. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jasypt</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> ToolingThe Jasypt component provides a little command line tooling to encrypt or decrypt values. The console output the syntax and which options it provides: Apache Camel Jasypt takes the following options -h or -help = Displays the help screen -c or -command <command> = Command either encrypt or decrypt -p or -password <password> = Password to use -i or -input <input> = Text to encrypt or decrypt -a or -algorithm <algorithm> = Optional algorithm to use For example to encrypt the value tiger you run with the following parameters. In the apache camel kit, you cd into the lib folder and run the following java cmd, where <CAMEL_HOME> is where you have downloaded and extract the Camel distribution. $ cd <CAMEL_HOME>/lib $ java -jar camel-jasypt-2.5.0.jar -c encrypt -p secret -i tiger Which outputs the following result Encrypted text: qaEEacuW7BUti8LcMgyjKw== This means the encrypted representation qaEEacuW7BUti8LcMgyjKw== can be decrypted back to tiger if you know the master password which was secret. So you can test it by running the tooling using the following parameters: $ cd <CAMEL_HOME>/lib $ java -jar camel-jasypt-2.5.0.jar -c decrypt -p secret -i qaEEacuW7BUti8LcMgyjKw== Which outputs the following result: Decrypted text: tiger The idea is then to use those encrypted values in your Properties files. Notice how the password value is encrypted and the value has the tokens surrounding ENC(value here) # refer to a mock endpoint name by that encrypted password
cool.result=mock:{{cool.password}}
# here is a password which is encrypted
cool.password=ENC(bsW9uV37gQ0QHFu7KO03Ww==)
Tooling dependencies for Camel 2.5 and 2.6The tooling requires the following JARs in the classpath, which has been enlisted in the MANIFEST.MF file of camel-jasypt with optional/ as prefix. Hence why the java cmd above can pickup the needed JARs from the Apache Distribution in the optional directory. jasypt-1.6.jar commons-lang-2.4.jar commons-codec-1.4.jar icu4j-4.0.1.jar
Tooling dependencies for Camel 2.7 or betterJasypt 1.7 onwards is now fully standalone so no additional JARs is needed. URI OptionsThe options below are exclusive for the Jasypt component.
Protecting the master passwordThe master password used by Jasypt must be provided, so its capable of decrypting the values. However having this master password out in the opening may not be an ideal solution. Therefore you could for example provided it as a JVM system property or as a OS environment setting. If you decide to do so then the password option supports prefixes which dictates this. sysenv: means to lookup the OS system environment with the given key. sys: means to lookup a JVM system property. For example you could provided the password before you start the application $ export CAMEL_ENCRYPTION_PASSWORD=secret Then start the application, such as running the start script. When the application is up and running you can unset the environment $ unset CAMEL_ENCRYPTION_PASSWORD The password option is then a matter of defining as follows: password=sysenv:CAMEL_ENCRYPTION_PASSWORD. Example with Java DSLIn Java DSL you need to configure Jasypt as a JasyptPropertiesParser instance and set it on the Properties component as show below: // create the jasypt properties parser JasyptPropertiesParser jasypt = new JasyptPropertiesParser(); // and set the master password jasypt.setPassword("secret"); // create the properties component PropertiesComponent pc = new PropertiesComponent(); pc.setLocation("classpath:org/apache/camel/component/jasypt/myproperties.properties"); // and use the jasypt properties parser so we can decrypt values pc.setPropertiesParser(jasypt); // add properties component to camel context context.addComponent("properties", pc); The properties file myproperties.properties then contain the encrypted value, such as shown below. Notice how the password value is encrypted and the value has the tokens surrounding ENC(value here) # refer to a mock endpoint name by that encrypted password
cool.result=mock:{{cool.password}}
# here is a password which is encrypted
cool.password=ENC(bsW9uV37gQ0QHFu7KO03Ww==)
Example with Spring XMLIn Spring XML you need to configure the JasyptPropertiesParser which is shown below. Then the Camel Properties component is told to use jasypt as the properties parser, which means Jasypt have its chance to decrypt values looked up in the properties. <!-- define the jasypt properties parser with the given password to be used --> <bean id="jasypt" class="org.apache.camel.component.jasypt.JasyptPropertiesParser"> <property name="password" value="secret"/> </bean> <!-- define the camel properties component --> <bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent"> <!-- the properties file is in the classpath --> <property name="location" value="classpath:org/apache/camel/component/jasypt/myproperties.properties"/> <!-- and let it leverage the jasypt parser --> <property name="propertiesParser" ref="jasypt"/> </bean> The Properties component can also be inlined inside the <camelContext> tag which is shown below. Notice how we use the propertiesParserRef attribute to refer to Jasypt. <!-- define the jasypt properties parser with the given password to be used --> <bean id="jasypt" class="org.apache.camel.component.jasypt.JasyptPropertiesParser"> <!-- password is mandatory, you can prefix it with sysenv: or sys: to indicate it should use an OS environment or JVM system property value, so you dont have the master password defined here --> <property name="password" value="secret"/> </bean> <camelContext xmlns="http://camel.apache.org/schema/spring"> <!-- define the camel properties placeholder, and let it leverage jasypt --> <propertyPlaceholder id="properties" location="classpath:org/apache/camel/component/jasypt/myproperties.properties" propertiesParserRef="jasypt"/> <route> <from uri="direct:start"/> <to uri="{{cool.result}}"/> </route> </camelContext> See Also
JavaSpace ComponentAvailable as of Camel 2.1 The javaspace component is a transport for working with any JavaSpace compliant implementation and this component has been tested with both the Blitz implementation and the GigaSpace implementation . Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-javaspace</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI format
javaspace:jini://host[?options]
You can append query options to the URI in the following format, ?option=value&option=value&... Options
ExamplesSending and Receiving Entries// sending route from("direct:input") .to("javaspace:jini://localhost?spaceName=mySpace"); // receiving Route from("javaspace:jini://localhost?spaceName=mySpace&templateId=template&verb=take&concurrentConsumers=1") .to("mock:foo"); In this case the payload can be any object that inherits from the Jini Entry type. Sending and receiving serializable objectsUsing the preceding routes, it is also possible to send and receive any serializable object. The JavaSpace component detects that the payload is not a Jini Entry and then it automatically wraps the payload with a Camel Jini Entry. In this way, a JavaSpace can be used as a generic transport mechanism. Using JavaSpace as a remote invocation transportThe JavaSpace component has been tailored to work in combination with the Camel bean component. It is therefore possible to call a remote POJO using JavaSpace as the transport: // client side from("direct:input") .to("javaspace:jini://localhost?spaceName=mySpace"); // server side from("javaspace:jini://localhost?concurrentConsumers=10&spaceName=mySpace") .to("mock:foo"); In the code there are two test cases showing how to use a POJO to realize the master/worker pattern. The idea is to use the POJO to provide the business logic and rely on Camel for sending/receiving requests/replies with the proper correlation. See AlsoJBI ComponentThe 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.
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 formatjbi: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:
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&... Examplesjbi: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
Examplesjbi: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 bodiesIf 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. Creating a JBI Service UnitIf 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:
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> See AlsoJCR ComponentThe 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
UsageThe repository element of the URI is used to look up the JCR Repository object in the Camel context registry. Producer
When a message is sent to a JCR producer endpoint:
ConsumerThe consumer will connect to JCR periodically and return a List<javax.jcr.observation.Event> in the message body.
ExampleThe snippet below creates a node named node under the /home/test node in the content repository. One additional attribute is added to the node as well: my.contents.property which will contain the body of the message being sent. from("direct:a").setProperty(JcrConstants.JCR_NODE_NAME, constant("node")) .setProperty("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> See AlsoJDBC ComponentThe jdbc component enables you to access databases through JDBC, where SQL queries and operations 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>
URI formatjdbc: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
ResultThe 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. Note: This component fetches ResultSetMetaData to be able to return the column name as the key in the Map. Message Headers
Generated keysAvailable 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. You can see more details in this unit test. SamplesIn the following example, we fetch the rows from the customer table. First we register our datasource in the Camel registry as testdb: JndiRegistry reg = super.createRegistry(); reg.bind("testdb", db); return reg; 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: // lets add simple route public void configure() throws Exception { from("direct:hello").to("jdbc:testdb?readSize=100"); } Or you can create a DataSource in Spring like this: <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <route> <!-- trigger every second --> <from uri="timer://kickoff?period=1s"/> <setBody> <constant>select * from customer</constant> </setBody> <to uri="jdbc:testdb"/> <to uri="mock:result"/> </route> </camelContext> <!-- Just add a demo to show how to bind a date source for camel in Spring--> <jdbc:embedded-database id="testdb" type="DERBY"> <jdbc:script location="classpath:sql/init.sql"/> </jdbc:embedded-database> 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: // first we create our exchange using the endpoint Endpoint endpoint = context.getEndpoint("direct:hello"); Exchange exchange = endpoint.createExchange(); // then we set the SQL on the in body exchange.getIn().setBody("select * from customer order by ID"); // now we send the exchange to the endpoint, and receives the response from Camel Exchange out = template.send(endpoint, exchange); // assertions of the response assertNotNull(out); assertNotNull(out.getOut()); List<Map<String, Object>> data = out.getOut().getBody(List.class); assertNotNull(data); assertEquals(3, data.size()); Map<String, Object> row = data.get(0); assertEquals("cust1", row.get("ID")); assertEquals("jstrachan", row.get("NAME")); row = data.get(1); assertEquals("cust2", row.get("ID")); assertEquals("nsandhu", row.get("NAME")); 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: 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 .to("jdbc:testdb").split(body()).to("mock:result"); Sample - Polling the database every minuteIf 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:
from("timer://foo?period=60000").setBody(constant("select * from customer")).to("jdbc:testdb").to("activemq:queue:customers");
See AlsoJetty ComponentThe jetty component provides HTTP-based endpoints for consuming and producing HTTP requests. That is, the Jetty component behaves as a simple Web server.
Maven users will need to add the following dependency to their pom.xml for this component: <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]
You can append query options to the URI in the following format, ?option=value&option=value&... Options
Message HeadersCamel uses the same message headers as the HTTP component. 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. Starting with Camel 2.2.0, you can get the request.parameter from the message header not only from Get Method, but also other HTTP method. UsageThe Jetty component supports both consumer and producer endpoints. Another option for producing to other HTTP endpoints, is to use the HTTP Component Component OptionsThe JettyHttpComponent provides the following options:
Producer ExampleThe following is a basic example of how to send an HTTP request to an existing HTTP endpoint. in Java DSL from("direct:start").to("jetty://http://www.google.com"); or in Spring XML <route> <from uri="direct:start"/> <to uri="jetty://http://www.google.com"/> <route> Consumer ExampleIn this sample we define a route that exposes a HTTP service at http://localhost:8080/myapp/myservice: from("jetty:http://localhost:{{port}}/myapp/myservice").process(new MyBookService());
Our business logic is implemented in the MyBookService class, which accesses the HTTP request contents and then returns a response. public class MyBookService implements Processor { public void process(Exchange exchange) throws Exception { // just get the body as a string String body = exchange.getIn().getBody(String.class); // we have access to the HttpServletRequest here and we can grab it if we need it HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class); assertNotNull(req); // for unit testing assertEquals("bookid=123", body); // send a html response exchange.getOut().setBody("<html><body>Book 123 is Camel in Action</body></html>"); } } 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. from("jetty:" + serverUri) .choice() .when().simple("${header.one}").to("mock:one") .otherwise() .to("mock:other"); So if a client sends the 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-- Session SupportThe 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: <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:
<bean id="myCode"class="com.mycompany.MyCodeProcessor"/>
Where the processor implementation can access the HttpSession as follows: public void process(Exchange exchange) throws Exception { HttpSession session = exchange.getIn(HttpMessage.class).getRequest().getSession(); ... } SSL Support (HTTPS)Using the JSSE Configuration UtilityAs of Camel 2.8, the 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 componentKeyStoreParameters 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
...
<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 DirectlyJetty provides SSL support out of the box. To enable Jetty to run in SSL mode, simply format the URI with the https:// prefix---for example:
<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: until Camel 2.2
from Camel 2.3 onwards
For details of how to configure SSL on a Jetty endpoint, read the following documentation at the Jetty Site: http://docs.codehaus.org/display/JETTY/How+to+configure+SSL 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 <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 <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 * <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 propertiesAvailable as of 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). <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 reference to the X509CertificateJetty stores a reference to the certificate in the HttpServletRequest which you can access from code as follows:
HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class);
X509Certificate cert = (X509Certificate) req.getAttribute("javax.servlet.request.X509Certificate")
Configuring general HTTP propertiesAvailable as of 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). <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> Default behavior for returning HTTP status codesThe 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. Customizing HttpBindingBy 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: public class MyHttpBinding extends DefaultHttpBinding { public MyHttpBinding(HttpEndpoint ep) { super(ep); } @Override public void doWriteExceptionResponse(Throwable exception, HttpServletResponse response) throws IOException { // we override the doWriteExceptionResponse as we only want to alter the binding how exceptions is // written back to the client. // we just return HTTP 200 so the client thinks its okay response.setStatus(200); // and we return this fixed text response.getWriter().write("Something went wrong but we dont care"); } } We can then create an instance of our binding and register it in the Spring registry as follows:
<bean id="mybinding"class="com.mycompany.MyHttpBinding"/>
And then we can reference this binding when we define the route: <route><from uri="jetty:http://0.0.0.0:8080/myapp/myservice?httpBindingRef=mybinding"/><to uri="bean:doSomething"/></route> Jetty handlers and security configurationYou 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: <-- 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> And from Camel 2.3 onwards you can configure a list of Jetty handlers as follows: <-- 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:
from("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 return a custom HTTP 500 reply messageYou may want to return a custom reply message when something goes wrong, instead of the default reply message Camel Jetty replies with. from("jetty://http://localhost:{{port}}/myserver") // use onException to catch all exceptions and return a custom reply message .onException(Exception.class) .handled(true) // create a custom failure response .transform(constant("Dude something went wrong")) // we must remember to set error code 500 as handled(true) // otherwise would let Camel thing its a OK response (200) .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(500)) .end() // now just force an exception immediately .throwException(new IllegalArgumentException("I cannot do this")); Multi-part Form supportFrom Camel 2.3.0, camel-jetty support to multipart 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. 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 // Set the jetty temp directory which store the file for multi part form // camel-jetty will clean up the file after it handled the request. // The option works rightly from Camel 2.4.0 getContext().getProperties().put("CamelJettyTempDir", "target"); from("jetty://http://localhost:{{port}}/test").process(new Processor() { public void process(Exchange exchange) throws Exception { Message in = exchange.getIn(); assertEquals("Get a wrong attachement size", 1, in.getAttachments().size()); // The file name is attachment id DataHandler data = in.getAttachment("NOTICE.txt"); assertNotNull("Should get the DataHandle NOTICE.txt", data); // This assert is wrong, but the correct content-type (application/octet-stream) // will not be returned until Jetty makes it available - currently the content-type // returned is just the default for FileDataHandler (for the implentation being used) //assertEquals("Get a wrong content type", "text/plain", data.getContentType()); assertEquals("Got the wrong name", "NOTICE.txt", data.getName()); assertTrue("We should get the data from the DataHandle", data.getDataSource() .getInputStream().available() > 0); // The other form date can be get from the message header exchange.getOut().setBody(in.getHeader("comment")); } }); Jetty JMX supportFrom Camel 2.3.0, camel-jetty supports the enabling of Jetty's JMX capabilities at the component and endpoint level with the endpoint configuration taking priority. Note that 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. Because 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. For 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".
<from uri="jetty:https://0.0.0.0/myapp/myservice1/?enableJmx=true"/>
<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 MBeanServer 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. See AlsoJing ComponentThe Jing component uses the Jing Library to perform XML validation of the message body using either Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jing</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
Note that the MSV component can also support RelaxNG XML syntax. URI formatrng:someLocalOrRemoteResource rnc:someLocalOrRemoteResource Where rng means use the RelaxNG XML Syntax whereas rnc means use RelaxNG Compact Syntax. The following examples show possible URI values
You can append query options to the URI in the following format, ?option=value&option=value&... Options
ExampleThe following example shows how to configure a route from the 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 Compact Syntax schema (which is supplied on the classpath). <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:start"/> <doTry> <to uri="rnc:org/apache/camel/component/validator/jing/schema.rnc"/> <to uri="mock:valid"/> <doCatch> <exception>org.apache.camel.ValidationException</exception> <to uri="mock:invalid"/> </doCatch> <doFinally> <to uri="mock:finally"/> </doFinally> </doTry> </route> </camelContext> See AlsoJMS Component
The JMS component allows messages to be sent to (or consumed from) a JMS Queue or Topic. The implementation of the JMS Component uses Spring's JMS support for declarative transactions, using Spring's JmsTemplate for sending and a MessageListenerContainer for consuming. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jms</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI formatjms:[queue:|topic:]destinationName[?options] Where destinationName is a JMS queue or topic name. By default, the destinationName is interpreted as a queue name. For example, to connect to the queue, FOO.BAR use: jms:FOO.BAR You can include the optional queue: prefix, if you prefer: jms:queue:FOO.BAR To connect to a topic, you must include the topic: prefix. For example, to jms:topic:Stocks.Prices You append query options to the URI using the following format, ?option=value&option=value&... NotesUsing ActiveMQThe JMS component reuses Spring 2's JmsTemplate for sending messages. This is not ideal for use in a non-J2EE container and typically requires some caching in the JMS provider to avoid poor performance. If you intend to use Apache ActiveMQ as your Message Broker - which is a good choice as ActiveMQ rocks
Transactions and Cache Levels
If you are not using XA, then you should consider caching as it speeds up performance, such as setting cacheLevelName=CACHE_CONSUMER. Through Camel 2.7.x, the default setting for cacheLevelName is CACHE_CONSUMER. You will need to explicitly set cacheLevelName=CACHE_NONE.
So you can say the default setting is conservative. Consider using cacheLevelName=CACHE_CONSUMER if you are using non-XA transactions. Durable SubscriptionsIf you wish to use durable topic subscriptions, you need to specify both clientId and durableSubscriptionName. The value of the clientId must be unique and can only be used by a single JMS connection instance in your entire network. You may prefer to use Virtual Topics instead to avoid this limitation. More background on durable messaging here. Message Header MappingWhen using message headers, the JMS specification states that header names must be valid Java identifiers. So try to name your headers to be valid Java identifiers. One benefit of doing this is that you can then use your headers inside a JMS Selector (whose SQL92 syntax mandates Java identifier syntax for headers). A simple strategy for mapping header names is used by default. The strategy is to replace any dots and hyphens in the header name as shown below and to reverse the replacement when the header name is restored from a JMS message sent over the wire. What does this mean? No more losing method names to invoke on a bean component, no more losing the filename header for the File Component, and so on. The current header name strategy for accepting header names in Camel is as follows:
OptionsYou can configure many different properties on the JMS endpoint which map to properties on the JMSConfiguration POJO.
The options are divided into two tables, the first one with the most common options used. The latter contains the rest. Most commonly used options
All the other options
Message Mapping between JMS and CamelCamel automatically maps messages between javax.jms.Message and org.apache.camel.Message. When sending a JMS message, Camel converts the message body to the following JMS message types:
When receiving a JMS message, Camel converts the JMS message to the following body type:
Disabling auto-mapping of JMS messagesYou can use the mapJmsMessage option to disable the auto-mapping above. If disabled, Camel will not try to map the received JMS message, but instead uses it directly as the payload. This allows you to avoid the overhead of mapping and let Camel just pass through the JMS message. For instance, it even allows you to route javax.jms.ObjectMessage JMS messages with classes you do not have on the classpath. Using a custom MessageConverterYou can use the messageConverter option to do the mapping yourself in a Spring org.springframework.jms.support.converter.MessageConverter class. For example, in the route below we use a custom message converter when sending a message to the JMS order queue:
from("file://inbox/order").to("jms:queue:order?messageConverter=#myMessageConverter");
You can also use a custom message converter when consuming from a JMS destination. Controlling the mapping strategy selectedYou can use the jmsMessageType option on the endpoint URL to force a specific message type for all messages.
from("file://inbox/order").to("jms:queue:order?jmsMessageType=Text");
You can also specify the message type to use for each messabe by setting the header with the key CamelJmsMessageType. For example:
from("file://inbox/order").setHeader("CamelJmsMessageType", JmsMessageType.Text).to("jms:queue:order");
The possible values are defined in the enum class, org.apache.camel.jms.JmsMessageType. Message format when sendingThe exchange that is sent over the JMS wire must conform to the JMS Message spec. For the exchange.in.header the following rules apply for the header keys:
For the exchange.in.header, the following rules apply for the header values:
Camel will log with category org.apache.camel.component.jms.JmsBinding at DEBUG level if it drops a given header value. For example:
2008-07-09 06:43:04,046 [main ] DEBUG JmsBinding
- Ignoring non primitive header: order of class: org.apache.camel.component.jms.issues.DummyOrder with value: DummyOrder{orderId=333, itemId=4444, quantity=2}
Message format when receivingCamel adds the following properties to the Exchange when it receives a message:
Camel adds the following JMS properties to the In message headers when it receives a JMS message:
As all the above information is standard JMS you can check the JMS documentation for further details. About using Camel to send and receive messages and JMSReplyToThe JMS component is complex and you have to pay close attention to how it works in some cases. So this is a short summary of some of the areas/pitfalls to look for. When Camel sends a message using its JMSProducer, it checks the following conditions:
All this can be a tad complex to understand and configure to support your use case. JmsProducerThe JmsProducer behaves as follows, depending on configuration:
JmsConsumerThe JmsConsumer behaves as follows, depending on configuration:
So pay attention to the message exchange pattern set on your exchanges. If you send a message to a JMS destination in the middle of your route you can specify the exchange pattern to use, see more at Request Reply. from("activemq:queue:in") .to("bean:validateOrder") .to(ExchangePattern.InOnly, "activemq:topic:order") .to("bean:handleOrder"); Reuse endpoint and send to different destinations computed at runtimeIf you need to send messages to a lot of different JMS destinations, it makes sense to reuse a JMS endpoint and specify the real destination in a message header. This allows Camel to reuse the same endpoint, but send to different destinations. This greatly reduces the number of endpoints created and economizes on memory and thread resources. You can specify the destination in the following headers:
For example, the following route shows how you can compute a destination at run time and use it to override the destination appearing in the JMS URL: from("file://inbox") .to("bean:computeDestination") .to("activemq:queue:dummy"); The queue name, dummy, is just a placeholder. It must be provided as part of the JMS endpoint URL, but it will be ignored in this example. In the computeDestination bean, specify the real destination by setting the CamelJmsDestinationName header as follows: public void setJmsHeader(Exchange exchange) { String id = .... exchange.getIn().setHeader("CamelJmsDestinationName", "order:" + id"); } Then Camel will read this header and use it as the destination instead of the one configured on the endpoint. So, in this example Camel sends the message to activemq:queue:order:2, assuming the id value was 2. If both the CamelJmsDestination and the CamelJmsDestinationName headers are set, CamelJmsDestination takes priority. Configuring different JMS providersYou can configure your JMS provider in Spring XML as follows: <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <jmxAgent id="agent" disabled="true"/> </camelContext> <bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent"> <property name="connectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="vm://localhost?broker.persistent=false&broker.useJmx=false"/> </bean> </property> </bean> Basically, you can configure as many JMS component instances as you wish and give them a unique name using the id attribute. The preceding example configures an activemq component. You could do the same to configure MQSeries, TibCo, BEA, Sonic and so on. Once you have a named JMS component, you can then refer to endpoints within that component using URIs. For example for the component name, activemq, you can then refer to destinations using the URI format, activemq:[queue:|topic:]destinationName. You can use the same approach for all other JMS providers. This works by the SpringCamelContext lazily fetching components from the spring context for the scheme name you use for Endpoint URIs and having the Component resolve the endpoint URIs. Using JNDI to find the ConnectionFactoryIf you are using a J2EE container, you might need to look up JNDI to find the JMS ConnectionFactory rather than use the usual <bean> mechanism in Spring. You can do this using Spring's factory bean or the new Spring XML namespace. For example: <bean id="weblogic" class="org.apache.camel.component.jms.JmsComponent"> <property name="connectionFactory" ref="myConnectionFactory"/> </bean> <jee:jndi-lookup id="myConnectionFactory" jndi-name="jms/connectionFactory"/> See The jee schema in the Spring reference documentation for more details about JNDI lookup. Concurrent ConsumingA common requirement with JMS is to consume messages concurrently in multiple threads in order to make an application more responsive. You can set the concurrentConsumers option to specify the number of threads servicing the JMS endpoint, as follows:
from("jms:SomeQueue?concurrentConsumers=20").
bean(MyClass.class);
You can configure this option in one of the following ways:
Request-reply over JMSCamel supports Request Reply over JMS. In essence the MEP of the Exchange should be InOut when you send a message to a JMS queue. Camel offers a number of options to configure request/reply over JMS that influence performance and clustered environments. The table below summaries the options.
The JmsProducer detects the InOut and provides a JMSReplyTo header with the reply destination to be used. By default Camel uses a temporary queue, but you can use the replyTo option on the endpoint to specify a fixed reply queue (see more below about fixed reply queue). Camel will automatic setup a consumer which listen on the reply queue, so you should not do anything.
from(xxx)
.inOut().to("activemq:queue:foo")
.threads(5)
.to(yyy)
.to(zzz);
In this route we instruct Camel to route replies asynchronously using a thread pool with 5 threads. From Camel 2.10.3 onwards you can now configure the listener to use concurrent threads using the concurrentConsumers and maxConcurrentConsumers options. This allows you to easier configure this in Camel as shown below:
from(xxx)
.inOut().to("activemq:queue:foo?concurrentConsumers=5")
.to(yyy)
.to(zzz);
Request-reply over JMS and using a shared fixed reply queueIf you use a fixed reply queue when doing Request Reply over JMS as shown in the example below, then pay attention.
from(xxx)
.inOut().to("activemq:queue:foo?replyTo=bar")
.to(yyy)
In this example the fixed reply queue named "bar" is used. By default Camel assumes the queue is shared when using fixed reply queues, and therefore it uses a JMSSelector to only pickup the expected reply messages (eg based on the JMSCorrelationID). See next section for exclusive fixed reply queues. That means its not as fast as temporary queues. You can speedup how often Camel will pull for reply messages using the receiveTimeout option. By default its 1000 millis. So to make it faster you can set it to 250 millis to pull 4 times per second as shown:
from(xxx)
.inOut().to("activemq:queue:foo?replyTo=bar&receiveTimeout=250")
.to(yyy)
Notice this will cause the Camel to send pull requests to the message broker more frequent, and thus require more network traffic. Request-reply over JMS and using an exclusive fixed reply queueAvailable as of Camel 2.9 In the previous example, Camel would anticipate the fixed reply queue named "bar" was shared, and thus it uses a JMSSelector to only consume reply messages which it expects. However there is a drawback doing this as JMS selectos is slower. Also the consumer on the reply queue is slower to update with new JMS selector ids. In fact it only updates when the receiveTimeout option times out, which by default is 1 second. So in theory the reply messages could take up till about 1 sec to be detected. On the other hand if the fixed reply queue is exclusive to the Camel reply consumer, then we can avoid using the JMS selectors, and thus be more performant. In fact as fast as using temporary queues. So in Camel 2.9 onwards we introduced the ReplyToType option which you can configure to Exclusive
from(xxx)
.inOut().to("activemq:queue:foo?replyTo=bar&replyToType=Exclusive")
.to(yyy)
Mind that the queue must be exclusive to each and every endpoint. So if you have two routes, then they each need an unique reply queue as shown in the next example: from(xxx) .inOut().to("activemq:queue:foo?replyTo=bar&replyToType=Exclusive") .to(yyy) from(aaa) .inOut().to("activemq:queue:order?replyTo=order.reply&replyToType=Exclusive") .to(bbb) The same applies if you run in a clustered environment. Then each node in the cluster must use an unique reply queue name. As otherwise each node in the cluster may pickup messages which was intended as a reply on another node. For clustered environments its recommended to use shared reply queues instead. Synchronizing clocks between senders and receiversWhen doing messaging between systems, its desirable that the systems have synchronized clocks. For example when sending a JMS message, then you can set a time to live value on the message. Then the receiver can inspect this value, and determine if the message is already expired, and thus drop the message instead of consume and process it. However this requires that both sender and receiver have synchronized clocks. If you are using ActiveMQ then you can use the timestamp plugin to synchronize clocks. About time to liveRead first above about synchronized clocks. When you do request/reply (InOut) over JMS with Camel then Camel uses a timeout on the sender side, which is default 20 seconds from the requestTimeout option. You can control this by setting a higher/lower value. However the time to live value is still set on the JMS message being send. So that requires the clocks to be synchronized between the systems. If they are not, then you may want to disable the time to live value being set. This is now possible using the disableTimeToLive option from Camel 2.8 onwards. So if you set this option to disableTimeToLive=true, then Camel does not set any time to live value when sending JMS messages. But the request timeout is still active. So for example if you do request/reply over JMS and have disabled time to live, then Camel will still use a timeout by 20 seconds (the requestTimeout option). That option can of course also be configured. So the two options requestTimeout and disableTimeToLive gives you fine grained control when doing request/reply. When you do fire and forget (InOut) over JMS with Camel then Camel by default does not set any time to live value on the message. You can configure a value by using the timeToLive option. For example to indicate a 5 sec., you set timeToLive=5000. The option disableTimeToLive can be used to force disabling the time to live, also for InOnly messaging. The requestTimeout option is not being used for InOnly messaging. Enabling Transacted ConsumptionA common requirement is to consume from a queue in a transaction and then process the message using the Camel route. To do this, just ensure that you set the following properties on the component/endpoint:
See the Transactional Client EIP pattern for further details.
Available as of Camel 2.10 You can leverage the DMLC transacted session API using the following properties on component/endpoint:
The benefit of doing so is that the cacheLevel setting will be honored when using local transactions without a configured TransactionManager. When a TransactionManager is configured, no caching happens at DMLC level and its necessary to rely on a pooled connection factory. For more details about this kind of setup see here and here. Using JMSReplyTo for late repliesWhen using Camel as a JMS listener, it sets an Exchange property with the value of the ReplyTo javax.jms.Destination object, having the key ReplyTo. You can obtain this Destination as follows: Destination replyDestination = exchange.getIn().getHeader(JmsConstants.JMS_REPLY_DESTINATION, Destination.class); And then later use it to send a reply using regular JMS or Camel.
// we need to pass in the JMS component, and in this sample we use ActiveMQ
JmsEndpoint endpoint = JmsEndpoint.newInstance(replyDestination, activeMQComponent);
// now we have the endpoint we can use regular Camel API to send a message to it
template.sendBody(endpoint, "Here is the late reply.");
A different solution to sending a reply is to provide the replyDestination object in the same Exchange property when sending. Camel will then pick up this property and use it for the real destination. The endpoint URI must include a dummy destination, however. For example:
// we pretend to send it to some non existing dummy queue
template.send("activemq:queue:dummy, new Processor() {
public void process(Exchange exchange) throws Exception {
// and here we override the destination with the ReplyTo destination object so the message is sent to there instead of dummy
exchange.getIn().setHeader(JmsConstants.JMS_DESTINATION, replyDestination);
exchange.getIn().setBody("Here is the late reply.");
}
}
Using a request timeoutIn the sample below we send a Request Reply style message Exchange (we use the requestBody method = InOut) to the slow queue for further processing in Camel and we wait for a return reply: // send a in-out with a timeout for 5 sec Object out = template.requestBody("activemq:queue:slow?requestTimeout=5000", "Hello World"); SamplesJMS is used in many examples for other components as well. But we provide a few samples below to get started. Receiving from JMSIn the following sample we configure a route that receives JMS messages and routes the message to a POJO: from("jms:queue:foo"). to("bean:myBusinessLogic"); You can of course use any of the EIP patterns so the route can be context based. For example, here's how to filter an order topic for the big spenders: from("jms:topic:OrdersTopic"). filter().method("myBean", "isGoldCustomer"). to("jms:queue:BigSpendersQueue"); Sending to a JMSIn the sample below we poll a file folder and send the file content to a JMS topic. As we want the content of the file as a TextMessage instead of a BytesMessage, we need to convert the body to a String: from("file://orders"). convertBodyTo(String.class). to("jms:topic:OrdersTopic"); Using AnnotationsCamel also has annotations so you can use POJO Consuming and POJO Producing. Spring DSL sampleThe preceding examples use the Java DSL. Camel also supports Spring XML DSL. Here is the big spender sample using Spring DSL: <route> <from uri="jms:topic:OrdersTopic"/> <filter> <method bean="myBean" method="isGoldCustomer"/> <to uri="jms:queue:BigSpendersQueue"/> </filter> </route> Other samplesJMS appears in many of the examples for other components and EIP patterns, as well in this Camel documentation. So feel free to browse the documentation. If you have time, check out the this tutorial that uses JMS but focuses on how well Spring Remoting and Camel works together Tutorial-JmsRemoting. Using JMS as a Dead Letter Queue storing ExchangeNormally, when using JMS as the transport, it only transfers the body and headers as the payload. If you want to use JMS with a Dead Letter Channel, using a JMS queue as the Dead Letter Queue, then normally the caused Exception is not stored in the JMS message. You can, however, use the transferExchange option on the JMS dead letter queue to instruct Camel to store the entire Exchange in the queue as a javax.jms.ObjectMessage that holds a org.apache.camel.impl.DefaultExchangeHolder. This allows you to consume from the Dead Letter Queue and retrieve the caused exception from the Exchange property with the key Exchange.EXCEPTION_CAUGHT. The demo below illustrates this: // setup error handler to use JMS as queue and store the entire Exchange errorHandler(deadLetterChannel("jms:queue:dead?transferExchange=true")); Then you can consume from the JMS queue and analyze the problem: from("jms:queue:dead").to("bean:myErrorAnalyzer"); // and in our bean String body = exchange.getIn().getBody(); Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); // the cause message is String problem = cause.getMessage(); Using JMS as a Dead Letter Channel storing error onlyYou can use JMS to store the cause error message or to store a custom body, which you can initialize yourself. The following example uses the Message Translator EIP to do a transformation on the failed exchange before it is moved to the JMS dead letter queue: // we sent it to a seda dead queue first errorHandler(deadLetterChannel("seda:dead")); // and on the seda dead queue we can do the custom transformation before its sent to the JMS queue from("seda:dead").transform(exceptionMessage()).to("jms:queue:dead"); Here we only store the original cause error message in the transform. You can, however, use any Expression to send whatever you like. For example, you can invoke a method on a Bean or use a custom processor. Sending an InOnly message and keeping the JMSReplyTo headerWhen sending to a JMS destination using camel-jms the producer will use the MEP to detect if its InOnly or InOut messaging. However there can be times where you want to send an InOnly message but keeping the JMSReplyTo header. To do so you have to instruct Camel to keep it, otherwise the JMSReplyTo header will be dropped. For example to send an InOnly message to the foo queue, but with a JMSReplyTo with bar queue you can do as follows:
template.send("activemq:queue:foo?preserveMessageQos=true", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody("World");
exchange.getIn().setHeader("JMSReplyTo", "bar");
}
});
Notice we use preserveMessageQos=true to instruct Camel to keep the JMSReplyTo header. Setting JMS provider options on the destinationSome JMS providers, like IBM's WebSphere MQ need options to be set on the JMS destination. For example, you may need to specify the targetClient option. Since targetClient is a WebSphere MQ option and not a Camel URI option, you need to set that on the JMS destination name like so: ... .setHeader("CamelJmsDestinationName", constant("queue:///MY_QUEUE?targetClient=1")) .to("wmq:queue:MY_QUEUE?useMessageIDAsCorrelationID=true"); Some versions of WMQ won't accept this option on the destination name and you will get an exception like:
A workaround is to use a custom DestinationResolver: JmsComponent wmq = new JmsComponent(connectionFactory); wmq.setDestinationResolver(new DestinationResolver(){ public Destination resolveDestinationName(Session session, String destinationName, boolean pubSubDomain) throws JMSException { MQQueueSession wmqSession = (MQQueueSession) session; return wmqSession.createQueue("queue:///" + destinationName + "?targetClient=1"); } }); See AlsoJMX ComponentAvailable as of Camel 2.6 Standard JMX Consumer ConfigurationComponent allows consumers to subscribe to an mbean's Notifications. The component supports passing the Notification object directly through the Exchange or serializing it to XML according to the schema provided within this project. This is a consumer only component. Exceptions are thrown if you attempt to create a producer for it. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jmx</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI FormatThe component can connect to the local platform mbean server with the following URI:
jmx://platform?options
A remote mbean server url can be provided following the initial JMX scheme like so:
jmx:service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi?options
You can append query options to the URI in the following format, ?options=value&option2=value&... URI Options
ObjectName ConstructionThe URI must always have the objectDomain property. In addition, the URI must contain either objectName or one or more properties that start with "key." Domain with Name propertyWhen the objectName property is provided, the following constructor is used to build the ObjectName? for the mbean: ObjectName(String domain, String key, String value) The key value in the above will be "name" and the value will be the value of the objectName property. Domain with HashtableObjectName(String domain, Hashtable<String,String> table) The Hashtable is constructed by extracting properties that start with "key." The properties will have the "key." prefixed stripped prior to building the Hashtable. This allows the URI to contain a variable number of properties to identify the mbean. Examplefrom("jmx:platform?objectDomain=jmxExample&key.name=simpleBean"). to("log:jmxEvent"); Monitor Type ConsumerAvailable as of Camel 2.8 CounterMonitor monitor = new CounterMonitor(); monitor.addObservedObject(makeObjectName("simpleBean")); monitor.setObservedAttribute("MonitorNumber"); monitor.setNotify(true); monitor.setInitThreshold(1); monitor.setGranularityPeriod(500); registerBean(monitor, makeObjectName("counter")); monitor.start(); The 2.8 version introduces a new type of consumer that automatically creates and registers a monitor bean for the specified objectName and attribute. Additional endpoint attributes allow the user to specify the attribute to monitor, type of monitor to create, and any other required properties. The code snippet above is condensed into a set of endpoint properties. The consumer uses these properties to create the CounterMonitor, register it, and then subscribe to its changes. All of the JMX monitor types are supported. Examplefrom("jmx:platform?objectDomain=myDomain&objectName=simpleBean&" + "monitorType=counter&observedAttribute=MonitorNumber&initThreshold=1&" + "granularityPeriod=500").to("mock:sink"); The example above will cause a new Monitor Bean to be created and depoyed to the local mbean server that monitors the "MonitorNumber" attribute on the "simpleBean." Additional types of monitor beans and options are detailed below. The newly deployed monitor bean is automatically undeployed when the consumer is stopped. URI Options for Monitor Type
The monitor style consumer is only supported for the local mbean server. JMX does not currently support remote deployment of mbeans without either having the classes already remotely deployed or an adapter library on both the client and server to facilitate a proxy deployment. See AlsoJPA ComponentThe 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: <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 endpointYou 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 does not contain one of the previous listed types, put a Message Translator in front of the endpoint to perform the necessary conversion first. Consuming from the endpointConsuming 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). URI formatjpa:[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
Message HeadersCamel adds the following message headers to the exchange:
Configuring EntityManagerFactoryIts 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: <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 TransactionManagerIts strongly advised to configure the TransactionManager instance used by the JPA component. If failed to do so each JpaEndpoint will auto create their own instance of TransactionManager which most often is not what you want. For example, you can instantiate a JPA component that references the myTransactionManager transaction manager, as follows: <bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent"> <property name="entityManagerFactory" ref="myEMFactory"/> <property name="transactionManager" ref="myTransactionManager"/> </bean> In Camel 2.3 the JpaComponent will auto lookup the TransactionManager 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. Using a consumer with a named queryFor 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 queryFor 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 queryFor 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. ExampleSee Tracer Example for an example using JPA to store traced messages into a database. Using the JPA based idempotent repositoryIn this section we will use the JPA based idempotent repository. First we need to setup a persistence-unit in the persistence.xml file: <persistence-unit name="idempotentDb" transaction-type="RESOURCE_LOCAL"> <class>org.apache.camel.processor.idempotent.jpa.MessageProcessed</class> <properties> <property name="openjpa.ConnectionURL" value="jdbc:derby:target/idempotentTest;create=true"/> <property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.EmbeddedDriver"/> <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/> <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO"/> </properties> </persistence-unit> Second we have to setup a org.springframework.orm.jpa.JpaTemplate which is used by the org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository: <!-- this is standard spring JPA configuration --> <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate"> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <!-- we use idempotentDB as the persitence unit name defined in the persistence.xml file --> <property name="persistenceUnitName" value="idempotentDb"/> </bean> Afterwards we can configure our org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository: <!-- we define our jpa based idempotent repository we want to use in the file consumer --> <bean id="jpaStore" class="org.apache.camel.processor.idempotent.jpa.JpaMessageIdRepository"> <!-- Here we refer to the spring jpaTemplate --> <constructor-arg index="0" ref="jpaTemplate"/> <!-- This 2nd parameter is the name (= a cateogry name). You can have different repositories with different names --> <constructor-arg index="1" value="FileConsumer"/> </bean> And finally we can create our JPA idempotent repository in the spring XML file as well: <camel:camelContext> <camel:route id="JpaMessageIdRepositoryTest"> <camel:from uri="direct:start" /> <camel:idempotentConsumer messageIdRepositoryRef="jpaStore"> <camel:header>messageId</camel:header> <camel:to uri="mock:result" /> </camel:idempotentConsumer> </camel:route> </camel:camelContext>
See AlsoJT/400 ComponentThe jt400 component allows you to exchanges messages with an AS/400 system using data queues. Maven users will need to add the following dependency to their pom.xml for this component: <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-jt400</artifactId> <version>x.x.x</version> <!-- use the same version as your Camel core version --> </dependency> URI format
jt400://user:password@system/QSYS.LIB/LIBRARY.LIB/QUEUE.DTAQ[?options]
To call remote program (Camel 2.7)
jt400://user:password@system/QSYS.LIB/LIBRARY.LIB/program.PGM[?options]
You can append query options to the URI in the following format, ?option=value&option=value&... URI optionsFor the data queue message exchange:
For the remote program call (Camel 2.7)
|