ステップ 4: ユーザ名とパスワードのログインを使用してコネクタを使用する
EMP コネクタをダウンロードして構築したので、次はそれを使用して CometD に接続し、PushTopic に登録します。
ユーザ名とパスワードのログインを使用する例を実行しましょう。
-
/src/main/java/com/salesforce/emp/connector/example フォルダで、LoginExample.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.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} -
LoginExample クラスを実行し、次の引数を指定します。
- Package Explorer で、LoginExample.java ファイルに移動します。ファイルを右クリックし、 を選択します。
-
[Arguments (引数)] タブで、次の引数の値をスペースで区切って入力します。
引数 値 username ログインユーザのユーザ名 password username (ログインユーザ) のパスワード channel PushTopic のチャネル名: /topic/InvoiceStatementUpdates。 - [Run (実行)] をクリックします。
サンプルがイベントチャネルに登録され、イベント通知をリスンし始めます。イベント通知が公開されて受信されるとすぐに、ツールがその旨をコンソールに出力します。
-
ブラウザウィンドウで、請求書明細を作成または変更します。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.}}