在项目中,我们可能会用到mq消息传递。所以这里咱么就来了解一下IBM MQ。
需要知道几个名字。
- 队列管理器
- 队列
- 通道
还是先上代码把,概念性的东西放在下边,供大家理解。
代码直接上
需要安装IBM WebSphere MQ。安装完启动。如下图所示
安装ibm mq 完成后,查看帮助内容。
创建好后,我们通过spring jms 去跟mq 进行通讯。
需要的maven依赖包
<dependency>
<groupId>com.ibm.mq</groupId>
<artifactId>com.ibm.mq.allclient</artifactId>
<version>9.0.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
Java代码实现发送mq消息 ,并获取mq消息
package com.tzw.util.ibm.mq;
import com.ibm.mq.jms.MQQueue;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import javax.jms.Message;
public class IbmSend {
public static void send() {
try {
MQQueueConnectionFactory mqConnection = new MQQueueConnectionFactory();
mqConnection.setPort(1414);
mqConnection.setHostName("127.0.0.1");
mqConnection.setChannel("SYSTEM.DEF.SVRCONN");
mqConnection.setQueueManager("QM_APPLE");
mqConnection.setCCSID(1381);
MQQueue queue = new MQQueue();
queue.setBaseQueueName("Q1");
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setDefaultDestination(queue);
jmsTemplate.setConnectionFactory(mqConnection);
jmsTemplate.convertAndSend("my name is zhangsan");
System.out.println("发送消息成功");
} catch (Exception e) {
}
}
public static void rec() {
try {
MQQueueConnectionFactory mqConnection = new MQQueueConnectionFactory();
mqConnection.setPort(1414);
mqConnection.setHostName("127.0.0.1");
mqConnection.setChannel("SYSTEM.DEF.SVRCONN");
mqConnection.setQueueManager("QM_APPLE");
mqConnection.setCCSID(1381);
MQQueue queue = new MQQueue();
queue.setBaseQueueName("Q1"