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 the createEventMessage and createEventMessages methods to include the namespace prefix in the field names.
  1. In a Terminal window, navigate to the top-level java folder.

  2. To run the Subscribe RPC example, enter: ./run.sh genericpubsub.Subscribe

  3. The Subscribe example prints out logging info on the terminal, similar to:

    12024-04-09 11:56:35,443 [main] java.lang.Class - Using grpcHost api.pubsub.salesforce.com and grpcPort 7443
    22024-04-09 11:56:36,610 [main] utility.APISessionCredentials - Client Trace Id for current request:
    3161d4754-cd94-446e-8c41-77626805dd7c
    42024-04-09 11:56:37,126 [main] java.lang.Class - Subscription started for topic:
    5/event/Order_Event__e.
    62024-04-09 11:56:37,128 [main] utility.APISessionCredentials - Client Trace Id for current request:
    7aeecdf2c-9522-4d1d-a4ba-359f137cf53e
    82024-04-09 11:56:42,140 [main] java.lang.Class - Subscription Active. Received a total of 0 events.

    Because no events were published, the count of received events is zero.

  4. 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 the java/src/main/java/utility/CommonContext.java from your pub-sub-api folder.

  5. There are several createEventMessage methods and one createEventMessages method that build the event messages. This method is used in Publish.java.

    1// Used by Publish.java
    2public GenericRecord createEventMessage(Schema schema)

    These methods are used in PublishStream.java. In this quick start, we use PublishStream.java with the default argument value of SINGLE_PUBLISH_REQUEST of true, so the createEventMessages method is used.

    1// Used by PublishStream.java when SINGLE_PUBLISH_REQUEST is false
    2public GenericRecord createEventMessage(Schema schema, final int counter)
    3
    4// Used by PublishStream.java when SINGLE_PUBLISH_REQUEST is true
    5public List<GenericRecord> createEventMessages(Schema schema, final int numEvents)

    Provide a valid CreatedById value for the published event message in this method. Replace the <User_Id> placeholder value of the CreatedById 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.

    1public List<GenericRecord> createEventMessages(Schema schema, final int numEvents) {
    2
    3     String[] orderNumbers = {"99","100","101","102","103"};
    4     String[] cities = {"Los Angeles", "New York", "San Francisco", "San Jose", "Boston"};
    5     Double[] amounts = {35.0, 20.0, 2.0, 123.0, 180.0};
    6
    7     // Update CreatedById with the appropriate User Id from your org.
    8     List<GenericRecord> events = new ArrayList<>();
    9     for (int i=0; i<numEvents; i++) {
    10         events.add(new GenericRecordBuilder(schema)
    11                 .set("CreatedDate", System.currentTimeMillis())
    12                 .set("CreatedById", "<User_Id>")
    13                 .set("Order_Number__c", orderNumbers[i % 5])
    14                 .set("City__c", cities[i % 5])
    15                 .set("Amount__c", amounts[i % 5]).build());
    16     }
    17
    18     return events;
    19 }
  6. Optional. If you want to supply other event fields and values, make additional changes.

  7. To publish all events in one publish request using the createEventMessages method, in java/src/main/resources/arguments.yaml, set SINGLE_PUBLISH_REQUEST to true: SINGLE_PUBLISH_REQUEST: true

  8. In a new terminal window, navigate to the top-level java folder in your pub-sub-api folder.

  9. To build the Java client, enter: mvn clean install

  10. To run the PublishStream RPC example, enter: ./run.sh genericpubsub.PublishStream The example publishes five events as set in the arguments.yaml file’s default configuration. It prints out logging information to the terminal.

  11. Return to the terminal for the Subscribe example. The output shows the events received.

12024-04-09 11:56:51,302 [pool-3-thread-1] java.lang.Class - Received event with payload:
2{
3  "CreatedDate": 1712689010574,
4  "CreatedById": "0055f000005mc66AAA",
5  "Order_Number__c": "99",
6  "City__c": "Los Angeles",
7  "Amount__c": 35.0
8}
9with schema name: Order_Event__e
102024-04-09 11:56:51,302 [pool-3-thread-2] java.lang.Class - Received event with payload:
11{
12  "CreatedDate": 1712689010574,
13  "CreatedById": "0055f000005mc66AAA",
14  "Order_Number__c": "100",
15  "City__c": "New York",
16  "Amount__c": 20.0
17}
18with schema name: Order_Event__e
192024-04-09 11:56:51,302 [pool-3-thread-3] java.lang.Class - Received event with payload:
20{
21  "CreatedDate": 1712689010574,
22  "CreatedById": "0055f000005mc66AAA",
23  "Order_Number__c": "101",
24  "City__c": "San Francisco",
25  "Amount__c": 2.0
26}
27with schema name: Order_Event__e
282024-04-09 11:56:51,303 [pool-3-thread-2] java.lang.Class - Received event with payload:
29{
30  "CreatedDate": 1712689010574,
31  "CreatedById": "0055f000005mc66AAA",
32  "Order_Number__c": "102",
33  "City__c": "San Jose",
34  "Amount__c": 123.0
35}
36with schema name: Order_Event__e
372024-04-09 11:56:51,303 [pool-3-thread-1] java.lang.Class - Received event with payload:
38{
39  "CreatedDate": 1712689010574,
40  "CreatedById": "0055f000005mc66AAA",
41  "Order_Number__c": "103",
42  "City__c": "Boston",
43  "Amount__c": 180.0
44}
45with schema name: Order_Event__e

Learn More 

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>