Web3j Ethereum Blockchain
Since Camel 2.22
Both producer and consumer are supported
The Ethereum blockchain component uses the web3j client API and allows you to interact with Ethereum compatible nodes such as Geth, Parity, Quorum, Infura, etc.
Maven users will need to add the following dependency to their pom.xml
for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-web3j</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
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 Web3j Ethereum Blockchain component supports 38 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Contract address or a list of addresses. | List | ||
Default configuration. | Web3jConfiguration | ||
The address the transaction is send from. | String | ||
The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions. | latest | String | |
If true it returns the full transaction objects, if false only the hashes of the transactions. | false | boolean | |
The maximum gas allowed in this block. | BigInteger | ||
A transaction privateFor nodes with public keys in a Quorum network. | List | ||
If true, this will support Quorum API. | false | boolean | |
The address the transaction is directed to. | String | ||
The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions. | latest | String | |
Topics are order-dependent. Each topic can also be a list of topics. Specify multiple topics separated by comma. | String | ||
The preconfigured Web3j object. | Web3j | ||
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 | |
Contract address. | String | ||
The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions. | latest | String | |
Hash of the block where this transaction was in. | String | ||
A random hexadecimal(32 bytes) ID identifying the client. | String | ||
The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. | String | ||
The local database name. | String | ||
The filter id to use. | BigInteger | ||
Gas price used for each paid gas. | BigInteger | ||
A hexadecimal string representation (32 bytes) of the hash rate. | String | ||
The header’s pow-hash (256 bits) used for submitting a proof-of-work solution. | String | ||
The transactions/uncle index position in the block. | BigInteger | ||
The key name in the database. | 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 mix digest (256 bits) used for submitting a proof-of-work solution. | String | ||
The nonce found (64 bits) used for submitting a proof-of-work solution. | String | ||
Operation to use. | transaction | String | |
The transaction index position withing a block. | BigInteger | ||
The priority of a whisper message. | BigInteger | ||
Message to sign by calculating an Ethereum specific signature. | String | ||
The signed transaction data for a new message call transaction or a contract creation for signed transactions. | String | ||
The source code to compile. | String | ||
The information about a transaction requested by transaction hash. | String | ||
The time to live in seconds of a whisper message. | BigInteger | ||
The value sent within a transaction. | BigInteger | ||
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 Web3j Ethereum Blockchain endpoint is configured using URI syntax:
web3j:nodeAddress
with the following path and query parameters:
Query Parameters (38 parameters)
Name | Description | Default | Type |
---|---|---|---|
Contract address or a list of addresses. | List | ||
The address the transaction is send from. | String | ||
The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions. | latest | String | |
If true it returns the full transaction objects, if false only the hashes of the transactions. | false | boolean | |
The maximum gas allowed in this block. | BigInteger | ||
A transaction privateFor nodes with public keys in a Quorum network. | List | ||
If true, this will support Quorum API. | false | boolean | |
The address the transaction is directed to. | String | ||
The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions. | latest | String | |
Topics are order-dependent. Each topic can also be a list of topics. Specify multiple topics separated by comma. | String | ||
The preconfigured Web3j object. | Web3j | ||
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 | |
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 | ||
Sets the exchange pattern when the consumer creates an exchange. Enum values:
| ExchangePattern | ||
Contract address. | String | ||
The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions. | latest | String | |
Hash of the block where this transaction was in. | String | ||
A random hexadecimal(32 bytes) ID identifying the client. | String | ||
The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. | String | ||
The local database name. | String | ||
The filter id to use. | BigInteger | ||
Gas price used for each paid gas. | BigInteger | ||
A hexadecimal string representation (32 bytes) of the hash rate. | String | ||
The header’s pow-hash (256 bits) used for submitting a proof-of-work solution. | String | ||
The transactions/uncle index position in the block. | BigInteger | ||
The key name in the database. | String | ||
The mix digest (256 bits) used for submitting a proof-of-work solution. | String | ||
The nonce found (64 bits) used for submitting a proof-of-work solution. | String | ||
Operation to use. | transaction | String | |
The transaction index position withing a block. | BigInteger | ||
The priority of a whisper message. | BigInteger | ||
Message to sign by calculating an Ethereum specific signature. | String | ||
The signed transaction data for a new message call transaction or a contract creation for signed transactions. | String | ||
The source code to compile. | String | ||
The information about a transaction requested by transaction hash. | String | ||
The time to live in seconds of a whisper message. | BigInteger | ||
The value sent within a transaction. | BigInteger | ||
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 Web3j Ethereum Blockchain component supports 38 message header(s), which is/are listed below:
Name | Description | Default | Type |
---|---|---|---|
Constant: | A hexadecimal string representation (32 bytes) of the hash rate. | String | |
Constant: | The id. | Long | |
Constant: | The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions. | String | |
Constant: | Contract address. | String | |
Constant: | Contract address or a list of addresses. | List | |
Constant: | The address the transaction is send from. | String | |
Constant: | The address the transaction is directed to. | String | |
Constant: | The transaction index position withing a block. | String | |
Constant: | Hash of the block where this transaction was in. | String | |
Constant: | The information about a transaction requested by transaction hash. | String | |
SHA3_HASH_OF_DATA_TO_SIGN (producer) Constant: | Message to sign by calculating an Ethereum specific signature. | String | |
SIGNED_TRANSACTION_DATA (producer) Constant: | The signed transaction data for a new message call transaction or a contract creation for signed transactions. | String | |
FULL_TRANSACTION_OBJECTS (producer) Constant: | If true it returns the full transaction objects, if false only the hashes of the transactions. | Boolean | |
Constant: | The transactions/uncle index position in the block. | String | |
Constant: | The source code to compile. | String | |
Constant: | The filter id to use. | BigInteger | |
Constant: | The local database name. | String | |
Constant: | The key name in the database. | String | |
Constant: | The nonce found (64 bits) used for submitting a proof-of-work solution. | BigInteger | |
Constant: | The header’s pow-hash (256 bits) used for submitting a proof-of-work solution. | String | |
Constant: | The mix digest (256 bits) used for submitting a proof-of-work solution. | String | |
Constant: | A random hexadecimal(32 bytes) ID identifying the client. | String | |
Constant: | Gas price used for each paid gas. | BigInteger | |
Constant: | The maximum gas allowed in this block. | BigInteger | |
Constant: | The value sent within a transaction. | BigInteger | |
Constant: | The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. | String | |
Constant: | The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions. | String | |
Constant: | The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions. | String | |
Constant: | Topics are order-dependent. Each topic can also be a list of topics. Specify multiple topics separated by comma. | List | |
Constant: | The priority of a whisper message. | BigInteger | |
Constant: | The time to live in seconds of a whisper message. | BigInteger | |
Constant: | A transaction privateFor nodes with public keys in a Quorum network. | List | |
Constant: | A transaction privateFrom. | String | |
Constant: | The error code. | int | |
Constant: | The error data. | String | |
Constant: | The error message. | String | |
Constant: | The status of the operation. | String | |
Constant: | The operation. | String |
All URI options can also be set as exchange headers.
Samples
Listen for new mined blocks and send the block hash to a jms queue:
from("web3j://http://127.0.0.1:7545?operation=ETH_BLOCK_HASH_OBSERVABLE")
.to("jms:queue:blocks");
Use the block hash code to retrieve the block and full transaction details:
from("jms:queue:blocks")
.setHeader(BLOCK_HASH, body())
.to("web3j://http://127.0.0.1:7545?operation=ETH_GET_BLOCK_BY_HASH&fullTransactionObjects=true");
Read the balance of an address at a specific block number:
from("direct:start")
.to("web3j://http://127.0.0.1:7545?operation=ETH_GET_BALANCE&address=0xc8CDceCE5d006dAB638029EBCf6Dd666efF5A952&atBlock=10");
Spring Boot Auto-Configuration
When using web3j 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-web3j-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
The component supports 39 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Contract address. | String | ||
Contract address or a list of addresses. | List | ||
The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions. | latest | 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 | |
Hash of the block where this transaction was in. | String | ||
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 | |
A random hexadecimal(32 bytes) ID identifying the client. | String | ||
Default configuration. The option is a org.apache.camel.component.web3j.Web3jConfiguration type. | Web3jConfiguration | ||
The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. | String | ||
The local database name. | String | ||
Whether to enable auto configuration of the web3j component. This is enabled by default. | Boolean | ||
The filter id to use. The option is a java.math.BigInteger type. | BigInteger | ||
The address the transaction is send from. | String | ||
The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions. | latest | String | |
If true it returns the full transaction objects, if false only the hashes of the transactions. | false | Boolean | |
The maximum gas allowed in this block. The option is a java.math.BigInteger type. | BigInteger | ||
Gas price used for each paid gas. The option is a java.math.BigInteger type. | BigInteger | ||
A hexadecimal string representation (32 bytes) of the hash rate. | String | ||
The header’s pow-hash (256 bits) used for submitting a proof-of-work solution. | String | ||
The transactions/uncle index position in the block. The option is a java.math.BigInteger type. | BigInteger | ||
The key name in the database. | 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 mix digest (256 bits) used for submitting a proof-of-work solution. | String | ||
The nonce found (64 bits) used for submitting a proof-of-work solution. | String | ||
Operation to use. | transaction | String | |
The transaction index position withing a block. The option is a java.math.BigInteger type. | BigInteger | ||
The priority of a whisper message. The option is a java.math.BigInteger type. | BigInteger | ||
A transaction privateFor nodes with public keys in a Quorum network. | List | ||
If true, this will support Quorum API. | false | Boolean | |
Message to sign by calculating an Ethereum specific signature. | String | ||
The signed transaction data for a new message call transaction or a contract creation for signed transactions. | String | ||
The source code to compile. | String | ||
The address the transaction is directed to. | String | ||
The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions. | latest | String | |
Topics are order-dependent. Each topic can also be a list of topics. Specify multiple topics separated by comma. | String | ||
The information about a transaction requested by transaction hash. | String | ||
The time to live in seconds of a whisper message. The option is a java.math.BigInteger type. | BigInteger | ||
The value sent within a transaction. The option is a java.math.BigInteger type. | BigInteger | ||
The preconfigured Web3j object. The option is a org.web3j.protocol.Web3j type. | Web3j |