Spring3.2.3整合MQ-WS_MQ_V7.5

阅读更多

记录给需要帮助的人~

step :1 pom.xml

3.2.3.RELEASE
7.5.0.3
        
			com.ibm.mq
			com.ibm.mqjms
			${mq.version}
		
		
			com.ibm.mq
			com.ibm.mq.jmqi
			${mq.version}
		
		
			com.ibm.mq
			connector
			${mq.version}
		
		
			com.ibm.mq
			providerutil
			${mq.version}
		
		
			com.ibm.mq
			com.ibm.mq.commonservices
			${mq.version}
		
		
			com.ibm.mq
			com.ibm.mq.defaultconfig
			${mq.version}
		
		
			com.ibm.mq
			dhbcore
			${mq.version}
		
		
			com.ibm.mq
			com.ibm.mq.headers
			${mq.version}
		
		
			com.ibm.mq
			com.ibm.mq.pcf
			${mq.version}
		

		
			javax.jms
			jms-api
			1.1-rev-1
		

		
			org.springframework
			spring-jms
			${spring-framework.version}
		

 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

 

你可能感兴趣的:(MQ,Spring3.2.3,监听,定时,消息)