Category: ‘Apache Karaf’

Use Camel, CXF and Karaf to implement batches

August 23, 2011 Posted by jbonofre

Introduction

Apache Camel has not be designed to be used for implementing batch tasks.

For instance, if your Camel route has a consumer endpoint polling files in a directory, Camel will periodically and indefinitely monitor the folder and poll any new incoming files.
It’s not a batch behavior: in batch mode, we want to run the file polling on demand, at a certain time, launched by a batch scheduler like ControlM, $Universe or Tivoli Worksheet Scheduler.

However, there are several interesting points to use Camel for batch implementation. First, Camel provides a large set of components. A lot of batches read/write files, read from a JMS queues, write into JMS queues, etc. Usage of Camel components in a batch way is really valuable.
Second, Camel uses a DSL to describe the process executed by the routes. Especially, it supports “human readable” DSL like Spring XML or Blueprint XML. It means that it’s easy to review what the batch is doing, eventually change an endpoint definition, etc. Most of the time, batches are “black box”: you run it, and you only get a status code to know if it’s OK or not. With Camel, you have a look on the batch process.
Third, Camel is a highly plug and play framework. It means that it’s easy to replace an endpoint by another one. For instance, if your batch polls files in a folder currently, it’s very easy to change this to poll messages from a JMS queue. You don’t really have to re-implement the whole batch.

More over, tools like Talend ESB Studio provide an IDE to create and design your Camel routes.

In this article, we are going to see how to use Camel in a “batch way”.

Design

In fact, we are going to have two Camel routes:
- the first one is called “control”. This route will “expose” a REST service to start the batch. A bean in this route will be responsible to start the “batch” route.
- the second one is called “batch”. It’s the core implementation of our batch. It’s a “standard” route, but at the end, we have a processor that “stop” the route (to avoid to have the route up indefinitely). This route is not auto started as it will be controller by the first one.

It means that a simple HTTP client (like a browser or REST client) will start the batch, on-demand. Most of enterprise batch schedulers ship a component to make HTTP requests.

POM

Our batch will be packaged as an OSGi bundle. It will allow us to deploy the batch in an Apache Karaf OSGi container:


<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>net.nanthrax.examples</groupId>
  <artifactId>camel-batch</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>bundle</packaging>

  <properties>
    <camel.version>2.8.0</camel.version>
    <cxf.version>2.4.1</cxf.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
      <version>${camel.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-spring</artifactId>
      <version>${camel.version}</version>
  &nbsp </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-cxf</artifactId>
      <version>${camel.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxrs</artifactId>
      <version>${cxf.version}</version>
    </dependency>
    &lt,dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>${cxf.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http-jetty</artifactId>
      <version>${cxf.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>2.3.4</version>
        <extensions>true</extensions>
        <configuration>
          <instructions>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
            <Require-Bundle>org.apache.cxf.bundle,org.apache.camel.camel-cxf,org.springframework.beans</Require-Bundle>
          </instructions>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

In this POM, we can see:
- the packaging is an OSGi bundle. That’s why we use the Apache Felix maven-bundle-plugin. We name the bundle with the project artifactId, and we define Camel and CXF bundles as dependencies (Require-Bundle).
- in the dependency sets, we define the Camel components that we use (camel-core, camel-spring to use the Camel Spring XML DSL, and camel-cxf to use the CXF JAX-RS implementation) and the CXF dependency to be able to create a JAX-RS server.

Control route

The first Camel route is the “control” one. This route will bind a JAX-RS server, listening HTTP requests (consumer) and will start the “batch” route on-demand.

The route definition will be located in the META-INF/spring/routes.xml folder of our bundle. We use the Camel Spring XML DSL in this file:


<?xml version="1.0" encoding="UTF-8"?>
<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"
  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
    http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
  ">

  <cxf:rsServer id="rsServer" address="http://localhost:9090/batch"
serviceClass="net.nanthrax.examples.camel.batch.impl.ControllerService"/&t;

  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <route id="control">
      <from uri="cxfrs:bean:rsServer"/>
      <to uri="log:net.nanthrax.examples.camel.batch"/>
      <to uri="controllerBean"/>
    </route>
  </camelContext>

  <bean id="controllerBean" class="net.nanthrax.examples.camel.batch.impl.ControllerBean">
    <property name="routeId" value="batch"/>
  </bean>

</beans>

We use the Camel CXF to create the JAX-RS server (using <cxf:rsServer/gt;). This JAX-RS server will listen on the local machine on the 9090 port, and the context path is /batch.
To “describe” the REST service behavior, we define the ControllerService class in the serviceClass attribute.
The ControllerService class is just an “empty container”. The purpose is just to “describe” the REST service, not to process it:


package net.nanthrax.examples.camel.batch.impl;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

/**
* REST service implementation of the Camel batch service.
*/
@Path("/")
public class ControllerService {

  @GET
  @Path("/start")
  @Produces("text/plain")
  public String startRoute() throws Exception {
    // nothing to do, it's just a wrapper
    return null;
  }

}

We can see the JAX-RS annotations:
- the ControllerService REST Path is /, it means directly bound to the JAX-RS server context path.
- the startRoute() method will accept GET HTTP method, on the context path /start and it will produce pure text (text/plain).

The process itself will be performed in the controllerBean:


package net.nanthrax.examples.camel.batch.impl;

import org.apache.camel.CamelContext;
import org.apache.camel.Handler;

/**
* Camel controller bean involved in the starting routed
*/
public class ControllerBean {

  private String routeId;

  public String getRouteId() {
    return this.routeId;
  }

  public void setRouteId(String routeId) {
    this.routeId = routeId;
  }

  @Handler
  public String startRoute(CamelContext camelContext) throws Exception {
    camelContext.startRoute(routeId);
    return "Batch " + routeId + " started.";
  }

}

We inject the Camel route ID of the batch route: “batch”. The CamelContext is automatically injected by Camel. This bean is quite simple, as it only starts the “batch” route.

Batch route

This route contains the “batch logic”. You can use any kind of routes, components, Enterprise Integration Patterns, etc provided by Camel. The only specific parts are:
- the autoStartup attribute set to false, to avoid to start the route automatically at context bootstrap
- the final processor which stop the route after processing.

We gather the two routes in the same META-INF/spring/routes.xml file:


<?xml version="1.0" encoding="UTF-8"?>
<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"
  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
    http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
  ">

  <cxf:rsServer id="rsServer" address="http://localhost:9090/batch"
  serviceClass="net.nanthrax.examples.camel.batch.impl.ControllerService"/>

  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <route id="control">
      <from uri="cxfrs:bean:rsServer"/>
      <to uri="log:net.nanthrax.examples.camel.batch"/>
      <to uri="controllerBean"/>
    </route>
    <route id="batch" autoStartup="false">
      <from uri="file:/tmp"/>
      <to uri="file:output"/>
      <process ref="stopProcessor"/>
    </route>
  </camelContext>

  <bean id="controllerBean" class="net.nanthrax.examples.camel.batch.impl.ControllerBean">
    <property name="routeId" value="batch"/>
  </bean>

  <bean id="stopProcessor" class="net.nanthrax.examples.camel.batch.impl.StopProcessor">
    <property name="routeId" value="batch"/>
  </bean>

</beans>

In this example, the batch polls files in the /tmp folder, and copies into the output folder.

The StopProcessor is Camel processor (aka, it implements the Camel Processor interface). It stops the route after processing the incoming message (we inject the “batch” route ID using Spring):


package net.nanthrax.examples.camel.batch.impl;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;

/**
* A Camel processor which stop routes.
*/
public class StopProcessor implements Processor {

  private String routeId;

  public String getRouteId() {
    return this.routeId;
  }

  public void setRouteId(String routeId) {
    this.routeId = routeId;
  }

  public void process(Exchange exchange) throws Exception {
    CamelContext camelContext = exchange.getContext();
    // remove myself from the in flight registry so we can stop this route without trouble
    camelContext.getInflightRepository().remove(exchange);
    // stop the route
    camelContext.stopRoute(routeId);
  }

}

Deployment and execution

Now, we can build our OSGi bundle, simply using:


mvn clean install

In a fresh Apache Karaf instance, we have first to install the CXF and Camel features:


karaf@root> features:addurl mvn:org.apache.cxf.karaf/apache-cxf/2.4.1/xml/features
karaf@root> features:install cxf
karaf@root> features:addurl mvn:org.apache.camel.karaf/apache-camel/2.8.0/xml/features
karaf@root> features:install camel-spring
karaf@root> features:install camel-cxf

Now, we can install our bundle:


karaf@root> osgi:install -s mvn:net.nanthrax.examples/camel-batch/1.0-SNAPSHOT

Our bundle appears as “created”:


karaf@root> la|grep -i batch
[ 134] [Active ] [ ] [Started] [ 60] camel-batch (1.0.0.SNAPSHOT)

Using a simple browser, we can access to http://localhost:9090/batch/start. The route is started (as a batch) and we can see in the browser:


Batch batch started.

Conclusion

Even if the first Camel route purpose is to be up and running all the time, we can use it in a more “batch” way. It allows developers to use the large set of Camel components, and be able to use all Enterprise Integration Patterns. For instance, the batch needs to copy a file, and after send an e-mail and a message into a JMS queue, it’s very easy using a recipient list. You have to send to a target endpoint depending of the content of the message, no problem using a Content Based Router.

You can run such kind of batches in Talend ESB. It’s an interesting addition to the Talend Data Integration products (ETL jobs, MDM, DQ, etc).

JAX-RS services using CXF and Karaf

August 19, 2011 Posted by jbonofre

Apache CXF provides a really great layer to implement JAX-RS services. Especially, it fully supports OSGi, including Blueprint. It means that you can very easily create and deploy your REST services in a Apache Karaf container.

In this example, we will see how to list all Karaf features via a REST service.

This exemple is composed by three modules:
- common is an OSGi bundle containing resources shared between the JAX-RS server and the clients. Basically, it contains the service interface and the objects used in the service.
- service is an OSGi bundle providing the implementation of the service interface
- client is a simple Main class that use CXF JAX-RS client

Common bundle

This bundle contains the interface describing the behavior of the REST service. We define it in the FeaturesRestService:


package net.nanthrax.examples.jaxrs.common;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.util.Collection;

/**
* REST service to manipulate Karaf features
*/
@Path("/")
public interface FeaturesRestService {

  /**
  * Returns an explicit collection of all features in XML format in response to HTTP GET requests.
  * @return the collection of features
  */
  @GET
  @Path("/features")
  @Produces("application/xml")
  public Collection getFeatures() throws Exception;

}

In this interface, the getFeatures() method returns a collection of FeatureWrapper. This object is an object that will be sent to the client. It contains JAXB and JAX-RS annotations:


package net.nanthrax.examples.jaxrs.common;

import javax.ws.rs.Path;
import javax.xml.bind.annotation.XmlRootElement;

/**
* Wrapper to a Karaf feature including JAXB nad JAX-RS annotations.
*/
@XmlRootElement(name = "Feature")
public class FeatureWrapper {

  private String name;
  private String version;

  public FeatureWrapper() { }

  public FeatureWrapper(String name, String version) {
    this.name = name;
    this.version = version;
  }

  @Path("name")
  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Path("version")
  public String getVersion() {
    return this.version;
  }

  public void setVersion(String version) {
    this.version = version;
  }

}

Now, we just need to define the Maven POM to build an OSGi bundle:


<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>net.nanthrax.examples</groupId>
    <artifactId>jaxrs-blueprint</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <groupId>net.nanthrax.examples.jaxrs-blueprint</groupId>
  <artifactId>net.nanthrax.examples.jaxrs-blueprint.common</artifactId>
  <packaging>bundle</packaging>

  <dependencies>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxrs</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <configuration>
          <instructions>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
            <Export-Package>
              net.nanthrax.examples.jaxrs.common*;version=${project.version}
            </Export-Package>
          </instructions>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

We can note that we have a dependencies to cxf-rt-frontend-jaxrs. This artifact provides us the JAX-RS annotations.
We don’t need additional dependencies for JAXB as it’s included in the JDK.

Service bundle

This bundle contains the implementation of the REST service.

We find two things in this bundle:
- the FeaturesRestServiceImpl class implementing the FeaturesRestService interface.
- the Blueprint descriptor in OSGI-INF/blueprint containing the bean definition of the FeaturesRestServiceImpl and the configuration of the JAX-RS server

The FeaturesRestServiceImpl doesn’t contain any JAX-RS annotations. It’s a pure implementation which use the Karaf FeaturesService OSGi service to get the list of Karaf features. It populates a collection of FeatureWrapper:


package net.nanthrax.examples.jaxrs.service;

import net.nanthrax.examples.jaxrs.common.FeatureWrapper;
import net.nanthrax.examples.jaxrs.common.FeaturesRestService;
import org.apache.karaf.features.Feature;
import org.apache.karaf.features.FeaturesService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* Implementation of the Features REST service.
*/
public class FeaturesRestServiceImpl implements FeaturesRestService {

  private FeaturesService featuresService;

  public FeaturesService getFeaturesService() {
    return this.featuresService;
  }

  public void setFeaturesService(FeaturesService featuresService) {
    this.featuresService = featuresService;
  }

  @Override
  public Collection getFeatures() throws Exception {
    List featuresWrapper = new ArrayList();
    Feature[] features = featuresService.listFeatures();
    for (int i = 0; i < features.length; i++) {
      FeatureWrapper wrapper = new FeatureWrapper(features[i].getName(), features[i].getVersion());
      featuresWrapper.add(wrapper);
    }
    return featuresWrapper;
  }

}

The Blueprint descriptor (in OSGI-INF/blueprint/rest.xml) is responsible:
- to get the reference to the Karaf FeaturesService OSGi service and inject it in the FeaturesRestServiceImpl bean
- to configure the JAX-RS server and define the FeaturesRestServiceImpl as a service bean
- optionally, we enable debug on the CXF internal bus


<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:jaxrs="http://cxf.apache.org/blueprint/jaxrs"
  xmlns:cxf="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
  http://cxf.apache.org/blueprint/jaxws http://cxf.apache.org/schemas/blueprint/jaxws.xsd
  http://cxf.apache.org/blueprint/jaxrs http://cxf.apache.org/schemas/blueprint/jaxrs.xsd
  http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
  ">

  <cxf:bus>
    <cxf:features>
      <cxf:logging/>
    </cxf:features>
  </cxf:bus>

  <jaxrs:server id="karafFeaturesService" address="/karaf">
    <jaxrs:serviceBeans>
      <ref component-id="karafFeaturesServiceBean"/>
    </jaxrs:serviceBeans>
  </jaxrs:server>

  <bean id="karafFeaturesServiceBean" class="net.nanthrax.examples.jaxrs.service.FeaturesRestServiceImpl">
    <property name="featuresService" ref="featuresService"/>
  </bean>

  <reference id="featuresService" interface="org.apache.karaf.features.FeaturesService"/>

&tt;/blueprint>

Finally, the service bundle POM creates an OSGi bundle importing package from Karaf (for the features service) and the common (for the interface and the FeatureWrapper):


<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>net.nanthrax.examples</groupId>
    <artifactId>jaxrs-blueprint</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <groupId>net.nanthrax.examples.jaxrs-blueprint</groupId>
  <artifactId>net.nanthrax.examples.jaxrs-blueprint.service</artifactId>
  <packaging>bundle</packaging>

  <dependencies>
    <dependency>
      <groupId>net.nanthrax.examples.jaxrs-blueprint</groupId>
      <artifactId>net.nanthrax.examples.jaxrs-blueprint.common</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.karaf.features</groupId>
      <artifactId>org.apache.karaf.features.core</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <configuration>
          <instructions>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
            <Export-Package>
              net.nanthrax.examples.jaxrs.service*;version=${project.version}
            </Export-Package>
            <Import-Package>
              net.nanthrax.examples.jaxrs.common*;version=${project.version},
              org.apache.karaf.features*;version="[2,4)",
              *
            </Import-Package>
          </instructions>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

Deployment of the REST service in Karaf

Now, we are ready to deploy our REST service into Karaf.

The first step to perform is the installation of the CXF feature:


karaf@root> features:addurl mvn:org.apache.cxf.karaf/apache-cxf/2.4.2/xml/features
karaf@root> features:install cxf

The CXF feature provides the CXF bundle (with the core engine) and the JAX-RS frontend.

Now, we can install the common bundle and the service bundle:


karaf@root> osgi:install -s mvn:net.nanthrax.examples.jaxrs-blueprint/net.nanthrax.examples.jaxrs-blueprint.common/1.0-SNAPSHOT
karaf@root> osgi:install -s mvn:net.nanthrax.examples.jaxrs-blueprint/net.nanthrax.examples.jaxrs-blueprint.service/1.0-SNAPSHOT

Our REST service is now available. The JAX-RS server uses the OSGi HTTP service of Karaf (the Karaf http feature is automatically installed by CXF). The default port number of the HTTP service (which use Jetty as web container) is 8181.

By default, CXF frontends/servlets use the cxf context root.

So it means that, if you point your browser on http://localhost:8181/cxf/karaf/features, we will see the XML formatted list of all Karaf features:


<Features>
  <Feature>
    <name>saaj-impl</name>
    <version>1.3.2</version>
   /Feature<
  <Feature>
    <name>abdera</name>
    <version>1.1.2</version>
  </Feature>
...

In detail, the URL http://localhost:8181/cxf/karaf/features comes from:
- the port (8181) is the default one of the Karaf HTTP service
- the context root (cxf) is the default one used by CXF
- the "karaf" context is defined in the JAX-RS server (in the Blueprint descriptor, by the address attribute)
- the "features" is defined in the FeaturesRestService interface (by the @Path("/features") annotation on the getFeatures() method)

REST client

CXF also provide a REST client, very easy to use:


package net.nanthrax.examples.jaxrs.client;

import net.nanthrax.examples.jaxrs.common.FeatureWrapper;
import org.apache.cxf.jaxrs.client.WebClient;

import java.util.ArrayList;
import java.util.List;

/**
* Simple JAX-RS client.
*/
public final class Main {

  public static void main(String[] args) throws Exception {
    WebClient webClient = WebClient.create("http://localhost:8181/cxf/karaf/features/");
    List features = new ArrayList(webClient.getCollection(FeatureWrapper.class));
    for (FeatureWrapper feature : features) {
      System.out.println("Feature " + feature.getName() + "/" + feature.getVersion());
    }
  }

}

Conclusion

REST service is really easy to do and Apache CXF fully supports the OSGi environment. We can use Blueprint (for OSGi services lookup) to describe and start the JAX-RS server. That's why Karaf is a great container for this kind of services (and a lot of others ;) ).

You can find a bunch of others REST services examples in Talend Service Factory (TSF):

- the Talend SF runtime: http://www.talend.com/download.php?src=HomePage#AI
- the Talend SF examples: http://www.talend.com/resources/documentation.php#SF

Apache Karaf Cellar 2.2.2 release

August 8, 2011 Posted by jbonofre

What’s new

Quite one month ago, we released Karaf Cellar 2.2.1, the first “official” release of the Karaf clustering sub-project.

This new Karaf Cellar 2.2.2 release includes bug fixes, especially one bug was a blocker as it was not possible to install Cellar on a Karaf instance running on Equinox OSGi framework.

But, it’s not just a bug fix release, we merge two features from the Cellar trunk.

Bundle synchronization

In Karaf Cellar 2.2.1, we were able to synchronize features (including features repositories) and configuration between Karaf Cellar instances. It means that you can install a feature on one node (cluster:features-install group feature), the feature will be install on each Karaf note.

Karaf Cellar 2.2.2 includes the same behavior for pure OSGi bundle. You can install a bundle on one node, the bundle will be installed on each other nodes on the same cluster group.


karaf@root> osgi:install mybundle

mybundle will be installed on all nodes in the same cluster group.

It’s a first step, as we have for features and config, we will add specific command to manipulate bundle, something like:


karaf@root> cluster:install-bundle group mybundle

Cloud support

Cellar relies on Hazelcast in order to discover cluster nodes. This can happen either by using multicast or by unicast (specifying the ip address of each node).
Unfortunately multicast is not allowed in most IaaS providers and specifying the all the ip addresses is not very flexible, since in most cases they are not known in advance.

Cellar solves this problem using a cloud discovery service powered by jclouds.

Cloud discovery service

Most cloud providers among other provide cloud storage. Cellar uses the cloud storage via jclouds, in order to put there the ip addresses of each node so that Hazelcast can found them.
This approach is also called blackboard and in other words is the process where each nodes registers itself in a common storage, so that other nodes know its existence.

Installing Cellar cloud discovery service

To install the cloud discovery service simply the appropriate jclouds provider and then install cellar-cloud feature. For the rest of this manual I will use amazon s3 as an example, but it applies to any provider supported by jclouds.


karaf@root> features:install jclouds-aws-s3
karaf@root> features:install cellar-cloud

Once the feature is installed, it requires you to create a configuration that contains credentials and type of the cloud storage (aka blobstore).
To do that add a configuration file under etc with the name org.apache.karaf.cellar.cloud- .cfg and put there the following information:

provider=aws-s3 (this varries according to the blobstore provider)
identity=”the identity of the blobstore account”
credential=”the credential/password of the blobstore account)”
container=”the name of the bucket”
validity=”the amount of time an entry is considered valid, after that time the entry is removed”

After creating the file the service will check for new nodes. If new nodes are found the Hazelcast instance configuration is updated and the instance is restarted.

Apache Karaf 2.1.x branch is in end of life, 2.1.6 available

July 17, 2011 Posted by jbonofre

We (The Apache Karaf team) have decided to switch Karaf 2.1.x branch into End Of Life mode.

We released Karaf 2.1.6 which is the last version on this branch:

http://karaf.apache.org/index/community/download/karaf-2.1.6-release.html

We encourage all users to upgrade to Karaf 2.2.2, which is the latest Karaf stable release.

This announce is in preparation of the next Karaf 3.0.0 on which we are hardly working currently.

Apache Karaf Cellar 2.2.1 Released

July 11, 2011 Posted by jbonofre

Apache Karaf Cellar 2.2.1 has been released today.

Cellar is a Karaf sub-project which aim to provide a clustering solution for Karaf.

Quick start

To enable Cellar into a Karaf, you just have to install the Cellar feature.

First, register the Cellar features descriptor in your running Karaf instance:

karaf@root> features:addurl mvn:org.apache.karaf.cellar/apache-karaf-cellar/2.2.1/xml/features

Now, you can see the Cellar features available:

karaf@root> features:list|grep -i cellar
[uninstalled] [2.2.1 ] cellar Karaf clustering
[uninstalled] [2.2.1 ] cellar-webconsole Karaf Cellar Webconsole Plugin

To start Cellar, install the cellar feature:

karaf@root> features:install cellar

It’s done: your Karaf instance is Cellar cluster ready.

You can see your cluster node ID and, eventually, others cluster nodes:

karaf@root> cluster:list-nodes
No. Host Name Port ID
* 1 node1.local 5701 node1.local:5701
2 node2.local 5702 node2.local:5702

The * indicates your local node (on which you are connected).

You can ping a given node to see how the network behaves:

karaf@root> cluster:ping node2.local:5702
Pinging node :node2.local:5702
PING 1 node2.local:5702 82ms
PING 2 node2.local:5702 11ms
PING 3 node2.local:5702 14ms

Now, if you install a feature, it will be installed on all nodes. For instance, if you install eventadmin feature on node1:

karaf@node1> features:install eventadmin
karaf@node1> features:list|grep -i eventadmin
[installed ] [2.2.1 ] eventadmin karaf-2.2.1

you can see it installed on node2:

karaf@node2> features:list|grep -i eventadmin
[installed ] [2.2.1 ] eventadmin karaf-2.2.1

Cellar groups

In Karaf Cellar, you can define cluster groups. It allows you to select nodes and resources involved in one group.

By default, you have the default group:

karaf@root> cluster:group-list
Node Group
* node1.local:5701 default
node2.local:5702 default

You can create a route, for instance test:

karaf@root> cluster:group-create test
Name test
Members []

 

For now, the group doesn’t have any member:

kaaf@root> cluster:group-list
Node Group
node1.local:5701 default
* node2.local:5702 default
test

We can add a node as a member of a cluster group:

karaf@root> cluster:group-join test node1.local:5701
Node Group
node1:5701 default
* node2:5702 default
node1:5701 test

The node could be local or remote.

Cluster units

Currently, Apache Karaf Cellar is able to manage features and configuration. It’s based on an event-driven model.

It means that Cellar is listening for some events (such as install/start/remove features, or set configuration) and fire the actions on all nodes.

Features

You can list the features of a given group:

karaf@root> cluster:features-list test

The feature events (install, uninstall) are sync between all members of the same cluster group.

Configuration

Cellar is able to manage configuration event, as it does for features.

You can see the configuration PID associated to a given cluster group:

karaf@root> cluster:config-list test
No PIDs found for group:test

You can create a configuration (with a PID) directly into a given group:

karaf@root> cluster:config-propset test my mykey myvalue

Now, we can see this configuration in the cluster memory:

karaf@root> cluster:config-list test
PIDs for group:test
PID
my

and the value in this configuration PID:

karaf@root> cluster:config-proplist test my
Property list for PID:my for group:test
Key Value
mykey myvalue

Now, you can see that the cluster group test doesn’t have any member for now:

karaf@root> cluster:group-list
Node Group
* node1.local:5701 default
test

If I list the configuration on my node1 local instance, my is not known:

karaf@root> config:list "(pid=my)"

Now, node1 join the test cluster group:

karaf@root> config:group-join test node1.local:5701

And, now, the node1.local inherits of the configuration defined in the cluster:

karaf@root> config:edit my
karaf@root> proplist
service.pid = my
mykey = myvalue

Now, time to work :)

OK, we have a first release of Karaf Cellar, allowing mainly discovery, group and state replication between Karaf instances. The bundle events support is also quite implemented (it just needs to be finalized).

We are going to add JMX Cellar MBeans and logging message with Mike Van. As Cellar is fully based on Blueprint, adding MBeans should be very easy and quick :)

But it’s not enough :)

Now, I’m sure you have a question: is it possible to have a kind of shared OSGi services registry, with proxy, and be able (transparently or not) to use a remote service ? Do we have DOSGi support ?

The short answer is: not in Cellar 2.2.1.

But, we are working on these two topics (I name it “distributed service registry” and “transport”) and it should be included the next Cellar releases.

I hope you enjoy this new Karaf sub-project.

Apache Karaf 2.2.2 available

July 4, 2011 Posted by jbonofre

A new Apache Karaf version is available. If it’s mainly a bug fixes release, it includes some small enhancements.

Especially, we added a completer for shell aliases. It means that you add a new command alias in etc/shell.init.script, the TAB key will look for aliases in addition of pure commands.

Another interesting enhancement used in Apache CXF and Apache Camel is that the feature Maven plugin now “embeds” the Karaf core features. It means that it’s no more required to define <repository>mvn:org.apache.karaf.assemblies.features/standard/2.2.2/xml/features</repository> in your features descriptors: it’s the default.

You can take a look on the complete release note.

I’m going to update Karaf related projects (CXF, Camel, ServiceMix, TIF, TSF) and focus on Apache Karaf Cellar 2.2.1 version ;)

Camel 2.8.0 new features for Karaf/ServiceMix

June 29, 2011 Posted by jbonofre

Camel provides Karaf features descriptor since quite a long time now. But Camel 2.8.0 will include new Karaf features very useful and turning Karaf and ServiceMix as the main container to run Camel.

Install Camel in Karaf

Installing Camel in Karaf is very simple as a features descriptor is provided. It means that you can register the Camel features descriptor in your running Karaf instance:

karaf@root> features:addurl mvn:org.apache.camel.karaf/apache-camel/2.8.0/xml/features

Now, you have the Camel features available:

karaf@root> features:list|grep -i camel

[uninstalled] [2.8.0 ] camel repo-0

[uninstalled] [2.8.0 ] camel-core repo-0

[uninstalled] [2.8.0 ] camel-spring repo-0

[uninstalled] [2.8.0 ] camel-blueprint repo-0

Deploy Camel features

To start using Camel in Karaf, you have to install at least the camel feature:

root@karaf> features:install camel

Depending of your requirements, you will certainly install others Camel features.

For instance, if you use Blueprint Camel DSL, you have to install the camel-blueprint feature:

root@karaf> features:install camel-blueprint

or, if you use stream component in an endpoint (for instance “stream:out”), you will install the camel-stream feature:

root@karaf> features:install camel-stream

Camel and OSGi

When Camel is used in an OSGi environment, it automatically  exposes CamelContexts as OSGi services. It means that, when you deploy a route, the associated CamelContext is available for OSGi bundles.

You can look up for CamelContext OSGi services with this small code snippet:


ServiceReference[] references = bundleContext.getServiceReferences(CamelContext.class.getName(), null);
if (references != null) {
  for (ServiceReference reference : references) {
    if (reference != null) {
      CamelContext camelContext = (CamelContext) bundleContext.getService(reference);
      if (camelContext != null) {
        // do what you want on the CamelContext
      }
    }
  }
}

Camel Karaf commands

Camel 2.8.0 provides a set of Karaf commands which allow you to get information, start, stop about Camel contexts and routes.

  • camel:list-contexts displays the list of Camel context currently available in the running Karaf/ServiceMix instance
  • camel:list-routes displays the list of Camel routes currently available in the running Karaf/ServiceMix instance
  • camel:start-context starts a Camel context
  • camel:stop-context stops a Camel context
  • camel:info-context display detailed information about a Camel context
  • camel:start-route starts a Camel route
  • camel:stop-route stops a Camel route
  • camel:show-route displays the XML rendering of a Camel route (whatever your route DSL is)
  • camel:info-route displays detailed information about a Camel route including statistics

Happy birthday Apache Karaf

June 17, 2011 Posted by jbonofre

Yesterday was the first birthday of Apache Karaf.

We made a conference call between all Karaf developer. It was funny to put a voice on a name :)

It was very interesting even if unfortunately some key players were not available such as Guillaume or Achim.

Past Year Review

First, we made a review about this past year. Jamie had prepared a very complete review:

http://icodebythesea.blogspot.com/2011/06/apache-karaf-year-one-in-review.html

We can note that the Karaf downloads increased a lot with the 2.2.0 release. It was a really expected release and it has been included in a lot of projects both open-source (ServiceMix, CXF, Camel, Geronimo, …) and enterprise (Talend, Fuse, …).

We can conclude that the Karaf visibility and adoption have quickly increase and Karaf is now a important container in the OSGi eco-system.

Karaf Future

We also share our vision about the Karaf future.

We identified the following topics:

  1. Our first focus will be the Karaf 3.0.0 release. It means that Karaf 2.1.x branch will turn in EOL (End Of Life), the Karaf 2.2.x branch will be in maintenance mode. This release will be a major release as it includes a lot of new features and enchancements.
  2. Create custom Karaf distribution is not really easy right now and some projects have to change low level configuration files. We are going to work on Karaf dsitributions and profiles, by providing tooling (such as maven plugin).
  3. Add a premium OSGi container, Karaf should provide a complete and “sexy” web console. The current web console (powered by Felix console) will be maintained and suported, but we are going to work on a new one, highly extendible (by a plugin mechanism) and supporting branding. We are going to use a modern component-oriented framework such as Wicket.
  4. We moved forward in the clustering area with Karaf Cellar sub-project. It’s clear that clustering, clouding, and farming are features expected by the enterprises. We are going to work on this topic by extending Cellar.
  5. Related to the previous topic, provisioning is a must have. Apache ACE and Apache Karaf will work together to provide an enterprise provisioning solution.
  6. Cellar was the first Karaf sub-project. Related to provisioning, Karaf Cave OBR could be a new Karaf sub-project. The purpose is to provide an OBR server implementations including advanced features (P2P, REST interface, multi-repositories, etc).

I wrote down the concall minute notes:

https://cwiki.apache.org/confluence/display/KARAF/Apache+Karaf+First+Birthday+Meeting+%282011-06-16%29

It’s clear that we should schedule this kind of concall every two or three months. It allows us to make a kind of checkpoint and drive the Karaf roadmap.