Step 4: Publish and Subscribe to Platform Events
Use the Java examples for the Subscribe
RPC and PublishStream
RPC to try out publishing and receiving events.
Prerequisites
- If you’re using a Developer Edition org and you set up a namespace prefix, modify the Java code that builds the event message to prepend the namespace prefix to the platform event fields. In
java/src/main/java/utility/CommonContext.java
, modify thecreateEventMessage
andcreateEventMessages
methods to include the namespace prefix in the field names.
-
In a Terminal window, navigate to the top-level
java
folder. -
To run the Subscribe RPC example, enter:
./run.sh genericpubsub.Subscribe
-
The Subscribe example prints out logging info on the terminal, similar to:
Because no events were published, the count of received events is zero.
-
Before you can publish events, modify the code to specify the event fields and values to publish. In this quick start, you modify the code to supply the user ID for the
CreatedById
field in the event message. You use the sample values for the other event fields. In a text editor or an IDE, such as Visual Studio Code, open thejava/src/main/java/utility/CommonContext.java
from yourpub-sub-api
folder. -
There are several
createEventMessage
methods and onecreateEventMessages
method that build the event messages. This method is used inPublish.java
.These methods are used in
PublishStream.java
. In this quick start, we usePublishStream.java
with the default argument value ofSINGLE_PUBLISH_REQUEST
oftrue
, so thecreateEventMessages
method is used.Provide a valid
CreatedById
value for the published event message in this method. Replace the<User_Id>
placeholder value of theCreatedById
field with a valid user ID in your Salesforce org. To get the user ID, see Find the Salesforce ID for a User or Profile. -
Optional. If you want to supply other event fields and values, make additional changes.
-
To publish all events in one publish request using the
createEventMessages
method, injava/src/main/resources/arguments.yaml
, setSINGLE_PUBLISH_REQUEST
to true:SINGLE_PUBLISH_REQUEST: true
-
In a new terminal window, navigate to the top-level
java
folder in yourpub-sub-api
folder. -
To build the Java client, enter:
mvn clean install
-
To run the
PublishStream
RPC example, enter:./run.sh genericpubsub.PublishStream
The example publishes five events as set in thearguments.yaml
file’s default configuration. It prints out logging information to the terminal. -
Return to the terminal for the Subscribe example. The output shows the events received.
To learn more about the example code, see Implementation and Limitations in the Readme page of the pub-sub-api
GitHub repository.
The client code for the Pub/Sub API RPC methods is in the genericpubsub folder. To check out the implementation for each RPC method, open the corresponding Java file in the genericpubsub
folder. For the PublishStream
example, open PublishStream.java. And for the Subscribe
example, open Subscribe.java.
For more information about the PublishStream
and Subscribe
RPC methods, see PublishStream RPC Method and Subscribe RPC Method.
To run other RPC methods, use this command format: ./run.sh <package_name>.<example_class_name>