Newer Version Available
Run a Java Client with OAuth Bearer Token Login
Run a Java client that uses EMP Connector to subscribe to the channel with OAuth
authentication.
Prerequisites
Obtain an OAuth bearer access token for your Salesforce user. You supply this access token in the connector example.See Set Up Authorization with OAuth 2.0. Also see Authorize Apps with OAuth in Salesforce Help.
Let’s run an example that uses OAuth bearer token
login.
- Get the EMP Connector project from GitHub. See Download and Build the Project.
-
In the
/src/main/java/com/salesforce/emp/connector/example
folder, open the BearerTokenExample.java Java source
file.
1/* 2 * Copyright (c) 2016, salesforce.com, inc. All rights reserved. Licensed under the BSD 3-Clause license. For full 3 * license text, see LICENSE.TXT file in the repo root or https://opensource.org/licenses/BSD-3-Clause 4 */ 5package com.salesforce.emp.connector.example; 6 7import java.net.MalformedURLException; 8import java.net.URL; 9import java.util.Map; 10import java.util.concurrent.TimeUnit; 11import java.util.function.Consumer; 12 13import com.salesforce.emp.connector.BayeuxParameters; 14import com.salesforce.emp.connector.EmpConnector; 15import com.salesforce.emp.connector.TopicSubscription; 16import org.cometd.bayeux.Channel; 17 18/** 19 * An example of using the EMP connector using bearer tokens 20 */ 21public class BearerTokenExample { 22 public static void main(String[] argv) throws Exception { 23 if (argv.length < 2 || argv.length > 4) { 24 System.err.println("Usage: BearerTokenExample url token channel [replayFrom]"); 25 System.exit(1); 26 } 27 long replayFrom = EmpConnector.REPLAY_FROM_EARLIEST; 28 if (argv.length == 4) { 29 replayFrom = Long.parseLong(argv[3]); 30 } 31 32 BayeuxParameters params = new BayeuxParameters() { 33 34 @Override 35 public String bearerToken() { 36 return argv[1]; 37 } 38 39 @Override 40 public URL host() { 41 try { 42 return new URL(argv[0]); 43 } catch (MalformedURLException e) { 44 throw new IllegalArgumentException(String.format( 45 "Unable to create url: %s", argv[0]), e); 46 } 47 } 48 }; 49 50 Consumer<Map<String, Object>> consumer = event -> System.out.println( 51 String.format("Received:\n%s", event)); 52 EmpConnector connector = new EmpConnector(params); 53 54 connector.addListener(Channel.META_CONNECT, new LoggingListener(true, true)) 55 .addListener(Channel.META_DISCONNECT, new LoggingListener(true, true)) 56 .addListener(Channel.META_HANDSHAKE, new LoggingListener(true, true)); 57 58 connector.start().get(5, TimeUnit.SECONDS); 59 60 TopicSubscription subscription = connector.subscribe( 61 argv[2], replayFrom, consumer).get(5, TimeUnit.SECONDS); 62 63 System.out.println(String.format("Subscribed: %s", subscription)); 64 } 65} -
Run the BearerTokenExample 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). channel /u/notifications/ExampleUserChannel 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.
-
When you run this client app and generate notifications using the REST
resource, the output will look something like:
1Subscribed: Subscription [/u/notifications/ExampleUserChannel:-2] 2Received: 3{payload=Broadcast message to all subscribers, event={createdDate=2016-12-13T00:57:36.020Z, replayId=1}} 4Received: 5{payload=Another message, event={createdDate=2016-12-13T00:58:16.591Z, replayId=2}}
In the next step, you learn how to generate notifications using REST.