标签:tcp man 文本 png data- 浏览器 ota ccs 缺省
#简介
IBM MQ(IBM Message Queue)是IBM的一款商业消息中间产品,适用于分布式计算环境或异构系统之中。
消息队列技术是分布式应用间交换信息的一种技术。消息队列可驻留在内存或磁盘上,队列存储消息直到它们被应用程序读走。
#安装
1、官网下载地址:https://www.ibm.com/support/pages/node/318077
2、下载完成之后,创建一个用户,选择组为mqm,如下图所示:
#配置MQ
#创建队列管理器
#创建本地队列
#将测试消息放入队列
#验证是否已发送测试消息
#创建服务器连接通道
#检查侦听器
#集成指南
#pom配置
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>javax.jms</groupId> <artifactId>javax.jms-api</artifactId> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>com.ibm.mq.allclient</artifactId> </dependency>
#配置文件
mq: ibm: host: 127.0.0.1 #侦听器端口号 port: 8888 #(队列管理器名称) queue-manager: QM_JACK #(队列名称) queue-name: QUEUE_RECV #(通道名称) channel: CNN_JACK #创建的MQ用户 username: Administrator #创建的MQ用户连接密码 password: 123 receive-timeout: 20000
#配置类
import com.ibm.mq.jms.MQQueueConnectionFactory; import com.ibm.msg.client.wmq.WMQConstants; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jms.connection.CachingConnectionFactory; import org.springframework.jms.connection.JmsTransactionManager; import org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter; import org.springframework.jms.core.JmsOperations; import org.springframework.jms.core.JmsTemplate; import org.springframework.transaction.PlatformTransactionManager; @Configuration public class JmsConfig { @Value("${mq.ibm.host}") private String host; @Value("${mq.ibm.port}") private Integer port; @Value("${mq.ibm.queue-manager}") private String queueManager; @Value("${mq.ibm.channel}") private String channel; @Value("${mq.ibm.username}") private String username; @Value("${mq.ibm.password}") private String password; @Value("${mq.ibm.receive-timeout}") private long receiveTimeout; @Bean public MQQueueConnectionFactory mqQueueConnectionFactory() { MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory(); mqQueueConnectionFactory.setHostName(host); try { mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT); mqQueueConnectionFactory.setCCSID(1208); mqQueueConnectionFactory.setChannel(channel); mqQueueConnectionFactory.setPort(port); mqQueueConnectionFactory.setQueueManager(queueManager); } catch (Exception e) { e.printStackTrace(); } return mqQueueConnectionFactory; } @Bean UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(MQQueueConnectionFactory mqQueueConnectionFactory) { UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter(); userCredentialsConnectionFactoryAdapter.setUsername(username); userCredentialsConnectionFactoryAdapter.setPassword(password); userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(mqQueueConnectionFactory); return userCredentialsConnectionFactoryAdapter; } @Bean @Primary public CachingConnectionFactory cachingConnectionFactory(UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter) { CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(); cachingConnectionFactory.setTargetConnectionFactory(userCredentialsConnectionFactoryAdapter); cachingConnectionFactory.setSessionCacheSize(500); cachingConnectionFactory.setReconnectOnException(true); return cachingConnectionFactory; } @Bean public PlatformTransactionManager jmsTransactionManager(CachingConnectionFactory cachingConnectionFactory) { JmsTransactionManager jmsTransactionManager = new JmsTransactionManager(); jmsTransactionManager.setConnectionFactory(cachingConnectionFactory); return jmsTransactionManager; } @Bean public JmsOperations jmsOperations(CachingConnectionFactory cachingConnectionFactory) { JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory); jmsTemplate.setReceiveTimeout(receiveTimeout); return jmsTemplate; } }
#发送消息
import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.jms.core.JmsOperations; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @Slf4j @Component public class SendMessage { @Autowired JmsOperations jmsOperations; @Value("${mq.ibm.queue-name}") String queueName; @PostConstruct public void send() { log.info("开始发送IBM WebSphere MQ消息..."); jmsOperations.convertAndSend(queueName, "这是一个IBM WebSphere MQ消息"); } }
#接收消息
import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.annotation.JmsListener; import org.springframework.jms.core.JmsOperations; import org.springframework.jms.listener.adapter.MessageListenerAdapter; import org.springframework.stereotype.Component; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.TextMessage; @Slf4j @Component public class ReceiveMessage extends MessageListenerAdapter { @Autowired JmsOperations jmsOperations; @Override @JmsListener(destination = "${mq.ibm.queue-name}") public void onMessage(Message message) { log.info("接收到IBM WebSphere MQ消息:{}", message.toString()); try { log.info("IBM WebSphere MQ消息内容为:{}", ((TextMessage) message).getText()); } catch (JMSException e) { e.printStackTrace(); } } }
标签:tcp man 文本 png data- 浏览器 ota ccs 缺省
原文地址:https://www.cnblogs.com/lushichao/p/13046437.html