Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1087610F11 for ; Tue, 25 Nov 2014 20:19:48 +0000 (UTC) Received: (qmail 72021 invoked by uid 500); 25 Nov 2014 20:19:47 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 71971 invoked by uid 500); 25 Nov 2014 20:19:47 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 71961 invoked by uid 99); 25 Nov 2014 20:19:47 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 25 Nov 2014 20:19:47 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 25 Nov 2014 20:19:45 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 75ACF23889E0 for ; Tue, 25 Nov 2014 20:19:25 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r930484 - in /websites/production/camel/content: cache/main.pageCache camel-and-scr.html Date: Tue, 25 Nov 2014 20:19:25 -0000 To: commits@camel.apache.org From: buildbot@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20141125201925.75ACF23889E0@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: buildbot Date: Tue Nov 25 20:19:25 2014 New Revision: 930484 Log: Production update by buildbot for camel Added: websites/production/camel/content/camel-and-scr.html Modified: websites/production/camel/content/cache/main.pageCache Modified: websites/production/camel/content/cache/main.pageCache ============================================================================== Binary files - no diff available. Added: websites/production/camel/content/camel-and-scr.html ============================================================================== --- websites/production/camel/content/camel-and-scr.html (added) +++ websites/production/camel/content/camel-and-scr.html Tue Nov 25 20:19:25 2014 @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + Apache Camel: Camel and SCR + + + +
+
+
+
+
+
+
+
+
+
+
+ + + + + + + + + + + + +
+

Working with Camel using SCR

SCR stands for Service Component Runtime and is an implementation of OSGi Declarative Services specification (TODO: add a link to spec here). SCR enables any plain old Java object to expose and use OSGi services with no boilerplate code.

OSGi framework knows your object by looking at SCR descriptor files in your bundle which are typically generated from annotations in your code by a plugin such as org.apache.felix:maven-scr-plugin (https://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin.html).

Running Camel in an SCR bundle is a great alternative for Spring DM and Blueprint based solutions having significantly fewer lines of code betw een you and the OSGi framework. Using SCR your bundle can remain completely in Java world; there is no need to edit XML or properties files. This offers you full control over everything and means your IDE of choice knows exactly what is going on in your project.

Camel SCR support

Available as of Camel 2.15.0

org.apache.camel/camel-scr bundle provides a base class, AbstractCamelRunner, which manages a Camel context for you and a helper class, ScrHelper, for using your SCR properties in unit tests. Camel-scr feature for Apache Karaf defines all features and bundles required for running Camel in SCR bundles.

AbstractCamelRunner class ties CamelContext's lifecycle to Service Component's lifecycle and handles configuration with help of Camel's PropertiesComponent. All you have to do to make a Service Component out of your java class is to extend it from AbstractCamelRunner and add the following org.apache.felix.scr.annotations on class level:

+ +

Then implement getRouteBuilders() method which returns the Camel routes you want to run. And finally provide the default configuration with:

+ +

That's all. And if you used camel-archetype-scr to generate a project all this is already taken care of.

CamelContextId and active properties control the CamelContext's name (defaults to "camel-runner-default") and whether it will be started or not (defaults to "false"), respectively. In addition to these you can add and use as many properties as you like. Camel's PropertiesComponent handles recursive properties and prefixing with fallback without problem.

AbstractCamelRunner will make these properties available to your RouteBuilders through Camel's PropertiesComponent and it will also inject these values into your Service Component's and RouteBuilder's fields when their names match. The fields can be declared with any visibility level, and many types are supported (String, int, boolean, URL, ...).

AbstractCamelRunner's lifecycle i n SCR

  1. When component's configuration policy and mandatory references are satisfied SCR calls activate(). This creates and sets up a CamelContext through the following call chain: activate() → prepare() → createCamelContext() → setupPropertiesComponent() → configure() → setupCamelContext(). Finally, the context is scheduled to start after a delay defined in AbstractCamelRunner.START_DELAY with runWithDelay().
  2. When Camel components (ComponentResolver services, to be exact) are registered in OSGi, SCR calls gotCamelComponent() which reschedules/delays the CamelContext start further by the same AbstractCamelRunner.START_DELAY. This in effect makes CamelContext wait until all Camel components are loaded or there is a sufficient gap between them. The same logic will tell a failed-to-start CamelContext to try again whenever we add more Camel components.
  3. When Camel components are unregistered SCR calls lostCamelComponent(). This call does nothing.
  4. When one of the requirements that caused the call to activate() is lost SCR will call deactivate(). This will shutdown the CamelContext.

In (non-OSGi) unit tests you should use prepare() → run() → stop() instead of activate() → ; deactivate() for more fine-grained control. Also, this allows us to avoid possible SCR specific operations in tests.

Example Camel SCR bundle

The easiest way to create an Camel SCR bundle is to use camel-archetype-scr Maven archetype.

mvn archetype:generate -Dfilter=org.apache.camel.archetypes:camel-archetype-scr

Choose archetype:
1: local -> org.apache.camel.archetypes:camel-archetype-scr (Creates a new Camel SCR bundle project for Karaf)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Define value for property 'groupId': : my.example
[INFO] Using property: groupId = my.example
Define value for property 'artifactId': : my-test
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': my.example: :
[INFO] Using property: archetypeArtifactId = camel-archetype-scr
[INFO] Using property: archetypeGroupId = org.apache.camel.archetypes
[INFO] Using property: archetypeVersion = 2.15-SNAPSHOT
Define value for property 'className': : MyTest
Confirm properties configuration:
groupId: my.example
artifactId: my-test
version: 1.0-SNAPSHOT
package: my.example
archetypeArtifactId: camel-archetype-scr
archetypeGroupId: org.apache.camel.archetypes
archetypeVersion: 2.15-SNAPSHOT
className: MyTest
Y: :

All done! Check ReadMe.txt in the generated project folder for the next steps.

To deploy a Camel SCR bundle project in Apache Karaf:

On Karaf command line:

# Add Camel feature repository
features:chooseurl camel 2.15-SNAPSHOT

# Install camel-scr feature
features:install camel-scr

# Install commons-lang, used in the example route to validate parameters
osgi:install mvn:commons-lang/commons-lang/2.6

# Install and start your bundle
osgi:install -s mvn:your.company/your-bundle/version

# See how it's running
log:tail -n 10

Press ctrl-c to stop watching the log.

+
+ +
+ + +
+
+
+
+
+
+ +
+
+
+© 2004-2014 The Apache Software Foundation. +
+Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners. +
+Graphic Design By Hiram +
+ + + + + + + +