Configuring Camel

How do I add a component?

You might first want to read Writing Components for a background in how to implement a new component. Typically it means you write an implementation of the Component interface, usually deriving from DefaultComponent.

You can then register your component explicitly via:

CamelContext context = new DefaultCamelContext();
context.addComponent("foo", new FooComponent(context));

However you can use the auto-discovery feature of Camel where by Camel will automatically add a Component when an endpoint URI is used. To do this you would create a file called:

/META-INF/services/org/apache/camel/component/foo

with contents:

class=org.acme.FooComponent

(You can add other property configurations in there too if you like.)

Then if you refer to an endpoint as foo://somethingOrOther Camel will auto-discover your component and register it.

The FooComponent can then be auto-injected with resources using the Injector, such as to support Spring based auto-wiring, or to support @Resource (EJB3 style) injection or Guice style @Inject injection.

Working with Spring XML

You can configure a component via Spring using the following mechanism:

<?xml version="1.0" encoding="UTF-8"?> <!--

Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-→ <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.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd ">

<!-- START SNIPPET: example -->
<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <jmxAgent id="agent" disabled="true"/>
</camelContext>
<bean id="activemq" class="org.apache.camel.component.jms.JmsComponent">
  <property name="connectionFactory">
    <bean class="org.apache.activemq.ActiveMQConnectionFactory">
      <property name="brokerURL" value="vm://localhost?broker.persistent=false&amp;broker.useJmx=false"/>
    </bean>
  </property>
</bean>
<!-- END SNIPPET: example -->

</beans>

Which allows you to configure a component using some name (activemq in the above example), then you can refer to the component using activemq:[queue:|topic:]destinationName.

If you want to add explicit Spring 2.x XML objects to your XML then you could use the xbean-spring which tries to automate most of the XML binding work for you; or you could look in camel-spring at CamelNamespaceHandler you’ll see how we handle the Spring XML stuff (warning it’s kinda hairy code to look at :smile:). If you wanted <fooComponent> to be a standard part of the core Camel schema then you’d hack that file to add your component & contribute a patch to the camel XSD. Otherwise you could write your own namespace & schema if you prefer.