PostgreSQL Event
Since Camel 2.15
Both producer and consumer are supported
This is a component for Apache Camel that allows for Producing/Consuming PostgreSQL notifications with the LISTEN/NOTIFY commands. It needs PostgreSQL 9.0 or newer, as the producer calls pg_notify.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-pgevent</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> URI format
The pgevent component uses the following two styles of endpoint URI notation:
pgevent://host:port/database/channel[?parameters] pgevent:///database/channel?datasource=#myDataSource[¶meters]
The port can be left out (pgevent://host/database/channel) and is then 5432. With the datasource option, the connection comes from the data source, and the host and port are not used.
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,*.yamlfiles, 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 PostgreSQL Event component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
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 | |
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 | |
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 PostgreSQL Event endpoint is configured using URI syntax:
pgevent:host:port/database/channel
With the following path and query parameters:
Path Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The host name or IP address of the PostgreSQL server. Not used when datasource is set. | localhost | String | |
The TCP port of the PostgreSQL server. Not used when datasource is set. | 5432 | Integer | |
Required The name of the database to connect to. Not used when datasource is set. | String | ||
Required The name of the channel that the consumer listens on (LISTEN) and that the producer sends the notifications to (pg_notify). The consumer accepts only letters, digits and underscores, not starting with a digit, and PostgreSQL folds the name to lower case when it listens; the producer sends to the name as it is given, so use a lower case name. | String |
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
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 | |
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 | ||
The interval in milliseconds between the attempts of the consumer to connect again after the driver reports that the connection to the database is lost. The first attempt is made after one second, and the attempts go on until one succeeds or the consumer stops. Notifications sent while the consumer is not connected are lost. A failure to connect when the consumer starts is not retried. | 5000 | int | |
A custom thread pool to process the received notifications, instead of the pool that the consumer creates. Camel does not shut it down when the consumer stops. | ExecutorService | ||
The number of core threads of the worker pool that the consumer creates to process the received notifications. Not used when workerPool is set. | 1 | int | |
The maximum number of threads of the worker pool that the consumer creates. Threads above the core size are only added when the task queue of the pool (1000 tasks with the default thread pool profile) is full. Not used when workerPool is set. | 10 | int | |
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 DataSource to get the database connection from, instead of connecting to host, port and database with user and pass. It must hand out connections of the pgjdbc-ng driver (com.impossibl.postgres.api.jdbc.PGConnection, or a wrapper of one); connections of the PostgreSQL JDBC driver (org.postgresql) are not supported. The consumer and the producer each keep one connection while they run. | DataSource | ||
The password of the user. Not used when datasource is set. | String | ||
The user name to log in to PostgreSQL with. Not used when datasource is set. | postgres | String |
Message Headers
The PostgreSQL Event component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
| Constant: | The name of the channel that the notification was received on, as PostgreSQL reports it. | String |
Message Body
The message body at each side of the PostgreSQL Event component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Consumer |
| The payload of the notification as a String, empty when it was sent without one; the channel is in the channel header. |
Producer input |
| The payload of the notification, converted to a String: text, or bytes, a stream or a file of it; a Map or a POJO is sent as its toString(), so marshal it first. PostgreSQL takes a payload of less than 8000 bytes, and no body sends an empty payload. |
Producer output |
|
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Usage
Listening and notifying
The consumer listens on a channel (LISTEN), and the message body is the payload of each notification. The producer sends the message body as the payload of a notification on the channel (NOTIFY):
- route:
from:
uri: pgevent://localhost:5432/mydb/orders
parameters:
user: postgres
pass: "{{pg.password}}"
steps:
- log:
message: "Notification on channel orders: ${body}"
- route:
from:
uri: timer:notify
parameters:
period: 5000
steps:
- setBody:
expression:
simple:
expression: "new order at ${date:now:HH:mm:ss}"
- to:
uri: pgevent://localhost:5432/mydb/orders
parameters:
user: postgres
pass: "{{pg.password}}"