Java connection instance of MQ, test class

Source: Internet
Author: User
Tags sendmsg

  1. Package cjf.mq.mqclient;
  2. Import Com.ibm.mq.MQC;
  3. Import com.ibm.mq.MQEnvironment;
  4. Import com.ibm.mq.MQException;
  5. Import com.ibm.mq.MQGetMessageOptions;
  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.MQGetMessageOptions;
  11. Public class Mqclient {
  12. static Mqqueuemanager qMgr;
  13. static int CCSID = 1381; Wingbk,1208:utf-8
  14. static String queuestring = "M Q_Q ueue";
  15. public static Void Connect () throws mqexception {
  16. Mqenvironment.hostname = "";
  17. Mqenvironment.channel = "Java.channel";
  18. Mqenvironment.port = 1321;
  19. Mqenvironment.ccsid = CCSID;
  20. QMGR = New Mqqueuemanager ("mq_test");
  21. Qmgr.disconnect ();
  22. }
  23. public static void sendmsg (String msgStr) {
  24. int openoptions = MQC. Mqoo_input_as_q_def | MQC. Mqoo_output
  25. | MQC. Mqoo_inquire;
  26. Mqqueue queue = null;
  27. try {
  28. //Establish a connection to the Q1 channel
  29. Queue = QMgr
  30. . Accessqueue (Queuestring, openoptions, null, null, null);
  31. Mqmessage msg = new Mqmessage (); Messages to write to the queue
  32. Msg.format = MQC. mqfmt_string;
  33. Msg.characterset = CCSID;
  34. msg.encoding = CCSID;
  35. //Msg.writeobject (MSGSTR);//write messages to the Message object
  36. Msg.writestring (MSGSTR);
  37. Mqputmessageoptions PMO = new Mqputmessageoptions ();
  38. Msg.expiry =-1; //Set messages with no expiration
  39. Queue.put (MSG, PMO); //Put messages in a queue
  40. } catch (Exception e) {
  41. //TODO auto-generated catch block
  42. E.printstacktrace ();
  43. } finally {
  44. if (queue! = null) {
  45. try {
  46. Queue.close ();
  47. } catch (Mqexception e) {
  48. //TODO auto-generated catch block
  49. E.printstacktrace ();
  50. }
  51. }
  52. }
  53. }
  54. public static void Receivemsg () {
  55. int openoptions = MQC. Mqoo_input_as_q_def | MQC. Mqoo_output
  56. | MQC. Mqoo_inquire;
  57. Mqqueue queue = null;
  58. try {
  59. Queue = QMgr
  60. . Accessqueue (Queuestring, openoptions, null, null, null);
  61. System.out.println ("The current depth of the queue is:" + queue.getcurrentdepth ());
  62. System.out.println ("===========================");
  63. int depth = queue.getcurrentdepth ();
  64. //read out the messages in the queue
  65. While (depth--> 0) {
  66. Mqmessage msg = new Mqmessage (); Message for the queue to read
  67. Mqgetmessageoptions GMO = new Mqgetmessageoptions ();
  68. Queue.get (MSG, GMO);
  69. SYSTEM.OUT.PRINTLN ("message size:" + msg.getdatalength ());
  70. System.out.println ("content of message: \ n" )
  71. + Msg.readline ());
  72. System.out.println ("---------------------------");
  73. }
  74. } catch (Exception e) {
  75. //TODO auto-generated catch block
  76. E.printstacktrace ();
  77. } finally {
  78. if (queue! = null) {
  79. try {
  80. Queue.close ();
  81. } catch (Mqexception e) {
  82. //TODO auto-generated catch block
  83. E.printstacktrace ();
  84. }
  85. }
  86. }
  87. }
  88. public static void Main (string[] args) throws mqexception {
  89. Connect ();
  90. sendmsg ("Fuck MQ");
  91. Receivemsg ();
  92. }
  93. }

Java connection instance of MQ, test class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.