在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

MQ发送文件到队列的Java代码

2016-12-20 13:15| 发布者: zhangjf| 查看: 498| 评论: 0

摘要: MQ发送文件到队列 mqfilesend.java package com.mq.dpca.file;import java.io.File;import java.io.FileInputStream;import com.ibm.mq.MQEnvironment;import com.ibm.mq.MQException;imp ...
MQ发送文件到队列

mqfilesend.java
  1. package com.mq.dpca.file;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import com.ibm.mq.MQEnvironment;
  5. import com.ibm.mq.MQException;
  6. import com.ibm.mq.MQMessage;
  7. import com.ibm.mq.MQPutMessageOptions;
  8. import com.ibm.mq.MQQueue;
  9. import com.ibm.mq.MQQueueManager;
  10. import com.ibm.mq.constants.MQConstants;
  11. /**
  12. *
  13. * MQ发送文件功能
  14. *
  15. */
  16. public class mqfilesend {
  17. final int BUFFER_LEN = 1024 * 1024; // 定义发送文件的大小
  18. private MQQueueManager qmgr; // 连接到队列管理器
  19. private MQQueue outQueue; // 传输队列
  20. private String queueName = "aa"; // 队列名称
  21. private String host = "127.0.0.1"; // 队列名称
  22. private int port = 1414; // 侦听器的端口号
  23. private String channel = "SYSTEM.BKR.CONFIG"; // 通道名称
  24. private String qmgrName = "mm"; // 队列管理器
  25. private MQMessage outMsg; // 创建消息缓冲
  26. private MQPutMessageOptions pmo; // 设置获取消息选项
  27. private String fileName = "D:\log.txt"; // 要往队列上发的文件
  28. /**
  29. * 初始化服务器连接信息
  30. *
  31. * @throws Exception
  32. */
  33. private void init() throws Exception {
  34. /** 设置MQEnvironment 属性以便客户机连接 */
  35. MQEnvironment.hostname = host;
  36. MQEnvironment.channel = channel;
  37. MQEnvironment.port = port;
  38. // MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,MQC.TRANSPORT_WEBSPHERE
  39. // MQ);
  40. /* 连接到队列管理器 */
  41. qmgr = new MQQueueManager(qmgrName);
  42. /* 设置队列打开选项以便输出 */
  43. int opnOptn = MQConstants.MQOO_OUTPUT
  44. | MQConstants.MQOO_FAIL_IF_QUIESCING;
  45. // int opnOptn = MQConstants.MQOO_OUTPUT ;
  46. outQueue = qmgr.accessQueue(queueName, opnOptn, null, null, null);
  47. }
  48. /**
  49. * 发送的主程序
  50. *
  51. * @throws Exception
  52. */
  53. public void sendMessages() throws Exception {
  54. /* 设置放置消息选项 */
  55. pmo = new MQPutMessageOptions();
  56. outMsg = new MQMessage();
  57. FileInputStream fis = new FileInputStream(new File(fileName));
  58. byte buffer[] = new byte[BUFFER_LEN];
  59. int count = 0;
  60. while (true) {
  61. count = fis.read(buffer, 0, BUFFER_LEN);
  62. if (count == -1) {
  63. break;
  64. }
  65. outMsg.write(buffer);
  66. if (count < BUFFER_LEN) {
  67. System.out.println("aaa");
  68. }
  69. outQueue.put(outMsg, pmo);
  70. outMsg.clearMessage();
  71. }
  72. fis.close();
  73. }
  74. public void runGoupSender() {
  75. try {
  76. init();
  77. sendMessages();
  78. qmgr.commit();
  79. System.out.println("n Messages successfully Send ");
  80. } catch (MQException mqe) {
  81. mqe.printStackTrace();
  82. try {
  83. System.out.println("n Backing out Transaction ");
  84. qmgr.backout();
  85. System.exit(2);
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. System.exit(2);
  89. }
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. System.exit(2);
  93. }
  94. }
  95. /**
  96. * 程序的入口
  97. *
  98. * @param args
  99. */
  100. public static void main(String args[]) {
  101. MQFileSender mfs = new MQFileSender();
  102. int i = 0;
  103. while (true) {
  104. i++;
  105. System.out.println("消息记录" + i);
  106. mfs.runGoupSender();
  107. if (i == 1) {
  108. break;
  109. }
  110. }
  111. }
  112. }
复制代码
MQConfig.java ~ 6KB (6)
  1. package com.mq.dpca.msg;
  2. import java.io.BufferedInputStream;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.Properties;
  8. import java.util.PropertyResourceBundle;
  9. import java.util.ResourceBundle;
  10. import com.mq.dpca.util.RenameUtil;
  11. /**
  12. * MQ访问配置文件各参数的获取
  13. *
  14. */
  15. public class MQConfig {
  16. // MQ配置及server配置文件路径
  17. private static final String ACTIONPATH = "config.properties";
  18. private static MQConfig instance = null;
  19. private String MQ_MANAGER = null;
  20. private String MQ_HOST_NAME = null;
  21. private String MQ_CHANNEL = null;
  22. private String MQ_QUEUE_NAME = null;
  23. private String MQ_PROT = null;
  24. private String MQ_CCSID = null;
  25. private String MQ_QUEUE_SUB = null;
  26. private String FILE_DIR = null;
  27. public String getFILE_DIR() {
  28. return FILE_DIR;
  29. }
  30. public void setFILE_DIR(String fILE_DIR) {
  31. FILE_DIR = fILE_DIR;
  32. }
  33. public String getMQ_MANAGER() {
  34. return MQ_MANAGER;
  35. }
  36. public void setMQ_MANAGER(String mq_manager) {
  37. MQ_MANAGER = mq_manager;
  38. }
  39. public String getMQ_HOST_NAME() {
  40. return MQ_HOST_NAME;
  41. }
  42. public void setMQ_HOST_NAME(String mq_host_name) {
  43. MQ_HOST_NAME = mq_host_name;
  44. }
  45. public String getMQ_CHANNEL() {
  46. return MQ_CHANNEL;
  47. }
  48. public void setMQ_CHANNEL(String mq_channel) {
  49. MQ_CHANNEL = mq_channel;
  50. }
  51. public String getMQ_QUEUE_NAME() {
  52. return MQ_QUEUE_NAME;
  53. }
  54. public void setMQ_QUEUE_NAME(String mq_queue_name) {
  55. MQ_QUEUE_NAME = mq_queue_name;
  56. }
  57. public String getMQ_PROT() {
  58. return MQ_PROT;
  59. }
  60. public void setMQ_PROT(String mq_prot) {
  61. MQ_PROT = mq_prot;
  62. }
  63. public String getMQ_CCSID() {
  64. return MQ_CCSID;
  65. }
  66. public void setMQ_CCSID(String mq_ccsid) {
  67. MQ_CCSID = mq_ccsid;
  68. }
  69. public static MQConfig getInstance() {
  70. if (instance == null) {
  71. instance = new MQConfig().getNewDbConfig();
  72. }
  73. return instance;
  74. }
  75. public static ResourceBundle getCFG(){
  76. String cfgPath = "";
  77. InputStream in = null;
  78. ResourceBundle pathCfg = null;
  79. ResourceBundle mqCfg = null;
  80. pathCfg = PropertyResourceBundle.getBundle("config");
  81. cfgPath = RenameUtil.getParameter(pathCfg, "mqcfgPath");
  82. try {
  83. in = new BufferedInputStream(new FileInputStream(cfgPath));
  84. mqCfg = new PropertyResourceBundle(in);
  85. return mqCfg;
  86. } catch (FileNotFoundException e) {
  87. e.printStackTrace();
  88. }catch (IOException e) {
  89. e.printStackTrace();
  90. }
  91. return mqCfg;
  92. }
  93. public static MQConfig getNewDbConfig() {
  94. MQConfig dc = new MQConfig();
  95. Properties prop = new Properties();
  96. String path = null;
  97. InputStream fis = null;
  98. try {
  99. fis = MQConfig.class.getClassLoader().getResourceAsStream(
  100. "config.properties");
  101. // fis = new FileInputStream(new File(path + ACTIONPATH));
  102. prop.load(fis);
  103. dc.MQ_CCSID = prop.getProperty("MQ_CCSID");
  104. dc.MQ_CHANNEL = prop.getProperty("MQ_CHANNEL");
  105. dc.MQ_HOST_NAME = prop.getProperty("MQ_HOST_NAME");
  106. dc.MQ_MANAGER = prop.getProperty("MQ_MANAGER");
  107. dc.MQ_PROT = prop.getProperty("MQ_PROT");
  108. dc.MQ_QUEUE_NAME = prop.getProperty("MQ_QUEUE_NAME");
  109. dc.MQ_QUEUE_SUB = prop.getProperty("MQ_QUEUE_SUB");
  110. dc.FILE_DIR = prop.getProperty("FILE_DIR");
  111. } catch (FileNotFoundException e) {
  112. e.printStackTrace();
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. }
  116. return dc;
  117. }
  118. public static MQConfig MqConfig() {
  119. MQConfig dc = new MQConfig();
  120. ResourceBundle rb = getCFG();
  121. try {
  122. // fis = new FileInputStream(new File(path + ACTIONPATH));
  123. dc.MQ_CCSID = RenameUtil.getParameter(rb,"MQ_CCSID");
  124. dc.MQ_CHANNEL = RenameUtil.getParameter(rb,"MQ_CHANNEL");
  125. dc.MQ_HOST_NAME = RenameUtil.getParameter(rb,"MQ_HOST_NAME");
  126. dc.MQ_MANAGER = RenameUtil.getParameter(rb,"MQ_MANAGER");
  127. dc.MQ_PROT = RenameUtil.getParameter(rb,"MQ_PROT");
  128. dc.MQ_QUEUE_NAME = RenameUtil.getParameter(rb,"MQ_QUEUE_NAME");
  129. dc.MQ_QUEUE_SUB = RenameUtil.getParameter(rb,"MQ_QUEUE_SUB");
  130. dc.FILE_DIR = RenameUtil.getParameter(rb,"FILE_DIR");
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. }
  134. return dc;
  135. }
  136. public static MQConfig getNewDbConfigFromKey(String key) {
  137. MQConfig dc = null;
  138. Properties prop = new Properties();
  139. String path = null;
  140. InputStream fis = null;
  141. try {
  142. fis = MQConfig.class.getClassLoader().getResourceAsStream(
  143. "config.properties");
  144. // fis = new FileInputStream(new File(path + ACTIONPATH));
  145. prop.load(fis);
  146. dc = new MQConfig();
  147. dc.MQ_MANAGER = prop.getProperty(key + "_MQ_MANAGER");
  148. dc.MQ_CCSID = prop.getProperty(key + "_MQ_CCSID");
  149. dc.MQ_CHANNEL = prop.getProperty(key + "_MQ_CHANNEL");
  150. dc.MQ_HOST_NAME = prop.getProperty(key + "_MQ_HOST_NAME");
  151. dc.MQ_PROT = prop.getProperty(key + "_MQ_PROT");
  152. dc.MQ_QUEUE_NAME = prop.getProperty(key + "_MQ_QUEUE_NAME");
  153. dc.MQ_QUEUE_SUB = prop.getProperty(key + "_MQ_QUEUE_SUB");
  154. dc.FILE_DIR = prop.getProperty(key + "_FILE_DIR");
  155. } catch (FileNotFoundException e) {
  156. dc = null;
  157. } catch (IOException e) {
  158. dc = null;
  159. }
  160. return dc;
  161. }
  162. public static void main(String args[]) throws Exception {
  163. MQConfig mc = MQConfig.getNewDbConfigFromKey("002");
  164. System.out.println(mc);
  165. }
  166. public void setMQ_QUEUE_SUB(String mQ_QUEUE_SUB) {
  167. MQ_QUEUE_SUB = mQ_QUEUE_SUB;
  168. }
  169. public String getMQ_QUEUE_SUB() {
  170. return MQ_QUEUE_SUB;
  171. }
  172. public static String getValueByKey(String key) {
  173. InputStream fis = null;
  174. String value = null;
  175. Properties prop = new Properties();
  176. try {
  177. fis = MQConfig.class.getClassLoader().getResourceAsStream(
  178. "config.properties");
  179. prop.load(fis);
  180. value = prop.getProperty(key);
  181. } catch (FileNotFoundException e) {
  182. e.printStackTrace();
  183. } catch (IOException e) {
  184. e.printStackTrace();
  185. }
  186. return value;
  187. }
  188. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-8-23 04:36

Copyright 2015-2025 djqfx

返回顶部