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 StringBuilder path = new StringBuilder(BUFFER_MIDDLE); 057 058 // 1. 前処理 059 // 大文字変換 060 if (!StringUtil.isNull(queueType)) { 061 setQueueType = queueType.toUpperCase(Locale.JAPAN); 062 } 063 064 // 2. 生成クラスの文字列生成 065 path.append("org.opengion.fukurou.queue."); 066 path.append("QueueSend_"); 067 path.append(setQueueType); 068 069 try { 070 // 3. 対象クラスの生成 071 // queueSend = (QueueSend) Class.forName(path.toString()).newInstance(); // 5.10.14.1 (2019/08/09) v7に合わせておく 072 queueSend = (QueueSend) Class.forName(path.toString()).getDeclaredConstructor().newInstance(); 073 } catch (final Throwable th) { 074 // キャッチしたエラー情報をスロー 075 throw new RuntimeException(th); 076 } 077 078 return queueSend; 079 } 080 081 /** 082 * バッチ処理用(mqのサンプル) 083 * バッチ処理用として、mainメソッドを実装します。 084 * 085 * @param args 086 */ 087 public static void main(String[] args) { 088 boolean mqflug=true; // サンプルメソッドはMQ利用が標準としておきます 089 System.out.println("main start"); 090 // 送信情報の設定 091 String url = null; 092// "tcp://localhost:61616"; 093// "ssl://b-92d59238-9eb5-41a9-9bc3-bb6e08a49c4a-1.mq.ap-northeast-1.amazonaws.com:61617"; 094 QueueInfo queueInfo = new QueueInfo(); 095 QueueSend queueSend = null; 096 if(mqflug) { 097 // MQサーバへの接続例 098 // -DmqUserId -DmqPasswordに認証情報の設定が必要です。 099 url = "tcp://localhost:61616"; 100 queueInfo.setMqQueueName("test01"); 101 queueInfo.setMqTransacted(false); 102 queueInfo.setMqAcknowledgeMode(QueueSession.AUTO_ACKNOWLEDGE); 103 queueSend = QueueSendFactory.newQueueSend("mq"); 104 }else { 105 // SQSサーバへの接続例 106 // EC2ロール認証か、-DaccessKey -DsecretKeyに認証情報の設定が必要です。 107 url = "https://sqs.ap-northeast-1.amazonaws.com/716527209952/MyFifo.fifo"; 108 queueInfo.setSqsFifoGroupId("grp01"); 109 queueSend = QueueSendFactory.newQueueSend("sqs"); 110 } 111 112 queueInfo.setMessage("送信メッセージ"); 113 // setBatchにtrueを設定することで、バッチ実行できるように実装しています。 114 // (falseの場合はtomcatのjndi接続) 115 queueSend.setBatchFlg(true); 116 117 // ジョブ実行の場合、HybsSystemException内でnullエラー? 118 try { 119 queueSend.connect(url,null,null); 120 queueSend.sendMessage(queueInfo); 121 }catch(Exception e) { 122 System.out.println(e.getMessage()); 123 } finally { 124 queueSend.close(); 125 } 126 System.out.println("main end"); 127 } 128 129 130}