Newer Version Available
Subscribe to a Channel and Receive Event Notifications
Use EMP Connector to subscribe to the channel of the Low_Ink__e custom platform event
that you defined earlier.
-
In the
/src/main/java/com/salesforce/emp/connector/example
folder, open the LoginExample.java source
file.
1/* 2 * Copyright (c) 2016, salesforce.com, inc. 3 * All rights reserved. 4 * Licensed under the BSD 3-Clause license. 5 * For full license text, see LICENSE.TXT file in the repo root or https://opensource.org/licenses/BSD-3-Clause 6 */ 7package com.salesforce.emp.connector.example; 8 9import static com.salesforce.emp.connector.LoginHelper.login; 10 11import java.net.URL; 12import java.util.Map; 13import java.util.concurrent.TimeUnit; 14import java.util.function.Consumer; 15 16import com.salesforce.emp.connector.BayeuxParameters; 17import com.salesforce.emp.connector.EmpConnector; 18import com.salesforce.emp.connector.LoginHelper; 19import com.salesforce.emp.connector.TopicSubscription; 20 21/** 22 * An example of using the EMP connector using login credentials 23 */ 24public class LoginExample { 25 public static void main(String[] argv) throws Exception { 26 if (argv.length < 3 || argv.length > 4) { 27 System.err.println( 28 "Usage: LoginExample username password channel [replayFrom]"); 29 System.exit(1); 30 } 31 long replayFrom = EmpConnector.REPLAY_FROM_EARLIEST; 32 if (argv.length == 4) { 33 replayFrom = Long.parseLong(argv[3]); 34 } 35 36 BearerTokenProvider tokenProvider = new BearerTokenProvider(() -> { 37 try { 38 return login(argv[0], argv[1]); 39 } catch (Exception e) { 40 e.printStackTrace(System.err); 41 System.exit(1); 42 throw new RuntimeException(e); 43 } 44 }); 45 46 BayeuxParameters params = tokenProvider.login(); 47 48 Consumer<Map<String, Object>> consumer = event -> 49 System.out.println(String.format("Received:\n%s", event)); 50 51 EmpConnector connector = new EmpConnector(params); 52 53 connector.setBearerTokenProvider(tokenProvider); 54 55 connector.start().get(5, TimeUnit.SECONDS); 56 57 TopicSubscription subscription = connector.subscribe( 58 argv[2], replayFrom, consumer).get(5, TimeUnit.SECONDS); 59 60 System.out.println(String.format("Subscribed: %s", subscription)); 61 } 62} -
Run the LoginExample class and provide
arguments.
- In Package Explorer, navigate to the LoginExample.java file. Right-click the file, and select .
-
On the Arguments tab, add values for the following arguments, separated
by a space.
Argument Value username Your Salesforce username password Your Salesforce password channel The channel name for the event: /event/Low_Ink__e. - Click Run.
The sample is now subscribed to the event channel and is listening to event notifications. As soon as an event notification is generated and received, the tool prints it to the console.
The sample fetches the earliest saved events within the past 24 hours. Optionally, to receive different events, you can include a replay ID as the last argument. Valid values are:- –1—Get all new events sent after subscription.
- –2—Get all new events sent after subscription and all past events within the past 24 hours.
- Specific number—Get all events that occurred after the event with the specified replay ID.
-
To generate an event message for the custom platform event, publish an event
message by running Apex in the Developer Console.
- In Salesforce Classic, select .
-
In Lightning Experience, click the quick access menu (
), and select Developer Console.
- In the Developer Console, select .
-
In the new window, replace any code with this Apex snippet, which
publishes the platform event.
1// Create event instance. 2Low_Ink__e event = new Low_Ink__e(Printer_Model__c='XZO-5', Serial_Number__c='12345', 3 Ink_Percentage__c=0.2); 4 5// Publish event. 6Database.SaveResult sr = EventBus.publish(event); 7 8// Inspect publishing result for each event 9if (sr.isSuccess()) { 10 System.debug('Successfully published event.'); 11} else { 12 for(Database.Error err : sr.getErrors()) { 13 System.debug('Error returned: ' + 14 err.getStatusCode() + 15 ' - ' + 16 err.getMessage()); 17 } 18} - Click Execute. After the platform event is published, EMP Connector receives an event notification, which is printed in the console. The output looks similar to the following.