camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Scripting Languages Context
Date Wed, 09 Jun 2010 04:35:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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/Scripting+Languages+Context">Scripting
Languages Context</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~njiang">willem
jiang</a>
    </h4>
        <br/>
                         <h4>Changes (1)</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>|| Attribute || Type ||
Value ||  <br></td></tr>
            <tr><td class="diff-changed-lines" >| <span class="diff-changed-words">c<span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">amelC</span>ontext</span>
| {{org.apache.camel.CamelContext}} | The Camel Context | <br></td></tr>
            <tr><td class="diff-unchanged" >| exchange | {{org.apache.camel.Exchange}}
| The current Exchange | <br>| request | {{org.apache.camel.Message}} | The IN message
| <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h3><a name="ScriptingLanguagesContext-ScriptContext"></a>ScriptContext</h3>
<p>The JSR-223 scripting languages ScriptContext is pre configured with the following
attributes all set at <tt>ENGINE_SCOPE</tt>:</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Attribute </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Value </th>
</tr>
<tr>
<td class='confluenceTd'> context </td>
<td class='confluenceTd'> <tt>org.apache.camel.CamelContext</tt> </td>
<td class='confluenceTd'> The Camel Context </td>
</tr>
<tr>
<td class='confluenceTd'> exchange </td>
<td class='confluenceTd'> <tt>org.apache.camel.Exchange</tt> </td>
<td class='confluenceTd'> The current Exchange </td>
</tr>
<tr>
<td class='confluenceTd'> request </td>
<td class='confluenceTd'> <tt>org.apache.camel.Message</tt> </td>
<td class='confluenceTd'> The IN message </td>
</tr>
<tr>
<td class='confluenceTd'> response </td>
<td class='confluenceTd'> <tt>org.apache.camel.Message</tt> </td>
<td class='confluenceTd'> The OUT message </td>
</tr>
</tbody></table>
</div>



<h3><a name="ScriptingLanguagesContext-Attributes"></a>Attributes</h3>

<p>You can add your own attributes with the <tt>attribute(name, value)</tt>
DSL method, such as:</p>

<p>In the sample below we add an attribute <tt>user</tt> that is an object
we already have instantiated as myUser. This object has a getFirstName() method that we want
to set as header on the message. We use the groovy language to concat the first and last name
into a single string that is returned.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:in"</span>).setHeader(<span class="code-quote">"name"</span>).groovy(<span
class="code-quote">"'$user.firstName $user.lastName'"</span>).attribute(<span
class="code-quote">"user"</span>, myUser).to(<span class="code-quote">"seda:users"</span>);
</pre>
</div></div>

<h3><a name="ScriptingLanguagesContext-Anyscriptinglanguage"></a>Any scripting
language</h3>
<p>Camel can run any JSR-223 scripting languages using the <tt>script</tt>
DSL method such as:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:in"</span>).setHeader(<span class="code-quote">"firstName"</span>).script(<span
class="code-quote">"jaskel"</span>, <span class="code-quote">"user.firstName"</span>).attribute(<span
class="code-quote">"user"</span>, myUser).to(<span class="code-quote">"seda:users"</span>);
</pre>
</div></div>

<p>This is a bit different using the Spring DSL where you use the <b>expression</b>
element that doesn't support setting attributes (yet):</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:in"</span>/&gt;</span>
    <span class="code-tag">&lt;setHeader headerName=<span class="code-quote">"firstName"</span>&gt;</span>
        <span class="code-tag">&lt;expression language=<span class="code-quote">"jaskel"</span>&gt;</span>user.firstName<span
class="code-tag">&lt;/expression&gt;</span>
    <span class="code-tag">&lt;/setHeader&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"seda:users"</span>/&gt;</span>
</pre>
</div></div>

<h3><a name="ScriptingLanguagesContext-Dependencies"></a>Dependencies</h3>

<p>To use scripting languages in your camel routes you need to add the a dependency
on <b>camel-script</b> which integrates the JSR-223 scripting engine. </p>

<p>If you use maven you could just add the following to your pom.xml, substituting the
version number for the latest &amp; greatest release (see <a href="/confluence/display/CAMEL/Download"
title="Download">the download page for the latest versions</a>).</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
  &lt;artifactId&gt;camel-script&lt;/artifactId&gt;
  &lt;version&gt;1.4.0&lt;/version&gt;
&lt;/dependency&gt;
</pre>
</div></div>
    </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/Scripting+Languages+Context">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=97905&revisedVersion=8&originalVersion=7">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Scripting+Languages+Context?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message