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 */ 016package org.opengion.fukurou.queue; 017 018import java.util.Locale; 019 020import javax.jms.QueueSession; 021 022import org.opengion.fukurou.util.StringUtil; 023 024/** 025 * キュータイプ別のクラス生成 026 * キュータイプ別のクラス生成用ファクトリークラスです。 027 * 028 * @og.rev 5.10.14.0 (2019/08/01) 新規作成 029 * 030 */ 031public final class QueueSendFactory { 032 private static final int BUFFER_MIDDLE = 200; 033 034 /** 035 * デフォルトコンストラクターをprivateにして、 036 * オブジェクトの生成をさせないようにします。 037 */ 038 private QueueSendFactory() { 039 } 040 041 /** 042 * キュー送信クラス生成 043 * 044 * 引数のキュータイプのキュー送信クラスを生成します。 045 * MQ:Apache ActiveMq、amazonMQの場合に設定します。 046 * SQS:Amazon SQSの場合に設定します。 047 * 048 * @param queueType キュータイプ 049 * @return キュータイプのキュー送信クラス 050 */ 051 public static QueueSend newQueueSend(final String queueType) { 052// QueueSend queueSend = null; 053// String setQueueType = null; 054 055 // 2. 生成クラスの文字列生成 056 final StringBuilder path = new StringBuilder(BUFFER_MIDDLE) 057 .append("org.opengion.fukurou.queue.") 058 .append("QueueSend_"); 059 060 // 1. 前処理 061 // 大文字変換 062 if (!StringUtil.isNull(queueType)) { 063// setQueueType = queueType.toUpperCase(Locale.JAPAN); 064 path.append(queueType.toUpperCase(Locale.JAPAN)); 065 } 066 067// // 2. 生成クラスの文字列生成 068// path.append("org.opengion.fukurou.queue."); 069// path.append("QueueSend_"); 070// path.append(setQueueType); 071 072 final QueueSend queueSend; 073 try { 074 // 3. 対象クラスの生成 075// queueSend = (QueueSend) Class.forName(path.toString()).newInstance(); // 警告:[deprecation] ClassのnewInstance()は推奨されません 076 queueSend = (QueueSend) Class.forName(path.toString()).getDeclaredConstructor().newInstance(); 077 } catch (final Throwable th) { 078 // キャッチしたエラー情報をスロー 079 throw new RuntimeException(th); 080 } 081 082 return queueSend; 083 } 084 085 /** 086 * バッチ処理用(mqのサンプル) 087 * バッチ処理用として、mainメソッドを実装します。 088 * 089 * @param args メイン引数配列 090 */ 091 public static void main(final String[] args) { 092 final boolean mqflug=true; // サンプルメソッドはMQ利用が標準としておきます 093 System.out.println("main start"); 094 // 送信情報の設定 095 final String url; 096// "tcp://localhost:61616"; 097// "ssl://b-92d59238-9eb5-41a9-9bc3-bb6e08a49c4a-1.mq.ap-northeast-1.amazonaws.com:61617"; 098 final QueueInfo queueInfo = new QueueInfo(); 099// QueueSend queueSend = null; 100 final QueueSend queueSend; 101 if(mqflug) { 102 // MQサーバへの接続例 103 // -DmqUserId -DmqPasswordに認証情報の設定が必要です。 104 url = "tcp://localhost:61616"; 105 queueInfo.setMqQueueName("test01"); 106 queueInfo.setMqTransacted(false); 107 queueInfo.setMqAcknowledgeMode(QueueSession.AUTO_ACKNOWLEDGE); 108 queueSend = QueueSendFactory.newQueueSend("mq"); 109 }else { 110 // SQSサーバへの接続例 111 // EC2ロール認証か、-DaccessKey -DsecretKeyに認証情報の設定が必要です。 112 url = "https://sqs.ap-northeast-1.amazonaws.com/716527209952/MyFifo.fifo"; 113 queueInfo.setSqsFifoGroupId("grp01"); 114 queueSend = QueueSendFactory.newQueueSend("sqs"); 115 } 116 117 queueInfo.setMessage("送信メッセージ"); 118 // setBatchにtrueを設定することで、バッチ実行できるように実装しています。 119 // (falseの場合はtomcatのjndi接続) 120 queueSend.setBatchFlg(true); 121 122 // ジョブ実行の場合、HybsSystemException内でnullエラー? 123 try { 124// queueSend.connect(url); 125 queueSend.connect(url,null,null); 126 queueSend.sendMessage(queueInfo); 127// }catch(final Exception e) { // 8.0.0.0 (2021/07/31) Avoid catching generic exceptions such as Exception in try-catch block 128// System.out.println(e.getMessage()); 129 } catch( final Throwable th ) { 130 System.out.println(th.getMessage()); 131 } finally { 132 queueSend.close(); 133 } 134 System.out.println("main end"); 135 } 136}