Reprint: http://blog.csdn.net/ozwarld/article/details/7735915
Two queues are set up in IBM MQ 6.0 (remote queues, channels, and so on are not set).
The Queue Manager is xir_qm_1502
Queue name is Esbreq
IP address is 10.23.117.134 (a remote computer, and my computer is not in a LAN)
Port 1414
CCSID 1208
MQ configuration can refer to this, there is a map http://wenku.baidu.com/view/06d108d0360cba1aa811daa3.html
Program as follows, send thread two, receive thread one. The end is completed when the reception is complete.
/* * Created Date 2012-7-10 * * TODO to change the template for this generated file, go to the * window-preferences-Java-code style-code template */Package yerasel; /** * @author fenglb e-mail:[email protected] * @version creation time: 2009-4-30 04:13:38 class Description */import Java.io.IOExce ption; 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; Interface Someconstants {string qmanager = "xir_qm_1502";//"XIR_QM";//queuemanager name string qName = "Esbreq ";//Queue Name String StrIP =" 10.23.117.134 ";//" 10.24.28.139 ";//" 10.24.28.102 "; int iport = 1502;//1414; String Strchl = "SYSTEM. Def. SVRCONN ";//server-connection Channel int iccsid = 1208; } class Sender implements Runnable, someconstants {public void run () {sendMessage (); } public void SendMessage () {String Name = Thread.CurrentThread (). GetName (); System.out.println ("Enter thread" + name); Mqqueuemanager qMgr = null; Configure connection parameters mqenvironment.hostname = StrIP; Server name or IP mqenvironment.port = Iport; Mqenvironment.channel = Strchl; Mqenvironment.ccsid = Iccsid; Java program to connect MQ in two ways, one is the client way, one is the binding method,//default is the client way, when the MQ deployment in the local time, you need to use the binding mode//native IP is 10.24.28.139 connection 10.23.117.134 Do not need the next sentence//mqenvironment.properties.put (MQC. Transport_property,//MQC. Transport_mqseries_bindings); Create a connection to the QueueManager System.out.println (name + "Connecting to Queue Manager:" + qmanager) ; try {qMgr = new Mqqueuemanager (Qmanager); Set up the options on the queue we wish to open int openoptions = MQC. Mqmt_request | MQC. mqpmo_new_msg_id | MQC. Mqoo_output | MQC. Mqoo_fail_if_quiescing | MQC. Mqoo_input_as_q_def; Now specify the queue, we wish to open and the open options System.out.println (name + "Accessing Queu E: "+ qName); Mqqueue queue = Qmgr.accessqueue (QName, openoptions); Define a simple WebSphere MQ message ...//Specify the default put Message options Mqputme Ssageoptions PMO = new Mqputmessageoptions (); Put the message to the queue System.out.println (name + "Sending a message ..."); Mqmessage msg = new Mqmessage (); Msg.messageid = "MSGID". GetBytes (); Msg.messagetype = MQC. Mqmt_request; Msg.replytoqueuename = "Esbreq"; Here you test the transmit sub-column of MQ for (int j = 1; j < 5; J + +) {msg.messagesequencenumber = J; Write some text in UTF8 format try {String str = "Salemetsizbe Yerasel "; str = str + "" + j; Msg.writeutf (str); Queue.put (MSG, PMO); Msg.clearmessage (); SYSTEM.OUT.PRINTLN (name + "Putting the message ... "+ j); } catch (Mqexception mqe) {mqe.printstacktrace (); Break } catch (IOException E1) {e1.printstacktrace (); }} qmgr.commit (); SYSTEM.OUT.PRINTLN (name + "done!"); System.out.println ("=========="); System.out.println (""); } catch (Mqexception e) {e.printstacktrace (); }}} class Receiver implements Runnable, someconstants {public void run () {recvmessage (); } public void Recvmessage () {String name = Thread.CurrentThread (). GetName (); try { Thread.Sleep (1000); Mqqueuemanager qMgr = null; System.out.println ("Enter thread" + name); SYSTEM.OUT.PRINTLN (name + "Connecting to Queue Manager:" + Qmanager); QMGR = new Mqqueuemanager (Qmanager); Set the queue properties//Note that will be connected. The MQC interface defines all the constants used by the//WebSphere MQ Java Programming Interface (except for completion code constants and error code constants). Mqoo_input_as_q_def:open the queue to get messages using the//queue-defined default. Mqoo_output:open the queue to put messages. int openoptions = MQC. Mqoo_input_as_q_def | MQC. Mqoo_output | MQC. Mqoo_inquire; Now get the message back again. First define a WebSphere MQ//message to receive the data mqmessage rcvmessage = new Mqmessage ( ); Specify default get Message options mqgetmessageoptions GMO = new Mqgetmessageoptions (); Gmo.options = Gmo.options + MQC. mqgmo_syncpoint;//Get messages//Under Sync Point Control (Get message under Sync Point control) gmo.options = Gmo.options + mqc.m qgmo_wait; Wait if no messages//on the Queue (Wait if there is no message on the queue) Gmo.options = Gmo.options + MQC. mqgmo_fail_if_quiescing;//FAIL IF//Qeue Manager Quiescing (fails if Queue Manager pauses) Gmo.waitinterval = 1000; Sets the time limit for the Wait. (Set the waiting millisecond time limit) System.out.println (name + "Accessing queue:" + qName); Mqqueue queue = Qmgr.accessqueue (QName, openoptions); int depth = 0; Get the message off the queue. System.out.println ("... "+ name +" Getting the message Back Again "); for (;;) {try {queue.get (rcvmessage, GMO); System.out.println ("ID:" + (new String (Rcvmessage.messageid)). Trim () + "Num:" + Rcvmessage.messagesequencenumber + "Type:" + Rcvmessage.messagetype + " Flag: "+ rcvmessage.messageflags); and display the message text ... String Msgtext = Rcvmessage.readutf (); SYSTEM.OUT.PRINTLN ("The message is:" + msgtext); Rcvmessage.cleaRmessage (); Break if no MSG left in queue depth = Queue.getcurrentdepth (); if (depth = = 0) break; } catch (Mqexception mqe) {mqe.printstacktrace (); Break Null } catch (IOException e) {e.printstacktrace (); }}//Close the queue System.out.println (name + "Closing the queue"); Queue.close (); Disconnect from the QueueManager System.out.println (name + disconnecting from the Queue Manager); Qmgr.disconnect (); SYSTEM.OUT.PRINTLN (name + "done!"); System.out.println ("=========="); System.out.println (""); } catch (Mqexception ex) {System.out. println ("A WebSphere MQ Error occured:cOmpletion Code "+ Ex.completioncode +" Reason Code "+ Ex.reas Oncode + ex.getmessage ()); } catch (Interruptedexception E1) {e1.printstacktrace (); }}} public class Mqtest {public static void main (String args[]) {/* * Mqtest First = new Mqtest (); First.sendmessage (); * First.recvmessage (); */Sender Sender = new sender (); Thread senderthread = new Thread (sender); Senderthread.start (); Senderthread.setname ("Sender"); Thread senderThread2 = new Thread (sender); Senderthread2.start (); Senderthread2.setname ("Sender2"); Receiver recv = new receiver (); Thread recvthread = new Thread (recv); Recvthread.start (); Recvthread.setname ("Receiver"); Receiver recv = new receiver (); New Thread (recv). Start ();} }
Java calls MQ queue