IBM WebSphere MQ message sending and fetching

Source: Internet
Author: User

I. Required dependency packages, after installing IBM WebSphere MQ, in the Java directory under the installation directory

Importjava.io.IOException;Importjava.util.Properties;ImportCom.ibm.mq.MQC;Importcom.ibm.mq.MQEnvironment;Importcom.ibm.mq.MQException;Importcom.ibm.mq.MQGetMessageOptions;ImportCom.ibm.mq.MQMessage;Importcom.ibm.mq.MQPutMessageOptions;ImportCom.ibm.mq.MQQueue;ImportCom.ibm.mq.MQQueueManager;ImportCom.ibm.mq.constants.CMQC;Importcom.ibm.mq.constants.MQConstants;/*** Client Mode development * *@authorGeely **/ Public classMQTest1 { Public Static voidMain (string[] args)throwsmqexception, IOException {//send message to queueput (); //reading messages from a queueget (); //Get Queue Depthgetdepth (); } @SuppressWarnings ("Unchecked")    Static voidPut ()throwsmqexception, IOException {//configuring MQ Server Connection ParametersMqenvironment.hostname = "127.0.0.1"; Mqenvironment.port= 1414; Mqenvironment.channel= "Qm_ack"; //Mqenvironment.userid = ""; //Mqenvironment.password = ""; //Set Queue Manager character set encodingMqenvironment.ccsid = 1381; //set the app name to allow server MQ to view app connectionsMQEnvironment.properties.put (Mqconstants.appname_property, "MQ Test by Java"); //set the app name to allow server MQ to view app connectionsMQEnvironment.properties.put (MQC. Transport_property, MQC.        Transport_mqseries); //creating an instance, connecting the queue ManagerMqqueuemanager QueueManager =NewMqqueuemanager ("QM", mqenvironment.properties); //write a writable way to access the Queue Manager's defined queue QUEUE1, or you can create a queueMqqueue putqueue = Queuemanager.accessqueue ("Queue_recv", CMQC.        Mqoo_output); //new and send message to queueMqmessage Mymessage =NewMqmessage (); Mymessage.expiry=-1;//set messages with no expirationString name = "Meplusplus ' s Blog";        MYMESSAGE.WRITEUTF (name); //using the default message optionsMqputmessageoptions PMO =Newmqputmessageoptions (); //Send Messageputqueue.put (Mymessage, PMO);        Putqueue.close (); //Disconnect ConnectionQueuemanager.disconnect (); } @SuppressWarnings ("Unchecked")    Static voidGet ()throwsmqexception, IOException {//configuring MQ Server Connection ParametersMqenvironment.hostname = "127.0.0.1"; Mqenvironment.port= 1414; Mqenvironment.channel= "Qm_ack"; //Mqenvironment.userid = ""; //Mqenvironment.password = ""; //Set Queue Manager character set encodingMqenvironment.ccsid = 1381; //set the app name to allow server MQ to view app connectionsMQEnvironment.properties.put (Mqconstants.appname_property, "MQ Test by Java"); //set the app name to allow server MQ to view app connectionsMQEnvironment.properties.put (CMQC. Transport_property, CMQC.        Transport_mqseries_bindings); //creating an instance, connecting the queue ManagerMqqueuemanager QueueManager =NewMqqueuemanager ("QM", mqenvironment.properties); //Open the queue.        intOpenoptions = Cmqc. Mqoo_input_as_q_def| Cmqc. Mqoo_output|CMQC.        Mqoo_inquire; //access the Queue Manager's defined queues in a readable manner QUEUE1//Mqqueue getqueue = Queuemanager.accessqueue ("Queue_recv", CMQC. MQOO_INPUT_AS_Q_DEF);Mqqueue getqueue = Queuemanager.accessqueue ("Queue_recv", Openoptions,NULL,NULL,NULL); Mqgetmessageoptions GMO=Newmqgetmessageoptions (); //Get messages under Sync Point control. //gets the message under synchronization point control. //gmo.options = gmo.options + CMQC.        Mqgmo_syncpoint; //Wait If no messages on the Queue. //wait if there is no message on the queue. //gmo.options = gmo.options + CMQC.        mqgmo_wait; //Fail if Qeuemanager quiescing. //fails if the queue manager pauses. //gmo.options = gmo.options + CMQC.        mqgmo_fail_if_quiescing; //Sets the time limit for the wait. //set the time limit for the wait.Gmo.waitinterval = 3000; //reading messages from a queueMqmessage themessage =NewMqmessage ();        Getqueue.get (Themessage, GMO); String name=Themessage.readutf ();        SYSTEM.OUT.PRINTLN (name);        Getqueue.close (); //Disconnect ConnectionQueuemanager.disconnect (); }        Static voidGetdepth ()throwsmqexception, IOException {Properties props=NewProperties (); Props. Put ("Hostname", "127.0.0.1"); Props. Put ("Port", 1414);//Port numberProps. Put ("channel", "Qm_ack");//Server Connection ChannelProps. Put ("CCSID", 1381); Props. Put ("Transport", "MQSeries"); //creating an instance, connecting the queue ManagerMqqueuemanager QueueManager =NewMqqueuemanager ("QM", props); //Open the queue.        intOpenoptions = Cmqc. Mqoo_input_as_q_def| Cmqc. Mqoo_output|CMQC.        Mqoo_inquire; //access the Queue Manager's defined queues in a readable manner QUEUE1Mqqueue getqueue = Queuemanager.accessqueue ("Queue_recv", Openoptions,NULL,NULL,NULL); Mqgetmessageoptions GMO=Newmqgetmessageoptions (); //Get messages under Sync Point control. //gets the message under synchronization point control. //gmo.options = gmo.options + CMQC.        Mqgmo_syncpoint; //Wait If no messages on the Queue. //wait if there is no message on the queue. //gmo.options = gmo.options + CMQC.        mqgmo_wait; //Fail if Qeuemanager quiescing. //fails if the queue manager pauses. //gmo.options = gmo.options + CMQC.        mqgmo_fail_if_quiescing; //Sets the time limit for the wait. //set the time limit for the wait.Gmo.waitinterval = 3000; intdepth =getqueue.getcurrentdepth (); System.out.println ("The current depth of the queue is:" +depth); System.out.println ("===========================");  while(depth-->0) {mqmessage msg=NewMqmessage ();//message for the queue to readgetqueue.get (MSG, GMO); System.out.println ("The message size is:" +msg.getdatalength ()); System.out.println ("Content of message: \ n" +Msg.readutf ()); System.out.println ("---------------------------"); } System.out.println ("Finish!!!");        Getqueue.close (); //Disconnect ConnectionQueuemanager.disconnect (); }}

IBM WebSphere MQ message sending and fetching

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.