java結(jié)合WebSphere MQ實(shí)現(xiàn)接收隊(duì)列文件功能
首先我們先來(lái)簡(jiǎn)單介紹下websphere mq以及安裝使用簡(jiǎn)介
websphere mq : 用于傳輸信息 具有跨平臺(tái)的功能。
1 安裝websphere mq 并啟動(dòng)
2 websphere mq 建立 queue Manager (如:MQSI_SAMPLE_QM)
3 建立queue 類型選擇 Local類型 的 (如lq )
4 建立channels 類型選擇Server Connection (如BridgeChannel)
接下來(lái),我們來(lái)看實(shí)例代碼:
MQFileReceiver.java
package com.mq.dpca.file;
import java.io.File;
import java.io.FileOutputStream;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.MQConstants;
import com.mq.dpca.msg.MQConfig;
import com.mq.dpca.util.ReadCmdLine;
import com.mq.dpca.util.RenameUtil;
/**
*
* MQ分組接收文件功能
* 主動(dòng)輪詢
*/
public class MQFileReceiver {
private MQQueueManager qmgr; // 連接到隊(duì)列管理器
private MQQueue inQueue; // 傳輸隊(duì)列
private String queueName = ""; // 隊(duì)列名稱
private String host = ""; //
private int port = 1414; // 偵聽器的端口號(hào)
private String channel = ""; // 通道名稱
private String qmgrName = ""; // 隊(duì)列管理器
private MQMessage inMsg; // 創(chuàng)建消息緩沖
private MQGetMessageOptions gmo; // 設(shè)置獲取消息選項(xiàng)
private static String fileName = null; // 接收隊(duì)列上的消息并存入文件
private int ccsid = 0;
private static String file_dir = null;
/**
* 程序的入口
*
* @param args
*/
public static void main(String args[]) {
MQFileReceiver mfs = new MQFileReceiver();
//初始化連接
mfs.initproperty();
//接收文件
mfs.runGoupReceiver();
//獲取shell腳本名
// String shellname = MQConfig.getValueByKey(fileName);
// if(shellname!=null&&!"".equals(shellname)){
// //調(diào)用shell
// ReadCmdLine.callShell(shellname);
// }else{
// System.out.println("have no shell name,Only receive files.");
// }
}
public void runGoupReceiver() {
try {
init();
getGroupMessages();
qmgr.commit();
System.out.println("\n Messages successfully Receive ");
} catch (MQException mqe) {
mqe.printStackTrace();
try {
System.out.println("\n Backing out Transaction ");
qmgr.backout();
System.exit(2);
} catch (Exception e) {
e.printStackTrace();
System.exit(2);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(2);
}
}
/**
* 初始化服務(wù)器連接信息
*
* @throws Exception
*/
private void init() throws Exception {
/* 為客戶機(jī)連接設(shè)置MQEnvironment屬性 */
MQEnvironment.hostname = host;
MQEnvironment.channel = channel;
MQEnvironment.port = port;
/* 連接到隊(duì)列管理器 */
qmgr = new MQQueueManager(qmgrName);
/* 設(shè)置隊(duì)列打開選項(xiàng)以輸 */
int opnOptn = MQConstants.MQOO_INPUT_AS_Q_DEF
| MQConstants.MQOO_FAIL_IF_QUIESCING;
/* 打開隊(duì)列以輸 */
inQueue = qmgr.accessQueue(queueName, opnOptn, null, null, null);
}
/**
* 接受文件的主函數(shù)
*
* @throws Exception
*/
public void getGroupMessages() {
/* 設(shè)置獲取消息選項(xiàng) */
gmo = new MQGetMessageOptions();
gmo.options = MQConstants.MQGMO_FAIL_IF_QUIESCING;
gmo.options = gmo.options + MQConstants.MQGMO_SYNCPOINT;
/* 等待消息 */
gmo.options = gmo.options + MQConstants.MQGMO_WAIT;
/* 設(shè)置等待時(shí)間限制 */
gmo.waitInterval = 5000;
/* 只獲取消息 */
gmo.options = gmo.options + MQConstants.MQGMO_ALL_MSGS_AVAILABLE;
/* 以輯順序獲取消息 */
gmo.options = gmo.options + MQConstants.MQGMO_LOGICAL_ORDER;
gmo.matchOptions = MQConstants.MQMO_MATCH_GROUP_ID;
/* 創(chuàng)建消息緩沖 */
inMsg = new MQMessage();
try {
FileOutputStream fos = null;
/* 處理組消息 */
while (true) {
try {
inQueue.get(inMsg, gmo);
if (fos == null) {
try {
fileName = inMsg.getStringProperty("fileName");
String fileName_full = null;
fileName_full = file_dir + RenameUtil.rename(fileName);
fos = new FileOutputStream(new File(fileName_full));
int msgLength = inMsg.getMessageLength();
byte[] buffer = new byte[msgLength];
inMsg.readFully(buffer);
fos.write(buffer, 0, msgLength);
/* 查看是否是最后消息標(biāo)識(shí) */
char x = gmo.groupStatus;
if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) {
System.out.println("Last Msg in Group");
break;
}
inMsg.clearMessage();
} catch (Exception e) {
System.out
.println("Receiver the message without property,do nothing!");
inMsg.clearMessage();
}
} else {
int msgLength = inMsg.getMessageLength();
byte[] buffer = new byte[msgLength];
inMsg.readFully(buffer);
fos.write(buffer, 0, msgLength);
/* 查看是否是最后消息標(biāo)識(shí) */
char x = gmo.groupStatus;
if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) {
System.out.println("Last Msg in Group");
break;
}
inMsg.clearMessage();
}
} catch (Exception e) {
char x = gmo.groupStatus;
if (x == MQConstants.MQGS_LAST_MSG_IN_GROUP) {
System.out.println("Last Msg in Group");
}
break;
}
}
if (fos != null)
fos.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void initproperty() {
MQConfig config = new MQConfig().getInstance();
if (config.getMQ_MANAGER() != null) {
qmgrName = config.getMQ_MANAGER();
queueName = config.getMQ_QUEUE_NAME();
channel = config.getMQ_CHANNEL();
host = config.getMQ_HOST_NAME();
port = Integer.valueOf(config.getMQ_PROT());
ccsid = Integer.valueOf(config.getMQ_CCSID());
file_dir = config.getFILE_DIR();
}
}
}
- java利用delayedQueue實(shí)現(xiàn)本地的延遲隊(duì)列
- Java 隊(duì)列實(shí)現(xiàn)原理及簡(jiǎn)單實(shí)現(xiàn)代碼
- 剖析Java中阻塞隊(duì)列的實(shí)現(xiàn)原理及應(yīng)用場(chǎng)景
- 詳解Java消息隊(duì)列-Spring整合ActiveMq
- 解析Java中的隊(duì)列和用LinkedList集合模擬隊(duì)列的方法
- java使用數(shù)組和鏈表實(shí)現(xiàn)隊(duì)列示例
- 一個(gè)簡(jiǎn)易的Java多頁(yè)面隊(duì)列爬蟲程序
- Java 阻塞隊(duì)列詳解及簡(jiǎn)單使用
- Java消息隊(duì)列的簡(jiǎn)單實(shí)現(xiàn)代碼
- Java并發(fā)編程之阻塞隊(duì)列詳解
- java多線程消息隊(duì)列的實(shí)現(xiàn)代碼
- Java延遲隊(duì)列原理與用法實(shí)例詳解
相關(guān)文章
MyBatis-Plus不使用數(shù)據(jù)庫(kù)默認(rèn)值的問(wèn)題及解決
這篇文章主要介紹了MyBatis-Plus不使用數(shù)據(jù)庫(kù)默認(rèn)值的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
springBoot啟動(dòng)輸出三行日志控制臺(tái)自動(dòng)停止操作
這篇文章主要介紹了springBoot啟動(dòng)輸出三行日志控制臺(tái)自動(dòng)停止操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
簡(jiǎn)單易懂的MyBatis分庫(kù)分表方案分享
這篇文章主要給大家介紹了關(guān)于MyBatis分庫(kù)分表方案的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用MyBatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
MyBatis關(guān)閉一級(jí)緩存的兩種方式(分注解和xml兩種方式)
這篇文章主要介紹了MyBatis關(guān)閉一級(jí)緩存的兩種方式(分注解和xml兩種方式),mybatis默認(rèn)開啟一級(jí)緩存,執(zhí)行2次相同sql,但是第一次查詢sql結(jié)果會(huì)加工處理這個(gè)時(shí)候需要關(guān)閉一級(jí)緩存,本文給大家詳細(xì)講解需要的朋友可以參考下2022-11-11
關(guān)于bootstrap.yml和bootstrap.properties的優(yōu)先級(jí)問(wèn)題
這篇文章主要介紹了關(guān)于bootstrap.yml和bootstrap.properties的優(yōu)先級(jí)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java開發(fā)者必備10大數(shù)據(jù)工具和框架
這篇文章主要為大家詳細(xì)介紹了Java開發(fā)者必備10大數(shù)據(jù)工具和框架,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
SpringBoot3實(shí)現(xiàn)webclient的通用方法詳解
Spring Boot WebClient 是 Spring Framework 5 中引入的一個(gè)新的響應(yīng)式 Web 客戶端,用于異步和響應(yīng)式地與外部服務(wù)進(jìn)行通信,下面我們就來(lái)看看SpringBoot3實(shí)現(xiàn)webclient的通用方法吧2024-04-04
springboot配置aop切面日志打印過(guò)程解析
這篇文章主要介紹了springboot配置aop切面日志打印過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01

