Chapter 36. Google Pubsub Since Camel 2.19 Both producer and consumer are supported. The Google Pubsub component provides access to the Cloud Pub/Sub Infrastructure via the Google Cloud Java Client for Google Cloud Pub/Sub . 36.1. Dependencies When using google-pubsub with Red Hat build of Camel 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-google-pubsub-starter</artifactId> </dependency> 36.2. URI Format The Google Pubsub Component uses the following URI format: Destination Name can be either a topic or a subscription name. 36.3. Configuring Options Camel components are configured on two levels: Component level Endpoint level 36.3.1. Component Level Options The component level is the highest level. The configurations you define at this level are inherited by all the endpoints. For example, a component can have security settings, credentials for authentication, urls for network connection, and so on. Since components typically have pre-configured defaults for the most common cases, you may need to only configure a few component options, or maybe none at all. You can configure components with Component DSL in a configuration file (application.properties|yaml), or directly with Java code. 36.3.2. Endpoint Level Options At the Endpoint level you have many options, which you can use to configure what you want the endpoint to do. The options are categorized according to whether the endpoint is used as a consumer (from) or as a producer (to) or used for both. You can configure endpoints directly in the endpoint URI as path and query parameters. You can also use Endpoint DSL and DataFormat DSL as type safe ways of configuring endpoints and data formats in Java. When configuring options, use Property Placeholders for urls, port numbers, sensitive information, and other settings. Placeholders allows you to externalize the configuration from your code, giving you more flexible and reusable code. 36.4. Component Options The Google Pubsub component supports 10 options, which are listed below. Name Description Default Type authenticate (common) Use Credentials when interacting with PubSub service (no authentication is required when using emulator). true boolean endpoint (common) Endpoint to use with local Pub/Sub emulator. String serviceAccountKey (common) The Service account key that can be used as credentials for the PubSub publisher/subscriber. It can be loaded by default from classpath, but you can prefix with classpath:, file:, or http: to load the resource from different systems. String bridgeErrorHandler (consumer) 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 synchronousPullRetryableCodes (consumer) Comma-separated list of additional retryable error codes for synchronous pull. By default the PubSub client library retries ABORTED, UNAVAILABLE, UNKNOWN. 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 publisherCacheSize (producer) Maximum number of producers to cache. This could be increased if you have producers for lots of different topics. int publisherCacheTimeout (producer) How many milliseconds should each producer stay alive in the cache. int 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 publisherTerminationTimeout (advanced) How many milliseconds should a producer be allowed to terminate. int 36.5. Endpoint Options The Google Pubsub endpoint is configured using URI syntax: with the following path and query parameters: 36.5.1. Path Parameters (2 parameters) Name Description Default Type projectId (common) Required The Google Cloud PubSub Project Id. String destinationName (common) Required The Destination Name. For the consumer this will be the subscription name, while for the producer this will be the topic name. String 36.5.2. Query Parameters (15 parameters) Name Description Default Type authenticate (common) Use Credentials when interacting with PubSub service (no authentication is required when using emulator). true boolean loggerId (common) Logger ID to use when a match to the parent route required. String serviceAccountKey (common) The Service account key that can be used as credentials for the PubSub publisher/subscriber. It can be loaded by default from classpath, but you can prefix with classpath:, file:, or http: to load the resource from different systems. String ackMode (consumer) AUTO = exchange gets ack'ed/nack'ed on completion. NONE = downstream process has to ack/nack explicitly. Enum values: AUTO NONE AUTO AckMode concurrentConsumers (consumer) The number of parallel streams consuming from the subscription. 1 Integer maxAckExtensionPeriod (consumer) Set the maximum period a message ack deadline will be extended. Value in seconds. 3600 int maxMessagesPerPoll (consumer) The max number of messages to receive from the server in a single API call. 1 Integer synchronousPull (consumer) Synchronously pull batches of messages. false boolean bridgeErrorHandler (consumer (advanced)) 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 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 InOptionalOut ExchangePattern 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 messageOrderingEnabled (producer (advanced)) Should message ordering be enabled. false boolean pubsubEndpoint (producer (advanced)) Pub/Sub endpoint to use. Required when using message ordering, and ensures that messages are received in order even when multiple publishers are used. String serializer (producer (advanced)) Autowired A custom GooglePubsubSerializer to use for serializing message payloads in the producer. GooglePubsubSerializer 36.6. Message Headers The Google Pubsub component supports 5 message header(s), which are listed below: Name Description Default Type CamelGooglePubsubMessageId (common) Constant: MESSAGE_ID The ID of the message, assigned by the server when the message is published. String CamelGooglePubsubMsgAckId (consumer) Constant: ACK_ID The ID used to acknowledge the received message. String CamelGooglePubsubPublishTime (consumer) Constant: PUBLISH_TIME The time at which the message was published. Timestamp CamelGooglePubsubAttributes (common) Constant: ATTRIBUTES The attributes of the message. Map CamelGooglePubsubOrderingKey (producer) Constant: ORDERING_KEY If non-empty, identifies related messages for which publish order should be respected. String 36.7. Producer Endpoints Producer endpoints can accept and deliver to PubSub individual and grouped exchanges alike. Grouped exchanges have Exchange.GROUPED_EXCHANGE property set. Google PubSub expects the payload to be byte[] array, Producer endpoints will send: String body as byte[] encoded as UTF-8 byte[] body as is Everything else will be serialised into byte[] array A Map set as message header GooglePubsubConstants.ATTRIBUTES will be sent as PubSub attributes. Google PubSub supports ordered message delivery. To enable this set set the options messageOrderingEnabled to true, and the pubsubEndpoint to a GCP region. When producing messages set the message header GooglePubsubConstants.ORDERING_KEY . This will be set as the PubSub orderingKey for the message. More information in Ordering messages . Once exchange has been delivered to PubSub the PubSub Message ID will be assigned to the header GooglePubsubConstants.MESSAGE_ID . 36.8. Consumer Endpoints Google PubSub will redeliver the message if it has not been acknowledged within the time period set as a configuration option on the subscription. The component will acknowledge the message once exchange processing has been completed. If the route throws an exception, the exchange is marked as failed and the component will NACK the message - it will be redelivered immediately. To ack/nack the message the component uses Acknowledgement ID stored as header GooglePubsubConstants.ACK_ID . If the header is removed or tampered with, the ack will fail and the message will be redelivered again after the ack deadline. 36.9. Message Body The consumer endpoint returns the content of the message as byte[] - exactly as the underlying system sends it. It is up for the route to convert/unmarshall the contents. 36.10. Authentication Configuration By default this component aquires credentials using GoogleCredentials.getApplicationDefault() . This behavior can be disabled by setting authenticate option to false , in which case requests to Google API will be made without authentication details. This is only desirable when developing against an emulator. This behavior can be altered by supplying a path to a service account key file. 36.11. Rollback and Redelivery The rollback for Google PubSub relies on the idea of the Acknowledgement Deadline - the time period where Google PubSub expects to receive the acknowledgement. If the acknowledgement has not been received, the message is redelivered. Google provides an API to extend the deadline for a message. More information in Google PubSub Documentation . So, rollback is effectively a deadline extension API call with zero value - i.e. deadline is reached now and message can be redelivered to the consumer. It is possible to delay the message redelivery by setting the acknowledgement deadline explicitly for the rollback by setting the message header GooglePubsubConstants.ACK_DEADLINE to the value in seconds. 36.12. Spring Boot Auto-Configuration The component supports 11 options, which are listed below. Name Description Default Type camel.component.google-pubsub.authenticate Use Credentials when interacting with PubSub service (no authentication is required when using emulator). true Boolean camel.component.google-pubsub.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.google-pubsub.bridge-error-handler 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 camel.component.google-pubsub.enabled Whether to enable auto configuration of the google-pubsub component. This is enabled by default. Boolean camel.component.google-pubsub.endpoint Endpoint to use with local Pub/Sub emulator. String camel.component.google-pubsub.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.google-pubsub.publisher-cache-size Maximum number of producers to cache. This could be increased if you have producers for lots of different topics. Integer camel.component.google-pubsub.publisher-cache-timeout How many milliseconds should each producer stay alive in the cache. Integer camel.component.google-pubsub.publisher-termination-timeout How many milliseconds should a producer be allowed to terminate. Integer camel.component.google-pubsub.service-account-key The Service account key that can be used as credentials for the PubSub publisher/subscriber. It can be loaded by default from classpath, but you can prefix with classpath:, file:, or http: to load the resource from different systems. String camel.component.google-pubsub.synchronous-pull-retryable-codes Comma-separated list of additional retryable error codes for synchronous pull. By default the PubSub client library retries ABORTED, UNAVAILABLE, UNKNOWN. String

Commands:
<dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-google-pubsub-starter</artifactId> </dependency>
google-pubsub://project-id:destinationName?[options]
google-pubsub:projectId:destinationName