java结合WebSphere MQ实现接收队列文件功能

所属分类: 软件编程 / java 阅读数: 46
收藏 0 赞 0 分享

首先我们先来简单介绍下websphere mq以及安装使用简介

websphere mq  : 用于传输信息 具有跨平台的功能。

1 安装websphere mq 并启动

2 websphere mq 建立 queue Manager (如:MQSI_SAMPLE_QM)

3 建立queue 类型选择 Local类型 的 (如lq  )

4 建立channels 类型选择Server Connection (如BridgeChannel)

接下来,我们来看实例代码:

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分组接收文件功能
 * 主动轮询
 */
public class MQFileReceiver {
  private MQQueueManager qmgr; // 连接到队列管理器
 
  private MQQueue inQueue; // 传输队列
 
  private String queueName = ""; // 队列名称
 
  private String host = ""; //
 
  private int port = 1414; // 侦听器的端口号
 
  private String channel = ""; // 通道名称
 
  private String qmgrName = ""; // 队列管理器
 
  private MQMessage inMsg; // 创建消息缓冲
 
  private MQGetMessageOptions gmo; // 设置获取消息选项
 
  private static String fileName = null; // 接收队列上的消息并存入文件
 
  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)){
//     //调用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);
    }
  }
 
  /**
   * 初始化服务器连接信息
   * 
   * @throws Exception
   */
  private void init() throws Exception {
    /* 为客户机连接设置MQEnvironment属性 */
    MQEnvironment.hostname = host;
    MQEnvironment.channel = channel;
    MQEnvironment.port = port;
 
    /* 连接到队列管理器 */
    qmgr = new MQQueueManager(qmgrName);
 
    /* 设置队列打开选项以输 */
    int opnOptn = MQConstants.MQOO_INPUT_AS_Q_DEF
        | MQConstants.MQOO_FAIL_IF_QUIESCING;
 
    /* 打开队列以输 */
    inQueue = qmgr.accessQueue(queueName, opnOptn, null, null, null);
  }
 
  /**
   * 接受文件的主函数
   * 
   * @throws Exception
   */
  public void getGroupMessages() {
    /* 设置获取消息选项 */
    gmo = new MQGetMessageOptions();
    gmo.options = MQConstants.MQGMO_FAIL_IF_QUIESCING;
    gmo.options = gmo.options + MQConstants.MQGMO_SYNCPOINT;
    /* 等待消息 */
    gmo.options = gmo.options + MQConstants.MQGMO_WAIT;
    /* 设置等待时间限制 */
    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;
    /* 创建消息缓冲 */
    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);
              /* 查看是否是最后消息标识 */
              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);
            /* 查看是否是最后消息标识 */
            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();
    }
  }
}

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多