camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Writing Components
Date Tue, 07 Aug 2012 09:49:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Writing+Components">Writing
Components</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~hekonsek">Henryk
Konsek</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Explained what exactly should be added to the service autodiscovery file.<br />
    </div>
        <br/>
                         <h4>Changes (2)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>* write a POJO which implements
the [Component|http://activemq.apache.org/camel/maven/current/camel-core/apidocs/org/apache/camel/Component.html]
interface. The simplest approach is just to derive from [DefaultComponent|http://activemq.apache.org/camel/maven/current/camel-core/apidocs/org/apache/camel/impl/DefaultComponent.html]
<br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">*
to support auto-discovery of your component add a file to _META-INF/services/org/apache/camel/component/FOO_
where FOO is the URI scheme for your component and any related endpoints created on the fly
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">*
to support auto-discovery of your component add a file to _META-INF/services/org/apache/camel/component/FOO_
where FOO is the URI scheme for your component and any related endpoints created on the fly.
The latter file should contain the definition of the component class. For example if your
component is implemented by the _com.example.CustomComponent_ class, the service file should
contain the following line -  _class=com.example.CustomComponent_ . <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Users can then either explicitly
create your component, configure it and register with a [CamelContext|http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/CamelContext.html]
or users can use a URI which auto-creates your component. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="WritingComponents-WritingComponents"></a>Writing Components</h2>

<p>Apache Camel is designed to make it very easy to drop in new components whether they
be routing components, transformers, transports etc. The idea of a component is to be a factory
and manager of <a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoints</a>.</p>

<p>Here are the main steps to writing a component.</p>

<ul>
	<li>write a POJO which implements the <a href="http://activemq.apache.org/camel/maven/current/camel-core/apidocs/org/apache/camel/Component.html"
class="external-link" rel="nofollow">Component</a> interface. The simplest approach
is just to derive from <a href="http://activemq.apache.org/camel/maven/current/camel-core/apidocs/org/apache/camel/impl/DefaultComponent.html"
class="external-link" rel="nofollow">DefaultComponent</a></li>
	<li>to support auto-discovery of your component add a file to <em>META-INF/services/org/apache/camel/component/FOO</em>
where FOO is the URI scheme for your component and any related endpoints created on the fly.
The latter file should contain the definition of the component class. For example if your
component is implemented by the <em>com.example.CustomComponent</em> class, the
service file should contain the following line -  <em>class=com.example.CustomComponent</em>
.</li>
</ul>


<p>Users can then either explicitly create your component, configure it and register
with a <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/CamelContext.html"
class="external-link" rel="nofollow">CamelContext</a> or users can use a URI which
auto-creates your component.</p>

<h3><a name="WritingComponents-WritingEndpoints"></a>Writing Endpoints</h3>

<p>When implementing an <a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a>
you typically may implement one or more of the following methods</p>
<ul>
	<li><a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Endpoint.html#createProducer()"
class="external-link" rel="nofollow">createProducer()</a> will create a <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Producer.html"
class="external-link" rel="nofollow">Producer</a> for sending message exchanges to
the endpoint</li>
	<li><a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Endpoint.html#createConsumer(org.apache.camel.Processor)"
class="external-link" rel="nofollow">createConsumer()</a> implements the <a href="/confluence/display/CAMEL/Event+Driven+Consumer"
title="Event Driven Consumer">Event Driven Consumer</a> pattern for consuming message
exchanges from the endpoint via a <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Processor.html"
class="external-link" rel="nofollow">Processor</a> when creating a <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Consumer.html"
class="external-link" rel="nofollow">Consumer</a></li>
	<li><a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Endpoint.html#createPollingConsumer()"
class="external-link" rel="nofollow">createPollingConsumer()</a> implements the <a
href="/confluence/display/CAMEL/Polling+Consumer" title="Polling Consumer">Polling Consumer</a>
pattern for consuming message exchanges from the endpoint via a <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/PollingConsumer.html"
class="external-link" rel="nofollow">PollingConsumer</a></li>
</ul>


<p>Typically you just derive from <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/impl/DefaultEndpoint.html"
class="external-link" rel="nofollow">DefaultEndpoint</a> and implement the <a
href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Endpoint.html#createProducer()"
class="external-link" rel="nofollow">createProducer()</a> and / or <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Endpoint.html#createConsumer(org.apache.camel.Processor)"
class="external-link" rel="nofollow">createConsumer()</a> methods. The <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Endpoint.html#createPollingConsumer()"
class="external-link" rel="nofollow">createPollingConsumer()</a> method will be created
by default for you in the <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/impl/DefaultEndpoint.html"
class="external-link" rel="nofollow">DefaultEndpoint</a> class.</p>

<p>If your endpoint is a polling-centric component you can derive from <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/impl/DefaultPollingEndpoint.html"
class="external-link" rel="nofollow">DefaultPollingEndpoint</a> and then implement
<a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Endpoint.html#createPollingConsumer()"
class="external-link" rel="nofollow">createPollingConsumer()</a>; the <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Endpoint.html#createConsumer(org.apache.camel.Processor)"
class="external-link" rel="nofollow">createConsumer()</a>  method will be created
for you which avoids you having to write any polling code.</p>

<h3><a name="WritingComponents-UsingaProcessor"></a>Using a Processor</h3>

<p>If you are writing a simple endpoint which just processes messages in some way, you
can just implement a <a href="/confluence/display/CAMEL/Processor" title="Processor">Processor</a>
and <a href="/confluence/display/CAMEL/Processor" title="Processor">use that to create
an endpoint</a>.</p>

<h3><a name="WritingComponents-Dependencyinjectionandautodiscovery"></a>Dependency
injection and auto-discovery</h3>

<p>When using auto-discovery the CamelContext will default to its <a href="/confluence/display/CAMEL/Injector"
title="Injector">Injector</a> implementation to inject any required or optional dependencies
on the component. This allows you to use auto-discovery of components via <a href="/confluence/display/CAMEL/URIs"
title="URIs">URIs</a> while still getting the benefits of dependency injection.</p>

<p>For example your component can depend on a JDBC DataSource or JMS ConnectionFactory
which can be provided in the ApplicationContext in Spring or Module in Guice.</p>

<p>So you can if you prefer configure your Component using an IoC framework like Spring
or Guice; then add it to the CamelContext. Or you can let the Component auto-inject itself
as the endpoints are auto-discovered.</p>

<h3><a name="WritingComponents-Options"></a>Options</h3>

<p>If your component has options you can let it have public getters/setters and Camel
will automatically set the properties when the endpoint is created. If you however want to
take the matter in your own hands, then you must remove the option from the given parameter
list as Camel will validate that all options are used. If not Camel will throw a ResolveEndpointFailedException
stating some of the options are unknown.</p>

<p>The parameters is provided by Camel in the createEndpoint method from DefaultComponent:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">protected</span> <span class="code-keyword">abstract</span>
Endpoint&lt;E&gt; createEndpoint(<span class="code-object">String</span>
uri, <span class="code-object">String</span> remaining, Map parameters)
</pre>
</div></div>

<p>The code is an example from the SEDA component that removes the size parameter:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    <span class="code-keyword">public</span> BlockingQueue&lt;Exchange&gt;
createQueue(<span class="code-object">String</span> uri, Map parameters) {
        <span class="code-object">int</span> size = 1000;
        <span class="code-object">Object</span> value = parameters.remove(<span
class="code-quote">"size"</span>);
        <span class="code-keyword">if</span> (value != <span class="code-keyword">null</span>)
{
            <span class="code-object">Integer</span> i = convertTo(<span class="code-object">Integer</span>.class,
value);
            <span class="code-keyword">if</span> (i != <span class="code-keyword">null</span>)
{
                size = i;
            }
        }
        <span class="code-keyword">return</span> <span class="code-keyword">new</span>
LinkedBlockingQueue&lt;Exchange&gt;(size);
    }
</pre>
</div></div>

<h3><a name="WritingComponents-SeeAlso"></a>See Also</h3>

<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Writing+Components">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=50718&revisedVersion=20&originalVersion=19">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Writing+Components?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message