JClouds

Since Camel 2.9

Both producer and consumer are supported

This component allows interaction with cloud provider key-value engines (blobstores) and compute services. The component uses jclouds which is
a library that provides abstractions for blobstores and compute services.

ComputeService simplifies the task of managing machines in the cloud. For example, you can use ComputeService to start 5 machines and install your software on them.
BlobStore simplifies dealing with key-value providers such as Amazon S3. For example, BlobStore can give you a simple Map view of a container.

The camel jclouds component allows you to use both abstractions, as it specifies two types of endpoint the JcloudsBlobStoreEndpoint and the JcloudsComputeEndpoint. You can have both producers and consumers on a blobstore endpoint but you can only have producers on compute endpoints.

Maven users will need to add the following dependency to their pom.xml for this component:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jclouds</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

Configuring the component

The camel jclouds component will make use of multiple jclouds blobstores and compute services as long as they are passed to the component during initialization. The component accepts a list blobstores and compute services. Here is how it can be configured.

    <bean id="jclouds" class="org.apache.camel.component.jclouds.JcloudsComponent">
        <property name="computeServices">
            <list>
                <ref bean="computeService"/>
            </list>
        </property>
        <property name="blobStores">
            <list>
                <ref bean="blobStore"/>
            </list>
        </property>
    </bean>

    <!-- Creating a blobstore from spring / blueprint xml -->
    <bean id="blobStoreContextFactory" class="org.jclouds.blobstore.BlobStoreContextFactory"/>

    <bean id="blobStoreContext" factory-bean="blobStoreContextFactory" factory-method="createContext">
        <constructor-arg name="provider" value="PROVIDER_NAME"/>
        <constructor-arg name="identity"  value="IDENTITY"/>
        <constructor-arg name="credential" value="CREDENTIAL"/>
    </bean>

    <bean id="blobStore" factory-bean="blobStoreContext" factory-method="getBlobStore"/>

    <!-- Creating a compute service from spring / blueprint xml -->
    <bean id="computeServiceContextFactory" class="org.jclouds.compute.ComputeServiceContextFactory"/>

    <bean id="computeServiceContext" factory-bean="computeServiceContextFactory" factory-method="createContext">
        <constructor-arg name="provider" value="PROVIDER_NAME"/>
        <constructor-arg name="identity"  value="IDENTITY"/>
        <constructor-arg name="credential" value="CREDENTIAL"/>
    </bean>

    <bean id="computeService" factory-bean="computeServiceContext" factory-method="getComputeService"/>

As you can see the component is capable of handling multiple blobstores and compute services. The actual implementation that will be used by each endpoint is specified by passing the provider inside the URI.

Jclouds Options

jclouds:blobstore:[provider id][?options]
jclouds:compute:[provider id][?options]

The provider id is the name of the cloud provider that provides the target service (e.g. aws-s3 or aws_ec2).

You can append query options to the URI in the following format, ?option=value&option=value&…​

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

The component level is the highest level which holds general and common configurations that are inherited by the endpoints. For example a component may have security settings, credentials for authentication, urls for network connection and so forth.

Some components only have a few options, and others may have many. Because components typically have pre configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.

Configuring components can be done with the Component DSL, in a configuration file (application.properties|yaml), or directly with Java code.

Configuring Endpoint Options

Where you find yourself configuring the most is on endpoints, as endpoints often have many options, which allows you to configure what you need the endpoint to do. The options are also categorized into whether the endpoint is used as consumer (from) or as a producer (to), or used for both.

Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.

A good practice when configuring options is to use Property Placeholders, which allows to not hardcode urls, port numbers, sensitive information, and other settings. In other words placeholders allows to externalize the configuration from your code, and gives more flexibility and reuse.

The following two sections lists all the options, firstly for the component followed by the endpoint.

Component Options

The JClouds component supports 5 options, which are listed below.

Name Description Default Type

blobStores (common)

To use the given BlobStore which must be configured when using blobstore.

List

computeServices (common)

To use the given ComputeService which must be configured when use compute.

List

bridgeErrorHandler (consumer)

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

lazyStartProducer (producer)

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

autowiredEnabled (advanced)

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

boolean

Endpoint Options

The JClouds endpoint is configured using URI syntax:

jclouds:command:providerId

with the following path and query parameters:

Path Parameters (2 parameters)

Name Description Default Type

command (common)

Required What command to execute such as blobstore or compute.

Enum values:

  • blobstore

  • compute

JcloudsCommand

providerId (common)

Required The name of the cloud provider that provides the target service (e.g. aws-s3 or aws_ec2).

String

Query Parameters (15 parameters)

Name Description Default Type

bridgeErrorHandler (consumer (advanced))

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

boolean

exceptionHandler (consumer (advanced))

To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored.

ExceptionHandler

exchangePattern (consumer (advanced))

Sets the exchange pattern when the consumer creates an exchange.

Enum values:

  • InOnly

  • InOut

  • InOptionalOut

ExchangePattern

lazyStartProducer (producer (advanced))

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

boolean

blobName (blobstore)

The name of the blob.

String

container (blobstore)

The name of the blob container.

String

directory (blobstore)

An optional directory name to use.

String

group (compute)

The group that will be assigned to the newly created node. Values depend on the actual cloud provider.

String

hardwareId (compute)

The hardware that will be used for creating a node. Values depend on the actual cloud provider.

String

imageId (compute)

The imageId that will be used for creating a node. Values depend on the actual cloud provider.

String

locationId (compute)

The location that will be used for creating a node. Values depend on the actual cloud provider.

String

nodeId (compute)

The id of the node that will run the script or destroyed.

String

nodeState (compute)

To filter by node status to only select running nodes etc.

Enum values:

  • PENDING

  • TERMINATED

  • SUSPENDED

  • RUNNING

  • ERROR

  • UNRECOGNIZED

String

operation (compute)

Specifies the type of operation that will be performed to the blobstore.

String

user (compute)

The user on the target node that will run the script.

String

Usage

You can have as many of these options as you like.

jclouds:blobstore:aws-s3?operation=CamelJcloudsGet&container=mycontainer&blobName=someblob

For producer endpoint you can override all of the above URI options by passing the appropriate headers to the message.

Message Headers

The JClouds component supports 11 message header(s), which is/are listed below:

Name Description Default Type

CamelJcloudsBlobName (producer blobstore)

Constant: BLOB_NAME

The name of the blob.

String

CamelJcloudsContainerName (producer blobstore)

Constant: CONTAINER_NAME

The name of the blob container.

String

CamelJcloudsBlobNameList (producer blobstore)

Constant: BLOB_NAME_LIST

The blob name list.

List

CamelJcloudsNodeState (producer compute)

Constant: NODE_STATE

The node state.

Object

CamelJcloudsOperation (producer)

Constant: OPERATION

The operation to be performed on the blob. The valid options are: PUT GET.

String

CamelJcloudsImageId (producer compute)

Constant: IMAGE_ID

The imageId that will be used for creating a node. Values depend on the actual cloud provider.

String

CamelJcloudsLocationId (producer)

Constant: LOCATION_ID

The location that will be used for creating a node. Values depend on the actual cloud provider.

String

CamelJcloudsHardwareId (producer compute)

Constant: HARDWARE_ID

The hardware that will be used for creating a node. Values depend on the actual cloud provider.

String

CamelJcloudsGroup (producer compute)

Constant: GROUP

The group that will be assigned to the newly created node. Values depend on the actual cloud provider.

String

CamelJcloudsNodeId (producer compute)

Constant: NODE_ID

The id of the node that will run the script or destroyed.

String

CamelJcloudsUser (producer compute)

Constant: USER

The user on the target node that will run the script.

String

Blobstore Usage Samples

Example 1: Putting to the blob

This example will show you how you can store any message inside a blob using the jclouds component.

from("direct:start")
    .to("jclouds:blobstore:aws-s3" +
        "?operation=PUT" +
        "&container=mycontainer" +
        "&blobName=myblob");

In the above example you can override any of the URI parameters with headers on the message. Here is how the above example would look like using xml to define our route.

<route>
    <from uri="direct:start"/>
    <to uri="jclouds:blobstore:aws-s3?operation=PUT&container=mycontainer&blobName=myblob"/>
</route>

Example 2: Getting/Reading from a blob

This example will show you how you can read the content of a blob using the jclouds component.

from("direct:start")
    .to("jclouds:blobstore:aws-s3" +
        "?operation=GET" +
        "&container=mycontainer" +
        "&blobName=myblob");

In the above example you can override any of the URI parameters with headers on the message. Here is how the above example would look like using xml to define our route.

<route>
    <from uri="direct:start"/>
    <to uri="jclouds:blobstore:aws-s3?operation=PUT&container=mycontainer&blobName=myblob"/>
</route>

Example 3: Consuming a blob

This example will consume all blob that are under the specified container. The generated exchange will contain the payload of the blob as body.

    from("jclouds:blobstore:aws-s3" +
        "?container=mycontainer")
        .to("direct:next");

You can achieve the same goal by using xml, as you can see below.

<route>
    <from uri="jclouds:blobstore:aws-s3?operation=GET&container=mycontainer&blobName=myblob"/>
    <to uri="direct:next"/>
</route>
jclouds:compute:aws-ec2?operation=CamelJcloudsCreateNode&imageId=AMI_XXXXX&locationId=eu-west-1&group=mygroup

Compute Usage Samples

Below are some examples that demonstrate the use of jclouds compute producer in java dsl and spring/blueprint xml.

Example 1: Listing the available images.

    from("jclouds:compute:aws-ec2" +
        "&operation=CamelJCloudsListImages")
        .to("direct:next");

This will create a message that will contain the list of images inside its body. You can also do the same using xml.

<route>
    <from uri="jclouds:compute:aws-ec2?operation=CamelJCloudsListImages"/>
    <to uri="direct:next"/>
</route>

Example 2: Create a new node.

    from("direct:start").
    to("jclouds:compute:aws-ec2" +
        "?operation=CamelJcloudsCreateNode" +
        "&imageId=AMI_XXXXX" +
        "&locationId=XXXXX" +
        "&group=myGroup");

This will create a new node on the cloud provider. The out message in this case will be a set of metadata that contains information about the newly created node (e.g. the ip, hostname etc). Here is the same using spring xml.

<route>
    <from uri="direct:start"/>
    <to uri="jclouds:compute:aws-ec2?operation=CamelJcloudsCreateNode&imageId=AMI_XXXXX&locationId=XXXXX&group=myGroup"/>
</route>

Example 3: Run a shell script on running node.

    from("direct:start").
    to("jclouds:compute:aws-ec2" +
        "?operation=CamelJcloudsRunScript" +
        "?nodeId=10" +
        "&user=ubuntu");

The sample above will retrieve the body of the in message, which is expected to contain the shell script to be executed. Once the script is retrieved, it will be sent to the node for execution under the specified user (in order case ubuntu). The target node is specified using its nodeId. The nodeId can be retrieved either upon the creation of the node, it will be part of the resulting metadata or by a executing a LIST_NODES operation.

Note This will require that the compute service that will be passed to the component, to be initialized with the appropriate jclouds ssh capable module (e.g. jsch or sshj).

Here is the same using spring xml.

<route>
    <from uri="direct:start"/>
    <to uri="jclouds:compute:aws-ec2?operation=CamelJcloudsListNodes&?nodeId=10&user=ubuntu"/>
</route>

Spring Boot Auto-Configuration

When using jclouds with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:

<dependency>
  <groupId>org.apache.camel.springboot</groupId>
  <artifactId>camel-jclouds-starter</artifactId>
  <version>x.x.x</version>
  <!-- use the same version as your Camel core version -->
</dependency>

The component supports 6 options, which are listed below.

Name Description Default Type

camel.component.jclouds.autowired-enabled

Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc.

true

Boolean

camel.component.jclouds.blob-stores

To use the given BlobStore which must be configured when using blobstore.

List

camel.component.jclouds.bridge-error-handler

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored.

false

Boolean

camel.component.jclouds.compute-services

To use the given ComputeService which must be configured when use compute.

List

camel.component.jclouds.enabled

Whether to enable auto configuration of the jclouds component. This is enabled by default.

Boolean

camel.component.jclouds.lazy-start-producer

Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing.

false

Boolean