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:

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>

URI Format

web3j://<local/remote host:port or local IPC path>[?options]

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 Web3j Ethereum Blockchain component supports 38 options, which are listed below.

Name Description Default Type

addresses (common)

Contract address or a list of addresses.

List

configuration (common)

Default configuration.

Web3jConfiguration

fromAddress (common)

The address the transaction is send from.

String

fromBlock (common)

The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions.

latest

String

fullTransactionObjects (common)

If true it returns the full transaction objects, if false only the hashes of the transactions.

false

boolean

gasLimit (common)

The maximum gas allowed in this block.

BigInteger

privateFor (common)

A transaction privateFor nodes with public keys in a Quorum network.

List

quorumAPI (common)

If true, this will support Quorum API.

false

boolean

toAddress (common)

The address the transaction is directed to.

String

toBlock (common)

The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions.

latest

String

topics (common)

Topics are order-dependent. Each topic can also be a list of topics. Specify multiple topics separated by comma.

String

web3j (common)

The preconfigured Web3j object.

Web3j

bridgeErrorHandler (consumer)

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. 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

address (producer)

Contract address.

String

atBlock (producer)

The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions.

latest

String

blockHash (producer)

Hash of the block where this transaction was in.

String

clientId (producer)

A random hexadecimal(32 bytes) ID identifying the client.

String

data (producer)

The compiled code of a contract OR the hash of the invoked method signature and encoded parameters.

String

databaseName (producer)

The local database name.

String

filterId (producer)

The filter id to use.

BigInteger

gasPrice (producer)

Gas price used for each paid gas.

BigInteger

hashrate (producer)

A hexadecimal string representation (32 bytes) of the hash rate.

String

headerPowHash (producer)

The header’s pow-hash (256 bits) used for submitting a proof-of-work solution.

String

index (producer)

The transactions/uncle index position in the block.

BigInteger

keyName (producer)

The key name in the database.

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

mixDigest (producer)

The mix digest (256 bits) used for submitting a proof-of-work solution.

String

nonce (producer)

The nonce found (64 bits) used for submitting a proof-of-work solution.

String

operation (producer)

Operation to use.

transaction

String

position (producer)

The transaction index position withing a block.

BigInteger

priority (producer)

The priority of a whisper message.

BigInteger

sha3HashOfDataToSign (producer)

Message to sign by calculating an Ethereum specific signature.

String

signedTransactionData (producer)

The signed transaction data for a new message call transaction or a contract creation for signed transactions.

String

sourceCode (producer)

The source code to compile.

String

transactionHash (producer)

The information about a transaction requested by transaction hash.

String

ttl (producer)

The time to live in seconds of a whisper message.

BigInteger

value (producer)

The value sent within a transaction.

BigInteger

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 Web3j Ethereum Blockchain endpoint is configured using URI syntax:

web3j:nodeAddress

With the following path and query parameters:

Path Parameters (1 parameters)

Name Description Default Type

nodeAddress (common)

Required Sets the node address used to communicate.

String

Query Parameters (38 parameters)

Name Description Default Type

addresses (common)

Contract address or a list of addresses.

List

fromAddress (common)

The address the transaction is send from.

String

fromBlock (common)

The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions.

latest

String

fullTransactionObjects (common)

If true it returns the full transaction objects, if false only the hashes of the transactions.

false

boolean

gasLimit (common)

The maximum gas allowed in this block.

BigInteger

privateFor (common)

A transaction privateFor nodes with public keys in a Quorum network.

List

quorumAPI (common)

If true, this will support Quorum API.

false

boolean

toAddress (common)

The address the transaction is directed to.

String

toBlock (common)

The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions.

latest

String

topics (common)

Topics are order-dependent. Each topic can also be a list of topics. Specify multiple topics separated by comma.

String

web3j (common)

The preconfigured Web3j object.

Web3j

bridgeErrorHandler (consumer (advanced))

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. 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

ExchangePattern

address (producer)

Contract address.

String

atBlock (producer)

The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions.

latest

String

blockHash (producer)

Hash of the block where this transaction was in.

String

clientId (producer)

A random hexadecimal(32 bytes) ID identifying the client.

String

data (producer)

The compiled code of a contract OR the hash of the invoked method signature and encoded parameters.

String

databaseName (producer)

The local database name.

String

filterId (producer)

The filter id to use.

BigInteger

gasPrice (producer)

Gas price used for each paid gas.

BigInteger

hashrate (producer)

A hexadecimal string representation (32 bytes) of the hash rate.

String

headerPowHash (producer)

The header’s pow-hash (256 bits) used for submitting a proof-of-work solution.

String

index (producer)

The transactions/uncle index position in the block.

BigInteger

keyName (producer)

The key name in the database.

String

mixDigest (producer)

The mix digest (256 bits) used for submitting a proof-of-work solution.

String

nonce (producer)

The nonce found (64 bits) used for submitting a proof-of-work solution.

String

operation (producer)

Operation to use.

transaction

String

position (producer)

The transaction index position withing a block.

BigInteger

priority (producer)

The priority of a whisper message.

BigInteger

sha3HashOfDataToSign (producer)

Message to sign by calculating an Ethereum specific signature.

String

signedTransactionData (producer)

The signed transaction data for a new message call transaction or a contract creation for signed transactions.

String

sourceCode (producer)

The source code to compile.

String

transactionHash (producer)

The information about a transaction requested by transaction hash.

String

ttl (producer)

The time to live in seconds of a whisper message.

BigInteger

value (producer)

The value sent within a transaction.

BigInteger

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 Web3j Ethereum Blockchain component supports 38 message header(s), which is/are listed below:

Name Description Default Type

ETH_HASHRATE (producer)

Constant: ETH_HASHRATE

A hexadecimal string representation (32 bytes) of the hash rate.

String

ID (producer)

Constant: ID

The id.

Long

AT_BLOCK (producer)

Constant: AT_BLOCK

The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions.

String

ADDRESS (producer)

Constant: ADDRESS

Contract address.

String

ADDRESSES (producer)

Constant: ADDRESSES

Contract address or a list of addresses.

List

FROM_ADDRESS (producer)

Constant: FROM_ADDRESS

The address the transaction is send from.

String

TO_ADDRESS (producer)

Constant: TO_ADDRESS

The address the transaction is directed to.

String

POSITION (producer)

Constant: POSITION

The transaction index position withing a block.

String

BLOCK_HASH (producer)

Constant: BLOCK_HASH

Hash of the block where this transaction was in.

String

TRANSACTION_HASH (producer)

Constant: TRANSACTION_HASH

The information about a transaction requested by transaction hash.

String

SHA3_HASH_OF_DATA_TO_SIGN (producer)

Constant: SHA3_HASH_OF_DATA_TO_SIGN

Message to sign by calculating an Ethereum specific signature.

String

SIGNED_TRANSACTION_DATA (producer)

Constant: SIGNED_TRANSACTION_DATA

The signed transaction data for a new message call transaction or a contract creation for signed transactions.

String

FULL_TRANSACTION_OBJECTS (producer)

Constant: FULL_TRANSACTION_OBJECTS

If true it returns the full transaction objects, if false only the hashes of the transactions.

Boolean

INDEX (producer)

Constant: INDEX

The transactions/uncle index position in the block.

String

SOURCE_CODE (producer)

Constant: SOURCE_CODE

The source code to compile.

String

FILTER_ID (producer)

Constant: FILTER_ID

The filter id to use.

BigInteger

DATABASE_NAME (producer)

Constant: DATABASE_NAME

The local database name.

String

KEY_NAME (producer)

Constant: KEY_NAME

The key name in the database.

String

NONCE (producer)

Constant: NONCE

The nonce found (64 bits) used for submitting a proof-of-work solution.

BigInteger

HEADER_POW_HASH (producer)

Constant: HEADER_POW_HASH

The header’s pow-hash (256 bits) used for submitting a proof-of-work solution.

String

MIX_DIGEST (producer)

Constant: MIX_DIGEST

The mix digest (256 bits) used for submitting a proof-of-work solution.

String

CLIENT_ID (producer)

Constant: CLIENT_ID

A random hexadecimal(32 bytes) ID identifying the client.

String

GAS_PRICE (producer)

Constant: GAS_PRICE

Gas price used for each paid gas.

BigInteger

GAS_LIMIT (producer)

Constant: GAS_LIMIT

The maximum gas allowed in this block.

BigInteger

VALUE (producer)

Constant: VALUE

The value sent within a transaction.

BigInteger

DATA (producer)

Constant: DATA

The compiled code of a contract OR the hash of the invoked method signature and encoded parameters.

String

FROM_BLOCK (producer)

Constant: FROM_BLOCK

The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions.

String

TO_BLOCK (producer)

Constant: TO_BLOCK

The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions.

String

TOPICS (producer)

Constant: TOPICS

Topics are order-dependent. Each topic can also be a list of topics. Specify multiple topics separated by comma.

List

PRIORITY (producer)

Constant: PRIORITY

The priority of a whisper message.

BigInteger

TTL (producer)

Constant: TTL

The time to live in seconds of a whisper message.

BigInteger

PRIVATE_FOR (producer)

Constant: PRIVATE_FOR

A transaction privateFor nodes with public keys in a Quorum network.

List

PRIVATE_FROM (producer)

Constant: PRIVATE_FROM

A transaction privateFrom.

String

ERROR_CODE (producer)

Constant: ERROR_CODE

The error code.

int

ERROR_DATA (producer)

Constant: ERROR_DATA

The error data.

String

ERROR_MESSAGE (producer)

Constant: ERROR_MESSAGE

The error message.

String

status (consumer)

Constant: HEADER_STATUS

The status of the operation.

String

operation (consumer)

Constant: HEADER_OPERATION

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

camel.component.web3j.address

Contract address.

String

camel.component.web3j.addresses

Contract address or a list of addresses.

List

camel.component.web3j.at-block

The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions.

latest

String

camel.component.web3j.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.web3j.block-hash

Hash of the block where this transaction was in.

String

camel.component.web3j.bridge-error-handler

Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. 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.web3j.client-id

A random hexadecimal(32 bytes) ID identifying the client.

String

camel.component.web3j.configuration

Default configuration. The option is a org.apache.camel.component.web3j.Web3jConfiguration type.

Web3jConfiguration

camel.component.web3j.data

The compiled code of a contract OR the hash of the invoked method signature and encoded parameters.

String

camel.component.web3j.database-name

The local database name.

String

camel.component.web3j.enabled

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

Boolean

camel.component.web3j.filter-id

The filter id to use. The option is a java.math.BigInteger type.

BigInteger

camel.component.web3j.from-address

The address the transaction is send from.

String

camel.component.web3j.from-block

The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions.

latest

String

camel.component.web3j.full-transaction-objects

If true it returns the full transaction objects, if false only the hashes of the transactions.

false

Boolean

camel.component.web3j.gas-limit

The maximum gas allowed in this block. The option is a java.math.BigInteger type.

BigInteger

camel.component.web3j.gas-price

Gas price used for each paid gas. The option is a java.math.BigInteger type.

BigInteger

camel.component.web3j.hashrate

A hexadecimal string representation (32 bytes) of the hash rate.

String

camel.component.web3j.header-pow-hash

The header’s pow-hash (256 bits) used for submitting a proof-of-work solution.

String

camel.component.web3j.index

The transactions/uncle index position in the block. The option is a java.math.BigInteger type.

BigInteger

camel.component.web3j.key-name

The key name in the database.

String

camel.component.web3j.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.web3j.mix-digest

The mix digest (256 bits) used for submitting a proof-of-work solution.

String

camel.component.web3j.nonce

The nonce found (64 bits) used for submitting a proof-of-work solution.

String

camel.component.web3j.operation

Operation to use.

transaction

String

camel.component.web3j.position

The transaction index position withing a block. The option is a java.math.BigInteger type.

BigInteger

camel.component.web3j.priority

The priority of a whisper message. The option is a java.math.BigInteger type.

BigInteger

camel.component.web3j.private-for

A transaction privateFor nodes with public keys in a Quorum network.

List

camel.component.web3j.quorum-a-p-i

If true, this will support Quorum API.

false

Boolean

camel.component.web3j.sha3-hash-of-data-to-sign

Message to sign by calculating an Ethereum specific signature.

String

camel.component.web3j.signed-transaction-data

The signed transaction data for a new message call transaction or a contract creation for signed transactions.

String

camel.component.web3j.source-code

The source code to compile.

String

camel.component.web3j.to-address

The address the transaction is directed to.

String

camel.component.web3j.to-block

The block number, or the string latest for the last mined block or pending, earliest for not yet mined transactions.

latest

String

camel.component.web3j.topics

Topics are order-dependent. Each topic can also be a list of topics. Specify multiple topics separated by comma.

String

camel.component.web3j.transaction-hash

The information about a transaction requested by transaction hash.

String

camel.component.web3j.ttl

The time to live in seconds of a whisper message. The option is a java.math.BigInteger type.

BigInteger

camel.component.web3j.value

The value sent within a transaction. The option is a java.math.BigInteger type.

BigInteger

camel.component.web3j.web3j

The preconfigured Web3j object. The option is a org.web3j.protocol.Web3j type.

Web3j