查看: 1817|回复: 0

[Java代码] MQ发送消息到队列的Java代码

发表于 2016-10-18 12:00:00
MQ发送消息到队列

MsgSend.java
  1. package com.mq.dpca.msg;
  2. import javax.jms.JMSException;
  3. import javax.jms.Queue;
  4. import javax.jms.QueueConnection;
  5. import javax.jms.QueueSender;
  6. import javax.jms.QueueSession;
  7. import javax.jms.Session;
  8. import javax.jms.TextMessage;
  9. import com.ibm.mq.jms.MQQueueConnectionFactory;
  10. import com.ibm.msg.client.wmq.WMQConstants;
  11. /**
  12. * MQ字符串发送功能
  13. *
  14. */
  15. public class MsgSend {
  16. MQQueueConnectionFactory mcf;
  17. QueueConnection qconn;
  18. MQConfig mc = null;
  19. /**
  20. * 初始化连接
  21. *
  22. * @throws JMSException
  23. */
  24. public void openConnection() throws JMSException {
  25. mc = MQConfig.getInstance();
  26. mcf = new MQQueueConnectionFactory();
  27. mcf.setConnectionNameList(mc.getMQ_HOST_NAME());
  28. // mcf.setHostName(mc.getMQ_HOST_NAME());
  29. mcf.setChannel(mc.getMQ_CHANNEL());
  30. mcf.setTransportType(WMQConstants.WMQ_CM_CLIENT);
  31. mcf.setClientReconnectOptions(WMQConstants.WMQ_CLIENT_RECONNECT);
  32. // mcf.setPort(Integer.parseInt(mc.getMQ_PROT()));
  33. mcf.setCCSID(Integer.parseInt(mc.getMQ_CCSID()));
  34. mcf.setQueueManager(mc.getMQ_MANAGER());
  35. qconn = mcf.createQueueConnection();
  36. }
  37. /**
  38. * 发送消息
  39. *
  40. * @param key
  41. * @param msgInfo
  42. * @throws JMSException
  43. * @throws InterruptedException
  44. */
  45. public void send(String key, String msgInfo) {
  46. QueueSession session = null;
  47. QueueSender sender = null;
  48. try {
  49. openConnection();
  50. session = qconn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
  51. Queue queue = session.createQueue(mc.getMQ_QUEUE_NAME());
  52. sender = session.createSender(queue);
  53. TextMessage msg = session.createTextMessage();
  54. msg.setText(msgInfo);
  55. // msg.setStringProperty("Key", key);
  56. sender.send(msg);
  57. } catch (JMSException e) {
  58. e.printStackTrace();
  59. } finally {
  60. try {
  61. sender.close();
  62. session.close();
  63. qconn.close();
  64. } catch (JMSException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. }
  69. /**
  70. * 程序的入口
  71. * @param args
  72. * @throws JMSException
  73. * @throws InterruptedException
  74. */
  75. public static void main(String[] args) throws JMSException,
  76. InterruptedException {
  77. MsgSend ms = new MsgSend();
  78. String str = "<root><esbHeader><requestId>d5d001f47d46e1471ae42ebf1dded7a2</requestId><serviceName>syncCustmoerInfo</serviceName>"
  79. + "<key>3DOxe6EfkvcAAAFH95SjNwAAAAAAAAAD</key></esbHeader><serviceNameReq><security><key>f7b17a6ffcd5561748c5bb98075aa432</key><date>2013-04-10 15:25:00</date></security>"
  80. + "<customer><khmc>测试客户ES2B</khmc><fzxsid>120483321</fzxsid><khczqh>010</khczqh><khczfj>1234</khczfj><khcz>567892012</khcz>"
  81. + "</customer>"
  82. + "<customer><khmc>测试客户ES2B</khmc><fzxsid>120483321</fzxsid><khczqh>010</khczqh><khczfj>1234</khczfj><khcz>567892012</khcz>"
  83. + "</customer>"
  84. + "<customer><khmc>测试客户ES2B</khmc><fzxsid>120483321</fzxsid><khczqh>010</khczqh><khczfj>1234</khczfj><khcz>567892012</khcz>"
  85. + "</customer>" + "</serviceNameReq>" + "</root>";
  86. ms.send("", str);
  87. // Thread.sleep(10000);
  88. System.out.print("消息发送完毕!");
  89. }
  90. }
复制代码
MQConfig.java ~ 6KB
  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. }
复制代码


回复

使用道具 举报