Dapr

Since Camel 4.12

Only producer is supported

The Dapr Component provides support for interacting with the Dapr Building Blocks.

URI format

dapr:operation[?options]

Where operation indicates the specific Dapr building block to interact with.

Configuring Options

Camel components are configured on two separate levels:

  • component level

  • endpoint level

Configuring Component Options

At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.

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.

You can configure components using:

  • the Component DSL.

  • in a configuration file (application.properties, *.yaml files, etc).

  • directly in the Java code.

Configuring Endpoint Options

You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or 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.

Property placeholders provide a few benefits:

  • They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.

  • They allow externalizing the configuration from the code.

  • They help the code to become more flexible and reusable.

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

Component Options

The Dapr component supports 13 options, which are listed below.

Name Description Default Type

concurrency (producer)

Concurrency mode to use with state operations.

Enum values:

  • FIRST_WRITE

  • LAST_WRITE

Concurrency

configuration (producer)

The component configurations.

DaprConfiguration

consistency (producer)

Consistency level to use with state operations.

Enum values:

  • EVENTUAL

  • STRONG

Consistency

eTag (producer)

The eTag for optimistic concurrency during state save or delete operations.

String

httpExtension (producer)

Autowired HTTP method to use when invoking the service. Accepts verbs like GET, POST, PUT, DELETE, etc. Creates a minimal HttpExtension with no headers or query params. Takes precedence over verb.

HttpExtension

key (producer)

The key used to identify the state object within the specified state store.

String

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

methodToInvoke (producer)

The name of the method or route to invoke on the target service.

String

serviceToInvoke (producer)

Target service to invoke. Can be a Dapr App ID, a named HTTPEndpoint, or a FQDN/public URL.

String

stateOperation (producer)

The state operation to perform on the state store. Required for DaprOperation.state operation.

Enum values:

  • save

  • saveBulk

  • get

  • getBulk

  • delete

  • executeTransaction

get

StateOperation

stateStore (producer)

The name of the Dapr state store to interact with, defined in statestore.yaml config.

String

verb (producer)

The HTTP verb to use for invoking the method.

POST

String

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 Dapr endpoint is configured using URI syntax:

dapr:operation

With the following path and query parameters:

Path Parameters (1 parameters)

Name Description Default Type

operation (producer)

Required The Dapr building block operation to perform with this component.

Enum values:

  • invokeService

  • state

DaprOperation

Query Parameters (11 parameters)

Name Description Default Type

concurrency (producer)

Concurrency mode to use with state operations.

Enum values:

  • FIRST_WRITE

  • LAST_WRITE

Concurrency

consistency (producer)

Consistency level to use with state operations.

Enum values:

  • EVENTUAL

  • STRONG

Consistency

eTag (producer)

The eTag for optimistic concurrency during state save or delete operations.

String

httpExtension (producer)

Autowired HTTP method to use when invoking the service. Accepts verbs like GET, POST, PUT, DELETE, etc. Creates a minimal HttpExtension with no headers or query params. Takes precedence over verb.

HttpExtension

key (producer)

The key used to identify the state object within the specified state store.

String

methodToInvoke (producer)

The name of the method or route to invoke on the target service.

String

serviceToInvoke (producer)

Target service to invoke. Can be a Dapr App ID, a named HTTPEndpoint, or a FQDN/public URL.

String

stateOperation (producer)

The state operation to perform on the state store. Required for DaprOperation.state operation.

Enum values:

  • save

  • saveBulk

  • get

  • getBulk

  • delete

  • executeTransaction

get

StateOperation

stateStore (producer)

The name of the Dapr state store to interact with, defined in statestore.yaml config.

String

verb (producer)

The HTTP verb to use for invoking the method.

POST

String

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

Message Headers

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

Name Description Default Type

CamelDaprServiceToInvoke (producer)

Constant: SERVICE_TO_INVOKE

Target service to invoke. Can be a Dapr App ID, a named HTTPEndpoint, or a FQDN/public URL.

String

CamelDaprMethodToInvoke (producer)

Constant: METHOD_TO_INVOKE

The name of the method or route to invoke on the target service.

String

CamelDaprVerb (producer)

Constant: VERB

The HTTP verb to use for service invocation.

String

CamelDaprQueryParameters (producer)

Constant: QUERY_PARAMETERS

The query parameters for HTTP requests.

Map

CamelDaprHttpHeaders (producer)

Constant: HTTP_HEADERS

The headers for HTTP requests.

Map

CamelDaprHttpExtension (producer)

Constant: HTTP_EXTENSION

The HttpExtension object for service invocation. Takes precedence over verb.

HttpExtension

CamelDaprStateOperation (producer)

Constant: STATE_OPERATION

The state operation to perform on the state store. Required for DaprOperation.state operation.

get

StateOperation

CamelDaprStateStore (producer)

Constant: STATE_STORE

The name of the Dapr state store to interact with, defined in statestore.yaml config.

String

CamelDaprKey (producer)

Constant: KEY

The key used to identify the state object within the specified state store.

String

CamelDaprETag (producer)

Constant: E_TAG

The eTag for optimistic concurrency during state save or delete operations.

String

CamelDaprConcurrency (producer)

Constant: CONCURRENCY

Concurrency mode to use with state operations.

Enum values:

  • FIRST_WRITE

  • LAST_WRITE

Concurrency

CamelDaprConsistency (producer)

Constant: CONSISTENCY

Consistency level to use with state operations.

Enum values:

  • EVENTUAL

  • STRONG

Consistency

CamelDaprMetadata (producer)

Constant: METADATA

Additional key-value pairs to be passed to the state store.

Map

CamelDaprStates (producer)

Constant: STATES

List of states for bulk save operation.

List

CamelDaprKeys (producer)

Constant: KEYS

List of keys for bulk get operation.

List

CamelDaprTransactions (producer)

Constant: TRANSACTIONS

List of transactions for execute transactions state operations.

List

Dapr Producer operations

Camel Dapr component provides a wide range of operations on the producer side:

1. invokeService:

Using service invocation, your application can reliably and securely communicate with other applications using the standard HTTP protocol. For this operation serviceToInvoke and methodToInvoke are mandatory.

2. state:

State management in Dapr allows your application to save, get and delete key-value pairs in a reliable and consistent way. For these operations stateStore is required.

State Operations

Operation Description

save

Store or update a single key-value pair in the state store. For this operation payload and key are required.

saveBulk

Store or update multiple key-value pairs in the state store in a single operation. For this operation (List<State<?>>) states is a required field.

get

Retrieve the value of a specific key from the state store. For this operation key is a required field.

getBulk

Retrieve values for multiple keys from the state store in a single request. For this operation (List<String>) keys is a required field.

delete

Remove a specific key and its associated value from the state store. For this operation key is a required field.

executeTransaction

Execute a batch of operations (save or delete) atomically within a transaction. For this operation (List<TransactionalStateOperation<?>>) transactions is a required field.

Examples

Producer Operations Examples

  • invokeService -

from("direct:start")
  .process(exchange -> {
    // set the header you want the producer to evaluate, refer to the previous
    // section to learn about the headers that can be set
    // e.g.:
    exchange.getIn().setHeader(DaprConstants.VERB, "GET");
  })
  .to("dapr:invokeService?serviceToInvoke=myService&methodToInvoke=myMethod")
  .to("mock:result");
  • state: save -

from("direct:start")
  .process(exchange -> {
    // set the payload you want to save as state
    exchange.getIn().setBody("myValue");
  })
  .to("dapr:state?stateOperation=save&stateStore=myStore&key=myKey")
  .to("mock:result");
  • state: saveBulk -

from("direct:start")
  .process(exchange -> {
    // set the list of states you want to save
    State<String> state1 = new State<>("key1", "val1", "etag1");
    State<String> state2 = new State<>("key2", "val1", "etag2");
    List<State<?>> states = List.of(state1, state2);
    exchange.getMessage().setHeader(DaprConstants.STATES, states);
  })
  .to("dapr:state?stateOperation=saveBulk&stateStore=myStore")
  .to("mock:result");
  • state: get -

import org.apache.camel.component.dapr.DaprConstants;from("direct:start")
  .process(exchange -> {
    // set the header you want the producer to evaluate, refer to the previous
    // section to learn about the headers that can be set
    // e.g.:
    exchange.getIn().setHeader(DaprConstants.CONCURRENCY, "FIRST_WRITE");
    exchange.getIn().setHeader(DaprConstants.CONSISTENCY, "EVENTUAL");
  })
  .to("dapr:state?stateOperation=get&stateStore=myStore&key=myKey")
  .to("mock:result");
  • state: getBulk -

import org.apache.camel.component.dapr.DaprConstants;from("direct:start")
  .process(exchange -> {
    // set the header you want the producer to evaluate, refer to the previous
    // section to learn about the headers that can be set
    // e.g.:
    exchange.getIn().setHeader(DaprConstants.KEYS, List.of("key1", "key2"));
  })
  .to("dapr:state?stateOperation=getBulk&stateStore=myStore")
  .to("mock:result");
  • state: delete -

import org.apache.camel.component.dapr.DaprConstants;from("direct:start")
  .process(exchange -> {
    // set the header you want the producer to evaluate, refer to the previous
    // section to learn about the headers that can be set
    // e.g.:
    exchange.getIn().setHeader(DaprConstants.METADATA, Map.of("partitionKey", "myPartitionKey"));
  })
  .to("dapr:state?stateOperation=delete&stateStore=myStore&key=myKey")
  .to("mock:result");
  • state: executeTransaction -

import org.apache.camel.component.dapr.DaprConstants;from("direct:start")
  .process(exchange -> {
    // set the header you want the producer to evaluate, refer to the previous
    // section to learn about the headers that can be set
    // e.g.:
    TransactionalStateOperation.OperationType op = TransactionalStateOperation.OperationType.UPSERT;
    List<TransactionalStateOperation<?>> transactions = List.of(new TransactionalStateOperation<>(op, new State<>("myKey")));
    exchange.getIn().setHeader(DaprConstants.TRANSACTIONS, transactions);
  })
  .to("dapr:state?stateOperation=executeTransaction&stateStore=myStore")
  .to("mock:result");

Spring Boot Auto-Configuration

When using dapr 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-dapr-starter</artifactId>
  <version>x.x.x</version>
  <!-- use the same version as your Camel core version -->
</dependency>

The component supports 14 options, which are listed below.

Name Description Default Type

camel.component.dapr.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.dapr.concurrency

Concurrency mode to use with state operations.

StateOptions$Concurrency

camel.component.dapr.configuration

The component configurations. The option is a org.apache.camel.component.dapr.DaprConfiguration type.

DaprConfiguration

camel.component.dapr.consistency

Consistency level to use with state operations.

StateOptions$Consistency

camel.component.dapr.e-tag

The eTag for optimistic concurrency during state save or delete operations.

String

camel.component.dapr.enabled

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

Boolean

camel.component.dapr.http-extension

HTTP method to use when invoking the service. Accepts verbs like GET, POST, PUT, DELETE, etc. Creates a minimal HttpExtension with no headers or query params. Takes precedence over verb. The option is a io.dapr.client.domain.HttpExtension type.

HttpExtension

camel.component.dapr.key

The key used to identify the state object within the specified state store.

String

camel.component.dapr.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

camel.component.dapr.method-to-invoke

The name of the method or route to invoke on the target service.

String

camel.component.dapr.service-to-invoke

Target service to invoke. Can be a Dapr App ID, a named HTTPEndpoint, or a FQDN/public URL.

String

camel.component.dapr.state-operation

The state operation to perform on the state store. Required for DaprOperation.state operation.

get

StateOperation

camel.component.dapr.state-store

The name of the Dapr state store to interact with, defined in statestore.yaml config.

String

camel.component.dapr.verb

The HTTP verb to use for invoking the method.

POST

String