001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016
017package org.opengion.fukurou.queue;
018
019import java.util.Locale;
020
021import javax.jms.QueueSession;
022
023import org.opengion.fukurou.util.StringUtil;
024
025/**
026 * キュータイプ別のクラス生成 
027 * キュータイプ別のクラス生成用ファクトリークラスです。
028 * 
029 * @og.rev 5.10.14.0 (2019/08/01) 新規作成
030 * 
031 */
032public final class QueueSendFactory {
033        private static final int BUFFER_MIDDLE = 200;
034
035        /**
036         * デフォルトコンストラクターをprivateにして、
037         * オブジェクトの生成をさせないようにします。
038         */
039        private QueueSendFactory() {
040        }
041        
042        /**
043         * キュー送信クラス生成
044         * 
045         * 引数のキュータイプのキュー送信クラスを生成します。
046         * MQ:Apache ActiveMq、amazonMQの場合に設定します。
047         * SQS:Amazon SQSの場合に設定します。
048         * 
049         * @param queueType キュータイプ
050         * @return キュータイプのキュー送信クラス
051         */
052        public static QueueSend newQueueSend(final String queueType) {
053//              QueueSend queueSend = null;
054//              String setQueueType = null;
055
056                // 2. 生成クラスの文字列生成
057                final StringBuilder path = new StringBuilder(BUFFER_MIDDLE)
058                        .append("org.opengion.fukurou.queue.")
059                        .append("QueueSend_");
060
061                // 1. 前処理
062                // 大文字変換
063                if (!StringUtil.isNull(queueType)) {
064//                      setQueueType = queueType.toUpperCase(Locale.JAPAN);
065                        path.append(queueType.toUpperCase(Locale.JAPAN));
066                }
067
068//              // 2. 生成クラスの文字列生成
069//              path.append("org.opengion.fukurou.queue.");
070//              path.append("QueueSend_");
071//              path.append(setQueueType);
072
073                final QueueSend queueSend;
074                try {
075                        // 3. 対象クラスの生成
076//                      queueSend = (QueueSend) Class.forName(path.toString()).newInstance();                                                   // 警告:[deprecation] ClassのnewInstance()は推奨されません
077                        queueSend = (QueueSend) Class.forName(path.toString()).getDeclaredConstructor().newInstance();
078                } catch (final Throwable th) {
079                        // キャッチしたエラー情報をスロー
080                        throw new RuntimeException(th);
081                }
082
083                return queueSend;
084        }
085
086        /**
087         * バッチ処理用(mqのサンプル)
088         * バッチ処理用として、mainメソッドを実装します。
089         * 
090         * @param args メイン引数配列
091         */
092        public static void main(final String[] args) {
093                final boolean mqflug=true; // サンプルメソッドはMQ利用が標準としておきます
094                System.out.println("main start");
095                // 送信情報の設定
096                final String url;
097//                              "tcp://localhost:61616";
098//                              "ssl://b-92d59238-9eb5-41a9-9bc3-bb6e08a49c4a-1.mq.ap-northeast-1.amazonaws.com:61617";
099                final QueueInfo queueInfo = new QueueInfo();
100//              QueueSend queueSend = null;
101                final QueueSend queueSend;
102                if(mqflug) {
103                        // MQサーバへの接続例
104                        // -DmqUserId -DmqPasswordに認証情報の設定が必要です。
105                        url = "tcp://localhost:61616";
106                        queueInfo.setMqQueueName("test01");
107                        queueInfo.setMqTransacted(false);
108                        queueInfo.setMqAcknowledgeMode(QueueSession.AUTO_ACKNOWLEDGE);
109                        queueSend = QueueSendFactory.newQueueSend("mq");
110                }else {
111                        // SQSサーバへの接続例
112                        // EC2ロール認証か、-DaccessKey -DsecretKeyに認証情報の設定が必要です。
113                        url = "https://sqs.ap-northeast-1.amazonaws.com/716527209952/MyFifo.fifo";
114                        queueInfo.setSqsFifoGroupId("grp01");
115                        queueSend = QueueSendFactory.newQueueSend("sqs");
116                }
117                
118                queueInfo.setMessage("送信メッセージ");
119                // setBatchにtrueを設定することで、バッチ実行できるように実装しています。
120                // (falseの場合はtomcatのjndi接続)
121                queueSend.setBatchFlg(true);
122                
123                // ジョブ実行の場合、HybsSystemException内でnullエラー?
124                try {
125//                      queueSend.connect(url);
126                        queueSend.connect(url,null,null);
127                        queueSend.sendMessage(queueInfo);
128                }catch(final Exception e) {
129                        System.out.println(e.getMessage());
130                } finally {
131                        queueSend.close();
132                }
133                System.out.println("main end");
134        }
135}