- Package cjf.mq.mqclient;
- Import Com.ibm.mq.MQC;
- Import com.ibm.mq.MQEnvironment;
- Import com.ibm.mq.MQException;
- Import com.ibm.mq.MQGetMessageOptions;
- Import Com.ibm.mq.MQMessage;
- Import com.ibm.mq.MQPutMessageOptions;
- Import Com.ibm.mq.MQQueue;
- Import Com.ibm.mq.MQQueueManager;
- Import com.ibm.mq.MQGetMessageOptions;
- Public class Mqclient {
- static Mqqueuemanager qMgr;
- static int CCSID = 1381; Wingbk,1208:utf-8
- static String queuestring = "M Q_Q ueue";
- public static Void Connect () throws mqexception {
- Mqenvironment.hostname = "";
- Mqenvironment.channel = "Java.channel";
- Mqenvironment.port = 1321;
- Mqenvironment.ccsid = CCSID;
- QMGR = New Mqqueuemanager ("mq_test");
- Qmgr.disconnect ();
- }
- public static void sendmsg (String msgStr) {
- int openoptions = MQC. Mqoo_input_as_q_def | MQC. Mqoo_output
- | MQC. Mqoo_inquire;
- Mqqueue queue = null;
- try {
- //Establish a connection to the Q1 channel
- Queue = QMgr
- . Accessqueue (Queuestring, openoptions, null, null, null);
- Mqmessage msg = new Mqmessage (); Messages to write to the queue
- Msg.format = MQC. mqfmt_string;
- Msg.characterset = CCSID;
- msg.encoding = CCSID;
- //Msg.writeobject (MSGSTR);//write messages to the Message object
- Msg.writestring (MSGSTR);
- Mqputmessageoptions PMO = new Mqputmessageoptions ();
- Msg.expiry =-1; //Set messages with no expiration
- Queue.put (MSG, PMO); //Put messages in a queue
- } catch (Exception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- } finally {
- if (queue! = null) {
- try {
- Queue.close ();
- } catch (Mqexception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }
- }
- }
- public static void Receivemsg () {
- int openoptions = MQC. Mqoo_input_as_q_def | MQC. Mqoo_output
- | MQC. Mqoo_inquire;
- Mqqueue queue = null;
- try {
- Queue = QMgr
- . Accessqueue (Queuestring, openoptions, null, null, null);
- System.out.println ("The current depth of the queue is:" + queue.getcurrentdepth ());
- System.out.println ("===========================");
- int depth = queue.getcurrentdepth ();
- //read out the messages in the queue
- While (depth--> 0) {
- Mqmessage msg = new Mqmessage (); Message for the queue to read
- Mqgetmessageoptions GMO = new Mqgetmessageoptions ();
- Queue.get (MSG, GMO);
- SYSTEM.OUT.PRINTLN ("message size:" + msg.getdatalength ());
- System.out.println ("content of message: \ n" )
- + Msg.readline ());
- System.out.println ("---------------------------");
- }
- } catch (Exception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- } finally {
- if (queue! = null) {
- try {
- Queue.close ();
- } catch (Mqexception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }
- }
- }
- public static void Main (string[] args) throws mqexception {
- Connect ();
- sendmsg ("Fuck MQ");
- Receivemsg ();
- }
- }
Java connection instance of MQ, test class