下载
链接: https://pan.baidu.com/s/120J_GTax-M_d5z-Q9ZDTLA 密码: xans
安装
在虚拟机里面安装的
没有什么域控制器
否就可以了
- 新建队列管理器
- 新建队列
- 新建服务器链接通道
-修改链接端口
- 新建用户
- 结果
- 代码
public class Mq {
static MQQueueManager qMgr;
// 队列名称
static String queueString = "one";
// 编码字符集标识
static int CCSID = 1381;
public static void connect() throws MQException {
MQEnvironment.hostname = "192.168.10.155";
// 服务器链接通道
MQEnvironment.channel = "scc";
MQEnvironment.port = 1414;
MQEnvironment.CCSID = CCSID;
MQEnvironment.userID = "root";
MQEnvironment.password = "root";
// 队列管理器名称
qMgr = new MQQueueManager("qm");
}
public static void main(String[] args) {
try {
connect();
for (int i = 0; i < 10; i++) {
String str = String.valueOf(new Random().nextInt(111111111));
sendMsg(str);
}
getMsg();
} catch (MQException e) {
e.printStackTrace();
}
}
public static void getMsg() {
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE;
MQQueue queue = null;
try {
queue = qMgr.accessQueue(queueString, openOptions, null, null, null);
System.out.println("===========================");
System.out.println("该队列当前的深度为:" + queue.getCurrentDepth());
System.out.println("===========================");
int depth = queue.getCurrentDepth();
// 将队列的里的消息读出来
while (depth-- > 0) {
MQMessage msg = new MQMessage();// 要读的队列的消息
MQGetMessageOptions gmo = new MQGetMessageOptions();
queue.get(msg, gmo);
System.out.println("消息的大小为:" + msg.getDataLength());
System.out.println("消息的内容:" + msg.readStringOfByteLength(msg.getDataLength()));
System.out.println("---------------------------");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (queue != null) {
try {
queue.close();
qMgr.disconnect();
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void sendMsg(String msgStr) {
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE;
MQQueue queue = null;
try {
// 建立Q1通道的连接
queue = qMgr.accessQueue(queueString, openOptions, null, null, null);
MQMessage msg = new MQMessage();// 要写入队列的消息
msg.format = MQC.MQFMT_STRING;
msg.characterSet = CCSID;
msg.encoding = CCSID;
// msg.writeObject(msgStr); //将消息写入消息对象中
msg.writeString(msgStr);
MQPutMessageOptions pmo = new MQPutMessageOptions();
msg.expiry = -1; // 设置消息用不过期
queue.put(msg, pmo);// 将消息放入队列
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (queue != null) {
try {
queue.close();
// qMgr.disconnect();
System.out.println("写入的消息为:" + msgStr);
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
- pom依赖
<dependency>
<groupId>com.ibm.mq</groupId>
<artifactId>com.ibm.mq.allclient</artifactId>
<version>9.0.5.0</version>
</dependency>
这是完成之后才写的,如有疏漏,请留言指证,不胜感激
十分感谢前辈的博客
https://www.ibm.com/support/knowledgecenter/zh/SSFKSJ_8.0.0/com.ibm.mq.explorer.doc/bi00256_.htm
https://blog.csdn.net/qq_17616169/article/details/54633005
https://www.cnblogs.com/swugogo/p/5917677.html
https://blog.csdn.net/haitaofeiyang/article/details/46897137
有些时候的首要任务是完成工作任务
未必有深入研究的原理的时间
或许这个时候是考验解决问题的能力吧
所以这次也仅仅只是。。。