Linux下的IBMMQ使用和配置
隊列管理器相關命令
crtmqm -q QM_TEST 建立默認隊列管理器QM_TEST
strmqm QM_TEST 啓動隊列管理器QM_TEST
runmqsc QM_TEST 運行隊列管理器QM_TEST
endmqm QM_TEST 停止隊列管理器QM_TEST
dltmqm QM_TEST 刪除隊列管理器QM_TEST
1
創建QM_TEST的隊列管理器隊列
建立默認隊列管理器QM_TEST
[mqm@hadoop1 bin]$ crtmqm -q QM_TEST
WebSphere MQ queue manager created.
Directory '/var/mqm/qmgrs/QM_TEST' created.
The queue manager is associated with installation 'Installation1'.
Creating or replacing default objects for queue manager 'QM_TEST'.
Default objects statistics : 79 created. 0 replaced. 0 failed.
Completing setup.
Setup completed.
查看隊列狀態
// Ended未啓用
[mqm@hadoop1 bin]$ dspmq
QMNAME(QM_TEST) STATUS(Ended immediately)
啓動隊列管理器
[mqm@hadoop1 bin]$ strmqm QM_TEST
WebSphere MQ queue manager 'QM_TEST' starting.
The queue manager is associated with installation 'Installation1'.
5 log records accessed on queue manager 'QM_TEST' during the log replay phase.
Log replay for queue manager 'QM_TEST' complete.
Transaction manager state recovered for queue manager 'QM_TEST'.
WebSphere MQ queue manager 'QM_TEST' started using V8.0.0.4.
運行隊列管理器
[mqm@hadoop1 bin]$ runmqsc QM_TEST
5724-H72 (C) Copyright IBM Corp. 1994, 2015.
Starting MQSC for queue manager QM_TEST.
定義本地隊列
ql:定義的本地隊列名 maxdepth:消息最大接收,defpsist 隊列重啓消息不丟
def ql(QUEUE_RES) maxdepth(10000) defpsist(yes) replace
1 : def ql(QUEUE_RES) maxdepth(10000) defpsist(yes) replace
AMQ8006: WebSphere MQ queue created.
定義服務器連接通道
// define channel(CH1) chltype(SVRCONN) trptype(TCP) mcauser(‘mqm’)
DEFINE CHANNEL(CONN_CHANNEL) CHLTYPE(SVRCONN) TRPTYPE(TCP)
1 : DEFINE CHANNEL(CONN_CHANNEL) CHLTYPE(SVRCONN) TRPTYPE(TCP)
AMQ8014: WebSphere MQ channel created.
// 開啓通道
start chl(CONN_CHANNEL)
1 : start chl(CONN_CHANNEL)
AMQ8018: Start WebSphere MQ channel accepted.
輸入end退出隊列管理器控制檯。
測試發送和接收消息
發送消息
// amqsput queue_name queue_manager_name 發送消息
[mqm@hadoop1 bin]$ amqsput QUEUE_RES QM_TEST
Sample AMQSPUT0 start
target queue is QUEUE_RES
// 輸入字符串後,敲回車鍵結束文字輸入
hello ibmmq
Sample AMQSPUT0 end
接收消息
// amqsput queue_name queue_manager_name 發送消息
[mqm@hadoop1 bin]$ amqsget QUEUE_RES QM_TEST
Sample AMQSGET0 start
message <hello ibmmq>
啓動端口監聽(即ibmmq設置端口)
[mqm@hadoop1 bin]$ runmqlsr -m QM_TEST -p 1414 -t tcp &
[1] 2167
[mqm@hadoop1 bin]$ 5724-H72 (C) Copyright IBM Corp. 1994, 2015.
// 查看1414端口是否啓用
[mqm@hadoop1 bin]$ netstat -nltp | grep 1414
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp6 0 0 :::1414 :::* LISTEN 2167/runmqlsr
1
其他設置修改(需要運行隊列管理器窗口)
查看隊列管理器字符集:
dis qmgr
修改字符集:
alter qmgr CCSID(字符集號)
顯示隊列管理器中的所有隊列:
dis q(*)
查詢所有通道
display chl(*)
1
java代碼進行消息的生產和消費
導入MQ安裝目錄下的依賴包,具體代碼如下面所示:
import com.ibm.mq.*;
import org.junit.Before;
import org.junit.Test;
public class IBMMq {
// 隊列管理器
public static MQQueueManager QMGR;
// 隊列
public static MQQueue QUEUE;
// 請求隊列即 接收隊列、本地隊列
static int reqOpenOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE;
@Test
public void sendMessage() throws Exception {
// 要寫入隊列的消息
MQMessage msg = new MQMessage();
msg.format = MQC.MQFMT_STRING;
msg.characterSet = 1381;
msg.encoding = 1381;
MQPutMessageOptions gmo = new MQPutMessageOptions();
// 設置消息用不過期
msg.expiry = -1;
msg.writeString("hello ibmmq");
QUEUE.put(msg,gmo);
System.out.println("發送消息成功");
QUEUE.close();
}
@Test
public void receiveMsg() throws Exception{
int currentDepth = QUEUE.getCurrentDepth();
System.out.println("消息長度:"+currentDepth);
MQMessage msg = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();
// 本次接收失敗,應該繼續循環
QUEUE.get(msg, gmo);
// 接收成功後,啓動handler處理接收後的消息
String reqMsgStr = msg.readStringOfByteLength(msg.getDataLength());
System.out.println(reqMsgStr);
}
/**
* ibmMq連接初始化,這裏連接的要和上面你配置的Mq對象名稱一致
*/
@Before
public void initConn() throws Exception {
MQEnvironment.hostname = "10.211.55.11";
MQEnvironment.port = 1414; // TCP/IP port
MQEnvironment.channel = "test_conn";
MQEnvironment.CCSID = 1381;
// 用戶名對應的密碼
MQEnvironment.userID = "MUSR_MQADMIN";
// 默認密碼爲123456
MQEnvironment.password = "123456";
QMGR = new MQQueueManager("Queue_Manager");
QUEUE = QMGR.accessQueue("test_receive",
MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_INQUIRE | MQC.MQOO_OUTPUT);
// System.out.println(QUEUE);
}
}