Skip to main content

Getting Started with Momento Topics in Java

If you need to get going quickly with Java and Momento Topics, this page contains the basic API calls you'll need. Check the Java SDK examples for complete, working code samples.

Install the Momento SDK

The Momento SDK is available on Maven Central: `


Visit Maven Central to find the latest available version of the SDK.

Install the client library in an existing Java project:





Get your Momento API key

You'll need a Momento API key. You can get one from the Momento Web Console. Once you have your token, store it in an environment variable so that the Momento client can consume it:

export MOMENTO_API_KEY=<your api key here>

Note: it is best practice to put the API key into something like AWS Secret Manager or GCP Secret Manager instead of an environment variable for enhanced security, but we are using one here for demo purposes.

Set up a TopicClient

This code creates the TopicClient that you will use to interact with your pub/sub topic.

Publish a message to a topic

This is an example of publishing a message to a topic called "topic", then catching the return value to check if the publish was successful.

Subscribe to a topic

This is an example of subscribing to a topic called "topic". When messages are published to this topic, the code here receives and prints them asynchronously.

Running the code

You can find complete, working examples in the Java SDK GitHub repo examples directory.


Beyond these basic API calls check out the API reference page for more information on the full assortment of Momento API calls.