Full Java program for communication with MQ

Source: Internet
Author: User

The program realizes the function of sending messages and reading messages, see the send*** and get*** methods. This is only appropriate for testing because the program in the environment needs to make a slight change to this, and in the real world it must be in the infinite loop of the while (true) {...} to invoke the Get method, and if there is a value, perform a processing operation on the message, and if there is no value, continue the loop. There is a wait time in the Get method.

This procedure is quite understandable in its own right:

1, first set up some related environment variables

2. Re-connect the queue Manager

3. Operate the specified queue in the queue manager again

4. Send a message to a specified queue or a message from a specified column

5. Close the queue

If you do not know how to configure remote queues in MQ Explorer and send messages to remote MQ through remote queues, see the article:

Http://blog.csdn.net/fenglibing/archive/2009/05/08/4160639.aspx

MQ in the real world, personally feel that at least there should be two local queues plus a remote queue, because the interaction of the message is bound to be mutual, there is receiving messages, there must be a message. A local queue is used to receive messages that are sent externally, and another local team example is used for transport, which is used for the transmission queue of the remote team example, and sends the message to the local queue of the remote host. In order for a message to be successfully delivered to a remote queue, it is also necessary to configure the channel, typically by specifying the IP address and port of the remote channel, the name of the local transport team example, and the local communication address so that the message can be sent to the remote host.

View Plaincopy to Clipboardprint?
/**
* @author fenglb E-mail:[email protected]
* @version creation time: 2009-4-30 04:13:38
* Class Description
*/
Import java.io.IOException;
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;

public class messagebymq{
Define the name of the Queue Manager and queue
private static String Qmname;
private static String QName;
private static Mqqueuemanager qMgr;
static{
Setting the Environment:
Mqenvironment contains static variables that control the composition of the environment in the Mqqueuemanager object, and the value of mqenvironment is set to function when the Mqqueuemanager constructor is loaded.
Therefore, the values in the Mqenvironment must be set before the Mqqueuemanager object is established.
Mqenvironment.hostname= "10.24.1.180"; IP address of the MQ server
Mqenvironment.channel= "S_FENGLB"; Server-connected Channels
mqenvironment.ccsid=1381; The encoding used by the server MQ service 1381 represents GBK, 1208 for UTF (Coded Character Set identifier:ccsid)
mqenvironment.port=1414; MQ Port
Qmname = "QM_FENGLB"; Queue Manager name for MQ
QName = "TESTQ"; Name of the MQ remote queue
try {
Define and initialize the Queue Manager object and connect
Mqqueuemanager can be shared by multiple threads, but it is synchronized when it gets information from MQ, and only one thread can communicate with MQ at any time.
QMGR = new Mqqueuemanager (qmname);
} catch (Mqexception e) {
TODO auto-generated Catch block
System.out.println ("Initial make MQ error");
E.printstacktrace ();
}
}
/**
* Send messages to MQ
* @param message
* @return
*/
public static int sendMessage (String message) {
int result=0;
try{
Set the queue properties that will be connected
Note. 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.
/* Target is a remote queue, all the Mqoo_input_as_q_def properties cannot be used here */
int openoptions = MQC. Mqoo_input_as_q_def | MQC. Mqoo_output;
/* The following options are available for remote queues and local queues */
int openoptions = MQC. Mqoo_output | MQC. mqoo_fail_if_quiescing;
Connection queue
Mqqueue provides inquire, set, put and get operations for WebSphere MQ queues.
The inquire and set capabilities is inherited from Mqmanagedobject.
/* Reopen it when closed */
if (Qmgr==null | |!qmgr.isconnected ()) {
QMGR = new Mqqueuemanager (qmname);
}
Mqqueue queue = Qmgr.accessqueue (QName, openoptions);
To define a simple message
Mqmessage putmessage = new Mqmessage ();
Putting data into the message buffer
PUTMESSAGE.WRITEUTF (message);
Set the properties of the write message (default property)
Mqputmessageoptions PMO = new Mqputmessageoptions ();
Writing messages to the queue
Queue.put (PUTMESSAGE,PMO);
Queue.close ();
}catch (Mqexception ex) {
System.out.println ("A WebSphere MQ error Occurred:completion Code"
+ Ex.completioncode + "Reason Code" + Ex.reasoncode);
Ex.printstacktrace ();
}catch (IOException ex) {
SYSTEM.OUT.PRINTLN ("An error occurred whilst writing to the message buffer:" + ex);
}catch (Exception ex) {
Ex.printstacktrace ();
}finally{
try {
Qmgr.disconnect ();
} catch (Mqexception e) {
E.printstacktrace ();
}
}
return result;
}
/**
* from the queue to get the message, if there is no message in the queue, an exception will occur, but there is no relationship, there is try ... CATCH, if the method is called by a third-party program, no message is indicated if no return
* Third party can place this method in an infinite loop while (true) {...} , there is no need to set the wait, because there is no message inside the method that waits automatically.
* @return
*/
public static String GetMessage () {
String Message=null;
try{
Set the queue properties that will be connected
Note. 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;
Mqmessage retrieve = new Mqmessage ();
Set the properties of the fetch message (default property)
Set the put message options. (Set placement messages option)
Mqgetmessageoptions GMO = new Mqgetmessageoptions ();
Gmo.options = Gmo.options + MQC. Mqgmo_syncpoint;//get messages under Sync Point control (get messages under sync points)
Gmo.options = Gmo.options + MQC.  mqgmo_wait; Wait if no messages on the queue (wait if no messages are on the queues)
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 wait milliseconds limit)
/* Reopen it when closed */
if (Qmgr==null | |!qmgr.isconnected ()) {
QMGR = new Mqqueuemanager (qmname);
}
Mqqueue queue = Qmgr.accessqueue (QName, openoptions);
Remove a message from the queue
Queue.get (retrieve, GMO);
message = Retrieve.readutf ();
SYSTEM.OUT.PRINTLN ("The message is:" + message);
Queue.close ();
}catch (Mqexception ex) {
System.out.println ("A WebSphere MQ error Occurred:completion Code"
+ Ex.completioncode + "Reason Code" + Ex.reasoncode);
}catch (IOException ex) {
SYSTEM.OUT.PRINTLN ("An error occurred whilst writing to the message buffer:" + ex);
}catch (Exception ex) {
Ex.printstacktrace ();
}finally{
try {
Qmgr.disconnect ();
} catch (Mqexception e) {
E.printstacktrace ();
}
}
return message;
}
public static void Main (String args[]) {
/* The following two methods can be used simultaneously or separately */
SendMessage ("This is a Test");
GetMessage ();
}
}

This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/fenglibing/archive/2009/05/08/4161441.aspx

Related Article

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.