intra-mart Accel Platform IM-MessageHub プログラミングガイド 第3版 2021-04-01

4.4. メッセージの配信を依頼する

最後に、配信するメッセージを作成し、 IM-MessageHub に対してそのメッセージの配信を依頼します。
今回は、メッセージの作成、および、配信依頼を行うサンプルを作成します。

4.4.1. メッセージを作成する

はじめに、配信するメッセージの作成方法を説明します。
メッセージを作成する上で必要な情報は以下の通りです。
  • イベント
  • 送信元のアドレス情報
  • 宛先のアドレス情報リスト
  • 任意の属性値
これらの情報をもとに、 IM-MessageHub で配信するメッセージを作成するサンプルを実装します。
メッセージを作成する処理の実装例は以下の通りです。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    /**
     * メッセージを作成します。
     * @return メッセージ
     */
    public static Message createMessage(){
        // イベントの作成
        final Event event = new SampleMessageEvent();
        
        // 送信元アドレスの作成
        // 今回、送信元アドレスはアカウントコンテキストから取得しています。
        AccountContext context = Contexts.get(AccountContext.class);
        final Address fromAddress = new UserAddress(context.getUserCd());
        
        // 送信先アドレスの作成
        final List<Address> toAddressList = new ArrayList<Address>();

        // サンプルとしていくつかの宛先を追加する。
        toAddressList.add(new UserAddress("aoyagi")); // 青柳
        toAddressList.add(new UserAddress("ueda")); // 上田
        toAddressList.add(new UserAddress("hayashi")); // 林
        toAddressList.add(new UserAddress("maruyama")); // 円山
        
        // 属性の設定
        final Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put("url",      "http://www.intra-mart.jp/");
        attributes.put("sendDate", DateTime.now(TimeZone.getTimeZone("Asia/Tokyo")));
        
        // メッセージの作成
        final Message message = new Message(event, fromAddress, toAddressList, attributes);
        
        return message;
    }
表:実装の詳細
行番号 説明
7 イベントを定義する 」で実装したクラスを元にイベントを作成します。
12
配信を行うユーザのユーザコードを取得し、ユーザコードを元に差出人のアドレスを作成します。
今回は、ユーザコードを AccountContext から取得しています。
15, 18-21
宛先のアドレスを作成します。
今回は、サンプルユーザコードを直接指定してアドレスを作成しています。
24-26
置換処理への対応を行います。
詳細は「 置換処理の対応 」を参照してください。
29 作成した各種情報を元にメッセージ( Message )を作成します。

4.4.1.1. 置換処理の対応

テンプレートに定義されたプレースホルダを置換するためには、置換後の具体的な情報をメッセージの作成時に設定する必要があります。
今回のサンプルで、置換処理の対応を行っている箇所は以下の通りです。
1
2
3
4
5
6
7
        // 属性の設定
        final Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put("url",      "http://www.intra-mart.jp/");
        attributes.put("sendDate", DateTime.now(TimeZone.getTimeZone("Asia/Tokyo")));
        
        // メッセージの作成
        final Message message = new Message(event, fromAddress, toAddressList, attributes);
表:実装の詳細
行番号 説明
3-4
テンプレートに定義されたプレースホルダは、メッセージに指定された同名の属性に置換されます。
ここでは、メッセージの属性情報として利用される Map 形式の変数 attributes の keyvalue を以下のように設定しています。
  • key - プレースホルダ名
  • value - 置換後の具体的な情報
この処理によって、 IM-MessageHub は「 完成したテンプレートファイルのサンプル 」で作成したテンプレートファイルのプレースホルダを以下の通りに置換します。
  • ${sendDate} - メッセージの配信日付の情報に置換。
  • ${url} - メッセージに関連するURL情報に置換。

4.4.2. メッセージを配信する

次に、 IM-MessageHub へ作成したメッセージの配信を依頼する方法を説明します。
IM-MessageHub ではメッセージの配信を行うための MessageService クラスを提供しています。
  • jp.co.intra_mart.foundation.message_hub.service.MessageService
メッセージを配信する処理の実装例は以下の通りです。
(この実装を含めた、サンプルクラスの全体像は「 完成したメッセージ配信依頼クラスのサンプル 」で提示しています。)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
    /**
     * メッセージを配信します。
     * @return メッセージを正常に送信できた場合はtrueを返します。
     */
    public static boolean deliver(){
        try {
            // MessageServiceをFactoryより取得する。
            final MessageService messageService = MessageServiceFactory.getInstance().getMessageService();
            
            // メッセージを作成し、配信をメッセージサービスに依頼します。
            messageService.send(createMessage());
            
        } catch (MessageServiceException e) {
            return false;
        }
        return true;
    }
表:実装の詳細
行番号 説明
8 MessageServiceMessageServiceFactory より取得します。
11 メッセージを作成する 」で作成したメッセージを、 MessageService#send へ渡し、 IM-MessageHub へ配信依頼を行います。

4.4.3. 完成したメッセージ配信依頼クラスのサンプル

今回は、以下のようなサンプルクラスを作成します。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package sample.message_hub.send;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import jp.co.intra_mart.foundation.context.Contexts;
import jp.co.intra_mart.foundation.context.model.AccountContext;
import jp.co.intra_mart.foundation.i18n.datetime.DateTime;
import jp.co.intra_mart.foundation.message_hub.model.Address;
import jp.co.intra_mart.foundation.message_hub.model.Event;
import jp.co.intra_mart.foundation.message_hub.model.Message;
import jp.co.intra_mart.foundation.message_hub.model.UserAddress;
import jp.co.intra_mart.foundation.message_hub.service.MessageService;
import jp.co.intra_mart.foundation.message_hub.service.MessageServiceException;
import jp.co.intra_mart.foundation.message_hub.service.MessageServiceFactory;
import sample.message_hub.event.SampleMessageEvent;

/**
 * メッセージをIM-MessageHubを利用して作成から配信まで行うサンプルクラスです。
 */
public class SampleMessageSender {

    /**
     * メッセージを作成します。
     * @return メッセージ
     */
    public static Message createMessage(){
        // イベントの作成
        final Event event = new SampleMessageEvent();
        
        // 送信元アドレスの作成
        // 今回、送信元アドレスはアカウントコンテキストから取得しています。
        AccountContext context = Contexts.get(AccountContext.class);
        final Address fromAddress = new UserAddress(context.getUserCd());
        
        // 送信先アドレスの作成
        final List<Address> toAddressList = new ArrayList<Address>();

        // サンプルとしていくつかの宛先を追加する。
        toAddressList.add(new UserAddress("aoyagi")); // 青柳
        toAddressList.add(new UserAddress("ueda")); // 上田
        toAddressList.add(new UserAddress("hayashi")); // 林
        toAddressList.add(new UserAddress("maruyama")); // 円山
        
        // 属性の設定
        final Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put("url",      "http://www.intra-mart.jp/");
        attributes.put("sendDate", DateTime.now(TimeZone.getTimeZone("Asia/Tokyo")));
        
        // メッセージの作成
        final Message message = new Message(event, fromAddress, toAddressList, attributes);
        
        return message;
    }
    
    /**
     * メッセージを配信します。
     * @return メッセージを正常に送信できた場合はtrueを返します。
     */
    public static boolean deliver(){
        try {
            // MessageServiceをFactoryより取得する。
            final MessageService messageService = MessageServiceFactory.getInstance().getMessageService();
            
            // メッセージを作成し、配信をメッセージサービスに依頼します。
            messageService.send(createMessage());
            
        } catch (MessageServiceException e) {
            return false;
        }
        return true;
    }
}