JAVA calls MQ

Source: Internet
Author: User

Prerequisites: MQ server-side creation

Queue Manager: QueueManager

Queue: xml.in

Server Connection channel: SYSTEM.ADMIN.SVRCONN

The listener listens on port 1414.

Package Mq;import Com.ibm.mq.mqc;import Com.ibm.mq.mqenvironment;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;/** * Test Call MQ * * @author Administrator * */public class TESTMQ {/** * Queue Manager */private static F Inal string m Q_Q ueue_manager = "QueueManager";/** * Queue */private static final String m Q_Q ueue = "XML. In ";/** * Channel */private static final String Mq_channel =" SYSTEM. ADMIN. SVRCONN ";/** * IP */private static final String mq_host =" localhost ";/** * port */private static final int mq_port = 1414;p Rivate mqqueuemanager qmgr;/** * Test * @param args */public static void main (string[] args) {TESTMQ test = new TESTMQ (); TES T.getconnmqmanager (); Test.sendmessage ("1111111"); Test.receivemsg ();} /** * Send Message to MQ * @param message */public void SendMessage (String message) {Mqqueue Mqqueue = null;try {//Establish connection int Openoptio NS = MQC. Mqoo_input_as_q_def | MQC. Mqoo_output| MQC. Mqoo_inquire;mqqueuE = Qmgr.accessqueue (M Q_Q ueue, openoptions, NULL, NULL, NULL); Mqmessage msg = new Mqmessage (); Msg.format = MQC. Mqfmt_string;msg.characterset = 1381;msg.writeobject (message); Mqputmessageoptions PMO = new Mqputmessageoptions ();//Set message with no expiration Msg.expiry =-1; Put the message in the queue Mqqueue.put (MSG, PMO);} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();} finally{if (Mqqueue!=null) {try {mqqueue.close ();} catch (Exception E2) {//Todo:handle exceptione2.printstacktrace ();}}}} /** * Read MQ message */public void Receivemsg () {Mqqueue Mqqueue = null;try {//establish connection int openoptions = MQC. Mqoo_input_as_q_def | MQC. Mqoo_output| MQC. Mqoo_inquire;mqqueue = Qmgr.accessqueue (M Q_Q ueue, openoptions, NULL, NULL, NULL);//queue depth int depth = Mqqueue.getcurrentdepth (); System.out.println ("Current queue depth is:" +depth), while (depth-->0) {//message to read mqmessage msg = new Mqmessage (); Mqgetmessageoptions MqGet = new Mqgetmessageoptions (); Mqqueue.get (msg, mqGet); SYSTEM.OUT.PRINTLN ("Message size:" +msg.getdatalength ()); SYSTEM.OUT.PRINTLN ("Message content is:" +msG.readobject ());}} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();} finally{if (Mqqueue!=null) {try {mqqueue.close ();} catch (Exception E2) {//Todo:handle exceptione2.printstacktrace ();}}}} /** * Get connection */public void Getconnmqmanager () {mqenvironment.hostname = Mq_host; Mqenvironment.channel = Mq_channel; Mqenvironment.ccsid = 1381; Character encoding Mqenvironment.port = mq_port;try {qMgr = new Mqqueuemanager (M Q_Q ueue_manager);} catch (Exception e) {//Todo:handl e exceptione.printstacktrace ();}}  /** * Close connection */public void Closeconnmqmanager () {if (qMgr! = null) {try {qmgr.close ()} catch (Exception e) {//Todo:handle Exceptione.printstacktrace ();}}}
For references to the following error http://www-01.ibm.com/support/docview.wss?uid=swg21636532
MQJE001: Occurs mqexception: Completion code 2, Reason 2035
MQJE036: Queue Manager rejects a connection attempt
MQJE001: Completion code 2, Reason 2035
COM.IBM.MQ.MQEXCEPTION:MQJE001: Completion code 2, Reason 2035

JAVA calls MQ

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.