Newer Version Available

This content describes an older version of this product. View Latest

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 Authenticate Apps with OAuth in Salesforce Help and Understanding Authentication in the Force.com REST API Developer Guide.

Let’s run an example that uses OAuth bearer token login.
  1. Get the EMP Connector project from GitHub. See Download and Build the Project.
  2. Under the /src/main/java/ folder, open the BearerTokenExample.java Java source file from the example package folder.
    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;
    16
    17/**
    18 * An example of using the EMP connector using bearer tokens
    19 *
    20 * @author hal.hildebrand
    21 */
    22public class BearerTokenExample {
    23    public static void main(String[] argv) throws Exception {
    24        if (argv.length < 2 || argv.length > 4) {
    25            System.err.println(
    26                "Usage: BearerTokenExample url token topic [replayFrom]");
    27            System.exit(1);
    28        }
    29        long replayFrom = EmpConnector.REPLAY_FROM_EARLIEST;
    30        if (argv.length == 4) {
    31            replayFrom = Long.parseLong(argv[3]);
    32        }
    33
    34        BayeuxParameters params = new BayeuxParameters() {
    35
    36            @Override
    37            public String bearerToken() {
    38                return argv[1];
    39            }
    40
    41            @Override
    42            public URL host() {
    43                try {
    44                    return new URL(argv[0]);
    45                } catch (MalformedURLException e) {
    46                    throw new IllegalArgumentException(
    47                        String.format("Unable to create url: %s", argv[0]), e);
    48                }
    49            }
    50        };
    51
    52        Consumer<Map<String, Object>> consumer = event -> System.out.println(
    53                String.format("Received:\n%s", event));
    54        EmpConnector connector = new EmpConnector(params);
    55
    56        connector.start().get(5, TimeUnit.SECONDS);
    57
    58        TopicSubscription subscription = connector.subscribe(argv[2], 
    59            replayFrom, consumer).get(5, TimeUnit.SECONDS);
    60
    61        System.out.println(String.format("Subscribed: %s", subscription));
    62    }
    63}
  3. 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).
    topic /u/notifications/ExampleUserChannel
    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.
  4. 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.