MQ队列经常用于异步传输信息,本篇仅分享java调用mq的经验。
一、名词解释
1、MQ
Message Queue消息队列
2、队列管理器
(Queue Manager):管理队列的系统,实现 网络通信,保证消息安全可靠地传输到目的地。
3、发送队列 send queue
以MQ中间件为主体,发送队列是java代码从MQ中间件读取消息时,所走的队列
4、接收队列 receive queue
以MQ中间件为主体,接收队列是java代码将消息发送到MQ中间件时,所走的队列。
5、CCSID
编码字符集标识
||配置IBM MQ的时候经常涉及到CCSID的问题,其实IBM的官方有详细的说明,抄录一份备用。原文地址:
http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Fdb2%2Frbafzmstsidvals.htm
对于中国境内的服务器来说,其实常用的CCSID有819,1381,1383,1386,1051
819主要用于unix环境,也可用于unix与windows的通讯
1381主要用于windows环境
1383多用于unix下的中文环境
1386主要用于中文环境的unix与windows的通讯
1051用于hp unix平台
6、渠道 channel
队列管理器之间进行消息传输的通道
7、IP、PORT
MQ中间件所在主机的IP以及队列管理器对应的端口PORT
8、MessageId
MessageID唯一地标识了一条消息。
9、CorrelationID
CorrelationID提供了一个消息头,用于将当前的消息和先前的某些消息或应用程序特定的ID关联起来。
二、发布者订阅者模式
顾名思义,就像微信公众号的推送消息运作模式。
当且仅当用户订阅了该公众号,才能接收该公众号推送过来的消息。
这里的公众号就相当于MQ中间件
发布者就是不定期(或定期)向MQ中间件的消息队列中塞入消息的人
订阅者就是用户,或者说叫客户端,不定期(定期)监听指定的消息队列,如果有消息就取出(或增加筛选条件)
ip、port、channel、queue manager、queue name都确定之后,才可从中获取消息。
这里引用一张模型图,简单易懂:
三、java代码调用实现-使用com.ibm.mq
以下是使用com.ibm.mq包路径下的类来调用mq
import java.io.IOException;
import java.util.Hashtable;
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 MQSR{
private static String messageId="";
public static void main(String args[]) {
messageId=""+System.currentTimeMillis();
// messageId="1515404826519";
System.out.println("messageId:"+messageId);
send();
System.out.println("=============================================");
rec();
}
public static void send(){
//定义队列管理器和队列的名称
String qmName = "QM_000";
String qName = "RQ_888";
MQQueueManager qMgr;
Hashtable properties = new Hashtable();
try {
properties.put("hostname", "172.16.3.234");
properties.put("port", new Integer(1416));
properties.put("channel", "DC.SVRCONN"); // SERVER_CON
properties.put("CCSID", new Integer(1381));
properties.put("transport","MQSeries");
// properties.put("UserID", "MUSR_MQADMIN");
// properties.put("password", "123456");
// properties.put("UserID", "Administrator");
// properties.put("password", "Sunyard123");
// Create a connection to the queue manager
qMgr = new MQQueueManager(qmName,properties);
// Set up the options on the queue we wish to open...
int openOptions = 16;
// Now specify the queue that we wish to open,
// and the open options...
MQQueue remoteQ = qMgr.accessQueue(qName, openOptions);
// Define a simple WebSphere MQ message, and write some text in UTF format..
MQMessage putMessage = new MQMessage();
putMessage.messageId=messageId.getBytes();
putMessage.writeUTF("Test"+messageId);
// specify the message options...
MQPutMessageOptions pmo = new MQPutMessageOptions();
// pmo.options+=MQC.MQPMO_NEW_MSG_ID;
// accept the defaults, same as MQPMO_DEFAULT
// put the message on the queue
remoteQ.put(putMessage,pmo);
System.out.println("Message has been input into the Remote Queue");
System.out.println(new String(putMessage.messageId));
// Close the queue...
remoteQ.close();
// Disconnect from the queue manager
qMgr.disconnect();
}catch (MQException ex) {
ex.printStackTrace();
// If an error has occurred in the above, try to identify what went wrong
// Was it a WebSphere MQ error?
System.out.println("A WebSphere MQ error occurred : Completion code " + ex.completionCode +
" Reason code " + ex.reasonCode);
}catch (IOException ex) {
ex.printStackTrace();
// Was it a Java buffer space error?
System.out.println("An error occurred whilst writing to the message buffer: " + ex);
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void rec(){
//定义队列管理器和队列的名称
String qmName = "QM_888";
String qName = "LQ_888";
// String qName = "LQ_888";
MQQueueManager qMgr;
Hashtable properties = new Hashtable();
try {
properties.put("hostname", "172.16.3.234");
properties.put("port", new Integer(1417));
properties.put("channel", "DC.SVRCONN");
properties.put("CCSID", new Integer(1381));
properties.put("transport","MQSeries");
// properties.put("UserID", "Administrator");
// properties.put("password", "Sunyard123");
// Create a connection to the queue manager
qMgr = new MQQueueManager(qmName,properties);
// Set up the options on the queue we wish to open...
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
// Now specify the queue that we wish to open,
// and the open options...
MQQueue remoteQ = qMgr.accessQueue(qName, openOptions);
// Define a simple WebSphere MQ message, and write some text in UTF format..
MQMessage putMessage = new MQMessage();
putMessage.messageId=messageId.getBytes();
// specify the message options...
//Set the put message options.(设置放置消息选项)
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;//Get messages under sync point control(在同步点控制下获取消息)
gmo.options = gmo.options + MQC.MQGMO_WAIT; // Wait if no messages on the Queue(如果在队列上没有消息则等待)
gmo.options = gmo.options + MQC.MQGMO_FAIL_IF_QUIESCING;// Fail if Qeue Manager Quiescing(如果队列管理器停顿则失败)
gmo.waitInterval = 3000 ; // Sets the time limit for the wait.(设置等待的毫秒时间限制)
// accept the defaults, same as MQPMO_DEFAULT
// put the message on the queue
remoteQ.get(putMessage, gmo);
System.out.println("Message has get the Remote Queue");
System.out.println(new String(putMessage.messageId));
System.out.println("rec="+putMessage.readUTF());
// Close the queue...
remoteQ.close();
// Disconnect from the queue manager
qMgr.disconnect();
}catch (MQException ex) {
ex.printStackTrace();
// If an error has occurred in the above, try to identify what went wrong
// Was it a WebSphere MQ error?
System.out.println("A WebSphere MQ error occurred : Completion code " + ex.completionCode +
" Reason code " + ex.reasonCode);
}catch (IOException ex) {
ex.printStackTrace();
// Was it a Java buffer space error?
System.out.println("An error occurred whilst writing to the message buffer: " + ex);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQQueueConnectionFactory;
public class Test {
private QueueConnection conn = null;
public boolean sendFixHeadMsg(String message) throws IOException, JMSException, Exception {
QueueSession sendSession = null;
Queue queue = null;
QueueSender queueSender = null;
try {
if (conn == null) {
conn = newConnection();
}
sendSession = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = sendSession.createQueue("发送队列");
queueSender = sendSession.createSender(queue);
// queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
BytesMessage sendMessage = sendSession.createBytesMessage();
sendMessage.setStringProperty("TRANSID", "");
String msg = "";
sendMessage.writeBytes(msg.getBytes("GBK"));
queueSender.send(sendMessage);
// 保存消息ID,便于在接收端收到对应此ID的消息
} catch (JMSException e) {
e.printStackTrace();
disConnect();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
if (queueSender != null) {
queueSender.close();
}
}
return true;
}
private QueueConnection newConnection() throws Exception {
QueueConnectionFactory factory = new MQQueueConnectionFactory();
((MQQueueConnectionFactory) factory).setQueueManager("");
((MQQueueConnectionFactory) factory).setCCSID(819);
((MQQueueConnectionFactory) factory).setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
((MQQueueConnectionFactory) factory).setChannel("");
((MQQueueConnectionFactory) factory).setHostName("");
((MQQueueConnectionFactory) factory).setPort(15009);
((MQQueueConnectionFactory) factory).setUseConnectionPooling(true);
QueueConnection connection = factory.createQueueConnection();
connection.start();
return connection;
}
private void disConnect() {
try {
conn.stop();
conn.close();
} catch (JMSException e) {
e.printStackTrace();
} finally {
conn = null;
}
}
}
执行main方法,控制台输出如下:
messageId:1522226877027
Message has been input into the Remote Queue
1522226877027
四、java代码调用实现-使用jms
下次再写,东西比较乱。。。