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 mode to use with state operations. Enum values:
| Concurrency | ||
The component configurations. | DaprConfiguration | ||
Consistency level to use with state operations. Enum values:
| Consistency | ||
The eTag for optimistic concurrency during state save or delete operations. | String | ||
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 | ||
The key used to identify the state object within the specified state store. | String | ||
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 | |
The name of the method or route to invoke on the target service. | String | ||
Target service to invoke. Can be a Dapr App ID, a named HTTPEndpoint, or a FQDN/public URL. | String | ||
The state operation to perform on the state store. Required for DaprOperation.state operation. Enum values:
| get | StateOperation | |
The name of the Dapr state store to interact with, defined in statestore.yaml config. | String | ||
The HTTP verb to use for invoking the method. | POST | String | |
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:
Query Parameters (11 parameters)
Name | Description | Default | Type |
---|---|---|---|
Concurrency mode to use with state operations. Enum values:
| Concurrency | ||
Consistency level to use with state operations. Enum values:
| Consistency | ||
The eTag for optimistic concurrency during state save or delete operations. | String | ||
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 | ||
The key used to identify the state object within the specified state store. | String | ||
The name of the method or route to invoke on the target service. | String | ||
Target service to invoke. Can be a Dapr App ID, a named HTTPEndpoint, or a FQDN/public URL. | String | ||
The state operation to perform on the state store. Required for DaprOperation.state operation. Enum values:
| get | StateOperation | |
The name of the Dapr state store to interact with, defined in statestore.yaml config. | String | ||
The HTTP verb to use for invoking the method. | POST | String | |
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: | Target service to invoke. Can be a Dapr App ID, a named HTTPEndpoint, or a FQDN/public URL. | String | |
CamelDaprMethodToInvoke (producer) Constant: | The name of the method or route to invoke on the target service. | String | |
Constant: | The HTTP verb to use for service invocation. | String | |
CamelDaprQueryParameters (producer) Constant: | The query parameters for HTTP requests. | Map | |
CamelDaprHttpHeaders (producer) Constant: | The headers for HTTP requests. | Map | |
CamelDaprHttpExtension (producer) Constant: | The HttpExtension object for service invocation. Takes precedence over verb. | HttpExtension | |
CamelDaprStateOperation (producer) Constant: | The state operation to perform on the state store. Required for DaprOperation.state operation. | get | StateOperation |
CamelDaprStateStore (producer) Constant: | The name of the Dapr state store to interact with, defined in statestore.yaml config. | String | |
Constant: | The key used to identify the state object within the specified state store. | String | |
Constant: | The eTag for optimistic concurrency during state save or delete operations. | String | |
CamelDaprConcurrency (producer) Constant: | Concurrency mode to use with state operations. Enum values:
| Concurrency | |
CamelDaprConsistency (producer) Constant: | Consistency level to use with state operations. Enum values:
| Consistency | |
Constant: | Additional key-value pairs to be passed to the state store. | Map | |
Constant: | List of states for bulk save operation. | List | |
Constant: | List of keys for bulk get operation. | List | |
CamelDaprTransactions (producer) Constant: | 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 |
---|---|
| Store or update a single key-value pair in the state store. For this operation |
| Store or update multiple key-value pairs in the state store in a single operation. For this operation (List<State<?>>) |
| Retrieve the value of a specific key from the state store. For this operation |
| Retrieve values for multiple keys from the state store in a single request. For this operation (List<String>) |
| Remove a specific key and its associated value from the state store. For this operation |
| Execute a batch of operations (save or delete) atomically within a transaction. For this operation (List<TransactionalStateOperation<?>>) |
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 |
---|---|---|---|
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 | |
Concurrency mode to use with state operations. | StateOptions$Concurrency | ||
The component configurations. The option is a org.apache.camel.component.dapr.DaprConfiguration type. | DaprConfiguration | ||
Consistency level to use with state operations. | StateOptions$Consistency | ||
The eTag for optimistic concurrency during state save or delete operations. | String | ||
Whether to enable auto configuration of the dapr component. This is enabled by default. | Boolean | ||
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 | ||
The key used to identify the state object within the specified state store. | String | ||
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 | |
The name of the method or route to invoke on the target service. | String | ||
Target service to invoke. Can be a Dapr App ID, a named HTTPEndpoint, or a FQDN/public URL. | String | ||
The state operation to perform on the state store. Required for DaprOperation.state operation. | get | StateOperation | |
The name of the Dapr state store to interact with, defined in statestore.yaml config. | String | ||
The HTTP verb to use for invoking the method. | POST | String |