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 thecreateEventMessageandcreateEventMessagesmethods to include the namespace prefix in the field names.
-
In a Terminal window, navigate to the top-level
javafolder. -
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
CreatedByIdfield 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.javafrom yourpub-sub-apifolder. -
There are several
createEventMessagemethods and onecreateEventMessagesmethod that build the event messages. This method is used inPublish.java.These methods are used in
PublishStream.java. In this quick start, we usePublishStream.javawith the default argument value ofSINGLE_PUBLISH_REQUESToftrue, so thecreateEventMessagesmethod is used.Provide a valid
CreatedByIdvalue for the published event message in this method. Replace the<User_Id>placeholder value of theCreatedByIdfield 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
createEventMessagesmethod, injava/src/main/resources/arguments.yaml, setSINGLE_PUBLISH_REQUESTto true:SINGLE_PUBLISH_REQUEST: true -
In a new terminal window, navigate to the top-level
javafolder in yourpub-sub-apifolder. -
To build the Java client, enter:
mvn clean install -
To run the
PublishStreamRPC example, enter:./run.sh genericpubsub.PublishStreamThe example publishes five events as set in thearguments.yamlfile’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>