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:
2024-04-09 11:56:35,443 [main] java.lang.Class - Using grpcHost api.pubsub.salesforce.com and grpcPort 7443 2024-04-09 11:56:36,610 [main] utility.APISessionCredentials - Client Trace Id for current request: 161d4754-cd94-446e-8c41-77626805dd7c 2024-04-09 11:56:37,126 [main] java.lang.Class - Subscription started for topic: /event/Order_Event__e. 2024-04-09 11:56:37,128 [main] utility.APISessionCredentials - Client Trace Id for current request: aeecdf2c-9522-4d1d-a4ba-359f137cf53e 2024-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.
-
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.// Used by Publish.java public GenericRecord createEventMessage(Schema schema)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.// Used by PublishStream.java when SINGLE_PUBLISH_REQUEST is false public GenericRecord createEventMessage(Schema schema, final int counter) // Used by PublishStream.java when SINGLE_PUBLISH_REQUEST is true public List<GenericRecord> createEventMessages(Schema schema, final int numEvents)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.public List<GenericRecord> createEventMessages(Schema schema, final int numEvents) { String[] orderNumbers = {"99","100","101","102","103"}; String[] cities = {"Los Angeles", "New York", "San Francisco", "San Jose", "Boston"}; Double[] amounts = {35.0, 20.0, 2.0, 123.0, 180.0}; // Update CreatedById with the appropriate User Id from your org. List<GenericRecord> events = new ArrayList<>(); for (int i=0; i<numEvents; i++) { events.add(new GenericRecordBuilder(schema) .set("CreatedDate", System.currentTimeMillis()) .set("CreatedById", "<User_Id>") .set("Order_Number__c", orderNumbers[i % 5]) .set("City__c", cities[i % 5]) .set("Amount__c", amounts[i % 5]).build()); } return events; } -
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.
2024-04-09 11:56:51,302 [pool-3-thread-1] java.lang.Class - Received event with payload:
{
"CreatedDate": 1712689010574,
"CreatedById": "0055f000005mc66AAA",
"Order_Number__c": "99",
"City__c": "Los Angeles",
"Amount__c": 35.0
}
with schema name: Order_Event__e
2024-04-09 11:56:51,302 [pool-3-thread-2] java.lang.Class - Received event with payload:
{
"CreatedDate": 1712689010574,
"CreatedById": "0055f000005mc66AAA",
"Order_Number__c": "100",
"City__c": "New York",
"Amount__c": 20.0
}
with schema name: Order_Event__e
2024-04-09 11:56:51,302 [pool-3-thread-3] java.lang.Class - Received event with payload:
{
"CreatedDate": 1712689010574,
"CreatedById": "0055f000005mc66AAA",
"Order_Number__c": "101",
"City__c": "San Francisco",
"Amount__c": 2.0
}
with schema name: Order_Event__e
2024-04-09 11:56:51,303 [pool-3-thread-2] java.lang.Class - Received event with payload:
{
"CreatedDate": 1712689010574,
"CreatedById": "0055f000005mc66AAA",
"Order_Number__c": "102",
"City__c": "San Jose",
"Amount__c": 123.0
}
with schema name: Order_Event__e
2024-04-09 11:56:51,303 [pool-3-thread-1] java.lang.Class - Received event with payload:
{
"CreatedDate": 1712689010574,
"CreatedById": "0055f000005mc66AAA",
"Order_Number__c": "103",
"City__c": "Boston",
"Amount__c": 180.0
}
with 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>