Spring3.2.3整合MQ-WS_MQ_V7.5
程序员文章站
2022-05-11 21:57:22
...
记录给需要帮助的人~
step :1 pom.xml
<spring-framework.version>3.2.3.RELEASE</spring-framework.version> <mq.version>7.5.0.3</mq.version> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>com.ibm.mqjms</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>com.ibm.mq.jmqi</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>connector</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>providerutil</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>com.ibm.mq.commonservices</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>com.ibm.mq.defaultconfig</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>dhbcore</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>com.ibm.mq.headers</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>com.ibm.mq.pcf</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>javax.jms</groupId> <artifactId>jms-api</artifactId> <version>1.1-rev-1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring-framework.version}</version> </dependency>
step:2 connectionFactory , DefaultMessageListenerContainer 连接与监听
@Configuration // MQ连接信息的配置文件 @PropertySource("file:${ic.assistant.config.path}/ic-assistant.properties") public class MqConfig{ @Autowired private Environment env; /** * 连接MQ */ @Bean public ConnectionFactory connectionFactory() throws JMSException{ MQConnectionFactory cf = new MQConnectionFactory(); cf.setHostName(env.getRequiredProperty("ic.mq.host")); cf.setPort(Integer.parseInt(env.getRequiredProperty("ic.mq.port"))); cf.setQueueManager(env.getRequiredProperty("ic.mq.queue.manager")); cf.setChannel(env.getRequiredProperty("ic.mq.channel")); cf.setTransportType(1); return cf; } /** * 获得MQ方法监听器 */ @Bean public MessageListener eventListener(){ return new SynListener(); } /** * 配置连接,以及监听的序列,以及需要监听的方法 */ @Bean public DefaultMessageListenerContainer messageListenerContainer() throws JMSException{ DefaultMessageListenerContainer mc = new DefaultMessageListenerContainer(); mc.setConnectionFactory(connectionFactory()); mc.setDestinationName("queue:///"+env.getRequiredProperty("ic.mq.queue")); mc.setMessageListener(eventListener()); mc.setConcurrentConsumers(1); mc.setMaxConcurrentConsumers(2); return mc; }
step:3 实现MessageListener接口,重写onMessage方法
public class SynListener implements MessageListener { public void onMessage(Message message) { if (message instanceof TextMessage) { String msgContext; try { msgContext = ((TextMessage) message).getText(); if(msgContext.indexOf(FinalManager.SBC) >= 0){ synMsgService.saveSubc(msgContext); } else if (msgContext.indexOf(FinalManager.SBD) >= 0) { synMsgService.saveSubd(msgContext); } } catch (JMSException e) { } } } }
step:4 启动服务自动监听MQ