Java connection to IBM MQ

Source: Internet
Author: User
Tags ibm mq

Package com.hometest.IBMMqTest;

Import java.io.IOException;
Import java.io.UnsupportedEncodingException;

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.constants.MQConstants;

public class Testmq {

Private Mqqueuemanager Qmanager = null;
Private Mqqueue queue = null;
private static final String mq_host = "";
private static final Integer mq_port = 1414;
private static final String Mq_channel = "";
Private static final String m Q_Q ueuemanager_name = "Mq_app";
Private static final String m Q_Q ueue_name = "Queue1";

public static void Main (string[] args) {
TODO auto-generated Method Stub
TESTMQ MQ = new TESTMQ ();

try {
Mq.send ("Hello everyone");
Mq.send ("what");
System.out.println (Mq.receive ());
} catch (Mqexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Mq.receiveall ();
Integer depth = mq.getqueuedepth ();
System.out.println ("The current depth of the queue is" + depth);
Mq.close ();
}


Public Testmq () {
try {
Mqenvironment.hostname = Mq_host;
Mqenvironment.port = Mq_port;
Mqenvironment.channel = Mq_channel;
Qmanager = new Mqqueuemanager (M Q_Q ueuemanager_name);
int openoptions = Mqconstants.mqoo_input_as_q_def | Mqconstants.mqoo_inquire | Mqconstants.mqoo_output;
Queue = Qmanager.accessqueue (M Q_Q ueue_name, openoptions);
} catch (Mqexception e) {
E.printstacktrace ();
}
}

Public Integer getqueuedepth () {
Integer depth = 0;
try {
if (queue! = null && queue.isopen ()) {
depth = Queue.getcurrentdepth ();
}
} catch (Mqexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

return depth;
}

public void Close () {
if (queue! = null && queue.isopen ()) {
try {
Queue.close ();
} catch (Mqexception e) {
E.printstacktrace ();
}
}
if (Qmanager! = null && qmanager.isconnected ()) {
try {
Qmanager.disconnect ();
} catch (Mqexception e) {
E.printstacktrace ();
}
}
}


public void Send (String msg) throws Mqexception {
try {

Mqputmessageoptions mqputmessageoptions = new Mqputmessageoptions ();
Mqmessage mqmessage = new Mqmessage ();
Mqmessage.write (Msg.getbytes ("Utf-8"));
Queue.put (Mqmessage, mqputmessageoptions);
System.out.println ("Sent a message:" + msg);

} catch (Unsupportedencodingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

Public String receive () throws mqexception{
Mqmessage mqmsg = new Mqmessage ();
Mqgetmessageoptions mqgetmessageoptions = new Mqgetmessageoptions ();
try {

Queue.get (mqmsg, mqgetmessageoptions);
int len = Mqmsg.getdatalength ();
byte[] message = new Byte[len];
mqmsg.readfully (message, 0, Len);
String str = new string (message);
System.out.println ("Received a message:" + str);
return str;
} catch (Mqexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
return null;
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
return null;
}

}

public void Receiveall () {
try {
int depth = queue.getcurrentdepth ();
System.out.println ("The current depth of the queue is" + depth);
SYSTEM.OUT.PRINTLN ("Now receive all messages ....");
while (depth--> 0) {
Mqmessage mqmsg = new Mqmessage ();
Mqgetmessageoptions mqgetmessageoptions = new Mqgetmessageoptions ();
Queue.get (mqmsg, mqgetmessageoptions);
int len = Mqmsg.getdatalength ();
byte[] message = new Byte[len];
mqmsg.readfully (message, 0, Len);
System.out.println (new String (message));

}
} catch (Mqexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}


}




}

Java connection to IBM 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.