この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

ステップ 4: ユーザ名とパスワードのログインを使用してコネクタを使用する

EMP コネクタをダウンロードして構築したので、次はその機能を使用して CometD に接続し、PushTopic に登録します。
ユーザ名とパスワードのログインを使用する例を実行しましょう。
  1. /src/main/java/ フォルダで、例のパッケージフォルダから LoginExample.java Java ソースファイルを開きます。
    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 * @since 202
    24 */
    25public class LoginExample {
    26    public static void main(String[] argv) throws Exception {
    27        if (argv.length < 3 || argv.length > 4) {
    28            System.err.println("Usage: LoginExample username password topic [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        BayeuxParameters params;
    37        try {
    38            params = login(argv[0], argv[1]);
    39        } catch (Exception e) {
    40            e.printStackTrace(System.err);
    41            System.exit(1);
    42            throw e;
    43        } 
    44
    45        Consumer<Map<String, Object>> consumer = event -> System.out.println(
    46                String.format("Received:\n%s", event));
    47        EmpConnector connector = new EmpConnector(params);
    48
    49        connector.start().get(5, TimeUnit.SECONDS);
    50
    51        TopicSubscription subscription = connector.subscribe(argv[2], 
    52            replayFrom, consumer).get(5, TimeUnit.SECONDS);
    53
    54        System.out.println(String.format("Subscribed: %s", subscription));
    55    }
    56}
  2. LoginExample クラスを実行し、次の引数の値を指定します。
    引数
    username ログインユーザのユーザ名。
    password username (ログインユーザ) のパスワード。
    topic /topic/InvoiceStatementUpdates
    このサンプルでは、過去 24 時間以内で最も早く保存されたイベントを取得します。必要に応じて、最後の引数として再生 ID を指定して異なるイベントを取得できます。有効な値は、次のとおりです。
    • -1 — 登録後に送信されたすべての新規イベントを取得します。
    • -2 — 登録後に送信されたすべての新規イベントおよび過去 24 時間以内のすべての過去のイベントを取得します。
    • 特定の番号 - 指定された再生 ID を持つイベントより後に発生したすべてのイベントを取得します。
  3. ブラウザウィンドウで、請求書明細を作成または変更します。PushTopic 内のクエリに対応するデータを作成または変更すると、出力は次のようになります。
    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.}}
一般的に、本番環境でコードを実行するときには、他のユーザのユーザ名とパスワードを処理しないようにします。本番環境では、ログインを OAuth に委任してください。次の手順では、OAuth を使用して ストリーミング API に接続します。