Skip to content

Support lib for applications relying on GCP PubSub

License

Notifications You must be signed in to change notification settings

extenda/hiiretail-gcp-pubsub-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Extenda Hii Retail GCP PubSub client

A Google Cloud Platform PubSub client implemented for JDK 21+ (Virtual threads).

Quality Gate Status Coverage Code Smells

🔩 Configuration

The library supports changing these settings, via environmental variables:

  • SERVICE_PROJECT_ID

    The value of your GCP project id. Using test-project if not set.

  • PUBSUB_CLOSE_TIMEOUT_SECONDS

    The timeout in seconds before forcefully closing the client. Default 10.

  • PUBSUB_WAIT_PUBLISH_SECONDS

    The timeout in seconds to wait for publish result before throwing an error. Default 30.

  • PUBSUB_EMULATOR_HOST

    The host url to the PubSub emulator. Can also be set as system property, e.g. in tests.

📔 Usage

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>${version.google-cloud}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.retailsvc</groupId>
    <artifactId>hiiretail-gcp-pubsub-java</artifactId>
    <version>x.y.z</version>
  </dependency>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-pubsub</artifactId>
    <version>...</version>
  </dependency>
</dependencies>

The library uses SLF4J as logging API, so make sure you have log4j[2] or logback or other compatible implementation on the classpath.

To use the PubSub client, you first need to instantiate a PubSubClientFactory.
The factory can take a custom ObjectToBytesMapper as parameter. The mapper is used to convert the objects being sent, to suitable data to publish. If you want to use Jackson to support JSON you will also need to add jackson-databind if not already present:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>...</version>
</dependency>

Cached clients

Any client that is created via the factory is also cached internally by its topic.

📜 Usage

import com.retailsvc.gcp.pubsub.ObjectToBytesMapper;
import com.retailsvc.gcp.pubsub.PooledPublisherFactory;

ObjectMapper jsonMapper = new ObjectMapper();
ObjectToBytesMapper objectMapper = v -> ByteBuffer.wrap(jsonMapper.writeValueAsBytes(v));

PubSubClientFactory factory = new PubSubClientFactory(objectMapper,
  PooledPublisherFactory.defaultPool());

PubSubClient pubSubClient = factory.create("example.entities.v1");

Object payload = ...
/*
 'payload' could be any of the supported types:
  - String, such as "{ .. }", "my text" etc.
  - ByteBuffer
  - InputStream
  - Any Jackson serializable type such as Record class, List etc.
*/
Map<String, String> attributes = Map.of("Tenant-Id", "...", "key", "value");
pubSubClient.publish(payload, attributes);

🔧 Local development environment

  • JDK 21+
  • Python / pre-commit

Building

$ mvn clean package
$ mvn verify

Install and run the pre-commit hooks before you submit code:

$ pre-commit install -t pre-commit -t commit-msg

💁 Contribution

Contributions to the project are welcome, but must adhere to a few guidelines:

  • Conventional commits should be followed
  • Install and use a editorconfig plugin to use the project supplied settings

About

Support lib for applications relying on GCP PubSub

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages