Newer Version Available
Step 4: Use the Connector with Username and Password Login
Now that you’ve downloaded and built the EMP connector, use its functions to connect
to CometD and subscribe to the PushTopic.
Let’s run an example that uses username and password login.
-
Under the /src/main/java/
folder, open the LoginExample.java Java source file
from the example package folder.
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.util.Map; 12import java.util.concurrent.TimeUnit; 13import java.util.function.Consumer; 14 15import com.salesforce.emp.connector.BayeuxParameters; 16import com.salesforce.emp.connector.EmpConnector; 17import com.salesforce.emp.connector.TopicSubscription; 18 19/** 20 * An example of using the EMP connector using login credentials 21 * 22 * @author hal.hildebrand 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("Usage: LoginExample username password topic [replayFrom]"); 28 System.exit(1); 29 } 30 long replayFrom = EmpConnector.REPLAY_FROM_EARLIEST; 31 if (argv.length == 4) { 32 replayFrom = Long.parseLong(argv[3]); 33 } 34 35 BayeuxParameters params; 36 try { 37 params = login(argv[0], argv[1]); 38 } catch (Exception e) { 39 e.printStackTrace(System.err); 40 System.exit(1); 41 throw e; 42 } 43 44 Consumer<Map<String, Object>> consumer = event -> System.out.println( 45 String.format("Received:\n%s", event)); 46 EmpConnector connector = new EmpConnector(params); 47 48 connector.start().get(5, TimeUnit.SECONDS); 49 50 TopicSubscription subscription = connector.subscribe(argv[2], 51 replayFrom, consumer).get(5, TimeUnit.SECONDS); 52 53 System.out.println(String.format("Subscribed: %s", subscription)); 54 } 55} -
Run the LoginExample class and provide the following argument
values.
Argument Value username Username of the logged-in user. password Password for the username (or logged-in user). topic /topic/InvoiceStatementUpdates The sample fetches the earliest saved events within the past 24 hours. Optionally, you can supply a replay ID as the last argument to receive different events. 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 last 24 hours.
- Specific number—Get all events that occurred after the event with the specified replay ID.
-
In a browser window, create or modify an invoice statement. After you create or
change data that corresponds to the query in your PushTopic, the output looks
similar to the following.
1Subscribed: Subscription [/topic/InvoiceStatementUpdates:-2] 2Received: 3{event={createdDate=2016-12-12T22:31:48.035Z, replayId=1, type=created}, sobject={Status__c=Open, Id=a070P00000pn0hyQAA, Name=INV-0001, Description__c=blah}} 4Received: 5{event={createdDate=2016-12-12T22:32:06.440Z, replayId=2, type=updated}, sobject={Status__c=Negotiating, Id=a070P00000pn0hyQAA, Name=INV-0001, Description__c=blah}} 6Received: 7{event={createdDate=2016-12-12T22:32:57.404Z, replayId=3, type=created}, sobject={Status__c=Open, Id=a070P00000pn0lfQAA, Name=INV-0002, Description__c=Laptops and accessories.}}