websphere MQ3

大綱:



Creating the queue manager

Creating the local queue

Putting a test message on the local queue

Verifying that the test message was sent


1.Creating the queue manager using WebSphere MQ Explorer
Start WebSphere MQ Explorer.
In the Navigator view, right-click the Queue Managers folder, then click New > Queue Manager. The Create Queue Manager wizard opens.
In the Queue Manager name field, type QM_APPLE.
Select the Make this the default queue manager check box.
Click Next twice to go to Step 3 of the wizard.
Ensure that Auto Start Queue Manager is selected.
Click Next to go to Step 4 of the wizard.
Ensure that the Create listener configured for TCP/IP check box is selected.
If the Finish button is not available, type another port number in the Listen on port number field. If the current value is 1414, try typing 1415 or 1416.
Click Finish.
Results
An icon representing this queue manager is displayed in the Queue Managers folder in the Navigator view of WebSphere MQ Explorer, and the queue manager automatically starts running after you create it, as shown in the following screen capture:


Creating the queue manager using MQSC
About this task
Open a command prompt, and follow these steps:
Create a default queue manager called QM_APPLE by typing the command:
crtmqm -q QM_APPLEMessages tell you that the queue has been created and that the default WebSphere MQ objects have been created.
Start this queue manager by typing the command:
strmqm

A message tells you when the queue manager has started.
Results
You have now created a queue manager with the name QM_APPLE. The next task is to create a local queue that this queue manager will manage.


2.Creating the local queue using WebSphere MQ Explorer
In the Navigator view, expand the Queue Managers folder.
Expand queue manager QM_APPLE.
Right click the Queues folder, then click New > Local Queue... The New Local Queue wizard opens.
In the Name field, type Q1
Click Finish.
Results
The new queue, Q1, is displayed in the Content view, as displayed in the following screen capture:


If the queue is not displayed in the Content view, click Refresh at the top of the Content view.

Creating the local queue using MQSC
About this task
Open a command prompt and follow these steps:
Enable MQSC commands by typing the command:
runmqsc

Type the following command:
define qlocal (Q1)

Messages tell you that the queue has been created and that the default WebSphere MQ objects have been created.
Stop MQSC by typing the command:
end

Results
You have now created a local queue called Q1. The next task is to put a test message to this newly created local queue.




3.Putting a test message on the queue using WebSphere MQ Explorer
在Eclipse 中,創建如下類:
[java] view plaincopyprint?package com.ibm.test;

import java.io.IOException;

import com.ibm.mq.MQC;
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 MQSample {
//定義隊列管理器和隊列的名稱
private static String qmName;
private static String qName;

/**
* @param args
*/
public static void main(String[] args) {

qmName ="QM_APPLE";
qName = "Q1";
System.out.println("QManager:"+qmName);
System.out.println("QueueName:"+qName);
try {
//定義並初始化隊列管理器對象並連接
MQQueueManager qMgr = new MQQueueManager(qmName);

// 設置將要連接的隊列屬性
// Note. All WebSphere MQ Options are prefixed with MQC in Java.
@SuppressWarnings("deprecation")
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;

//連接隊列
MQQueue localQ = qMgr.accessQueue(qName, openOptions);

//定義一個簡單的消息
MQMessage putMessage = new MQMessage();
putMessage.writeUTF("Hello World!");

//設置寫入消息的屬性(默認屬性)
MQPutMessageOptions pmo = new MQPutMessageOptions();

//將消息寫入隊列
localQ.put(putMessage,pmo);

// MQMessage retrievedMessage = new MQMessage();
// retrievedMessage.messageId = putMessage.messageId;
//
// //設置取出消息的屬性(默認屬性)
// MQGetMessageOptions gmo = new MQGetMessageOptions();
//
// // 從隊列中取出消息
// localQ.get(retrievedMessage, gmo);
// String msgText = retrievedMessage.readUTF();

// System.out.println("The message is: " + msgText);

//關閉隊列
localQ.close();
//從隊列管理器斷開
qMgr.disconnect();
}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();
}

}

}

package com.ibm.test;

import java.io.IOException;

import com.ibm.mq.MQC;
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 MQSample {
//定義隊列管理器和隊列的名稱
private static String qmName;
private static String qName;

/**
* @param args
*/
public static void main(String[] args) {

qmName ="QM_APPLE";
qName = "Q1";
System.out.println("QManager:"+qmName);
System.out.println("QueueName:"+qName);
try {
//定義並初始化隊列管理器對象並連接
MQQueueManager qMgr = new MQQueueManager(qmName);

// 設置將要連接的隊列屬性
// Note. All WebSphere MQ Options are prefixed with MQC in Java.
@SuppressWarnings("deprecation")
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;

//連接隊列
MQQueue localQ = qMgr.accessQueue(qName, openOptions);

//定義一個簡單的消息
MQMessage putMessage = new MQMessage();
putMessage.writeUTF("Hello World!");

//設置寫入消息的屬性(默認屬性)
MQPutMessageOptions pmo = new MQPutMessageOptions();

//將消息寫入隊列
localQ.put(putMessage,pmo);

// MQMessage retrievedMessage = new MQMessage();
// retrievedMessage.messageId = putMessage.messageId;
//
// //設置取出消息的屬性(默認屬性)
// MQGetMessageOptions gmo = new MQGetMessageOptions();
//
// // 從隊列中取出消息
// localQ.get(retrievedMessage, gmo);
// String msgText = retrievedMessage.readUTF();

// System.out.println("The message is: " + msgText);

//關閉隊列
localQ.close();
//從隊列管理器斷開
qMgr.disconnect();
}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();
}

}

}
編譯時需要添加Websphere MQ相關類。


4.Verifying that the test message was sent using WebSphere MQ Explorer
In the Navigator view, expand the Queue Managers folder, then expand QM_APPLE.
Click the Queues folder.
In the Content view, right-click Q1, then click Browse Messages.... The Message browser opens to show the list of the messages that are currently on Q1.
Double-click the last message to open its properties dialog.
Results
On the Data page of the properties dialog, the Message data field displays the content of the message in human-readable form, as shown in the following screen capture:


Verifying that the test message was sent using MQSC
About this task
The amqsget sample program is used to get the message back from the queue.
Open a command prompt and follow these steps:

Start the amqsget sample program:
On Windows®, type the following command:amqsget Q1
On Linux®, change to the /opt/mqm/samp/bin directory and type the following command: ./amqsget Q1
Results
The sample program starts, and your message is displayed along with any other messages on this queue. After a pause of 15 seconds, the sample ends and the command prompt is displayed again.

Congratulations! You have now completed this tutorial.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章