你的位置:首页 > Java教程

[Java教程]Spring3.2.3整合MQ


记录给需要帮助的人~

step :1 pom.Java代码  收藏代码

  1. <spring-framework.version>3.2.3.RELEASE</spring-framework.version>  
  2. <mq.version>7.5.0.3</mq.version>  
  3.         <dependency>  
  4.             <groupId>com.ibm.mq</groupId>  
  5.             <artifactId>com.ibm.mqjms</artifactId>  
  6.             <version>${mq.version}</version>  
  7.         </dependency>  
  8.         <dependency>  
  9.             <groupId>com.ibm.mq</groupId>  
  10.             <artifactId>com.ibm.mq.jmqi</artifactId>  
  11.             <version>${mq.version}</version>  
  12.         </dependency>  
  13.         <dependency>  
  14.             <groupId>com.ibm.mq</groupId>  
  15.             <artifactId>connector</artifactId>  
  16.             <version>${mq.version}</version>  
  17.         </dependency>  
  18.         <dependency>  
  19.             <groupId>com.ibm.mq</groupId>  
  20.             <artifactId>providerutil</artifactId>  
  21.             <version>${mq.version}</version>  
  22.         </dependency>  
  23.         <dependency>  
  24.             <groupId>com.ibm.mq</groupId>  
  25.             <artifactId>com.ibm.mq.commonservices</artifactId>  
  26.             <version>${mq.version}</version>  
  27.         </dependency>  
  28.         <dependency>  
  29.             <groupId>com.ibm.mq</groupId>  
  30.             <artifactId>com.ibm.mq.defaultconfig</artifactId>  
  31.             <version>${mq.version}</version>  
  32.         </dependency>  
  33.         <dependency>  
  34.             <groupId>com.ibm.mq</groupId>  
  35.             <artifactId>dhbcore</artifactId>  
  36.             <version>${mq.version}</version>  
  37.         </dependency>  
  38.         <dependency>  
  39.             <groupId>com.ibm.mq</groupId>  
  40.             <artifactId>com.ibm.mq.headers</artifactId>  
  41.             <version>${mq.version}</version>  
  42.         </dependency>  
  43.         <dependency>  
  44.             <groupId>com.ibm.mq</groupId>  
  45.             <artifactId>com.ibm.mq.pcf</artifactId>  
  46.             <version>${mq.version}</version>  
  47.         </dependency>  
  48.   
  49.         <dependency>  
  50.             <groupId>javax.jms</groupId>  
  51.             <artifactId>jms-api</artifactId>  
  52.             <version>1.1-rev-1</version>  
  53.         </dependency>  
  54.   
  55.         <dependency>  
  56.             <groupId>org.springframework</groupId>  
  57.             <artifactId>spring-jms</artifactId>  
  58.             <version>${spring-framework.version}</version>  
  59.         </dependency>  

 step:2 connectionFactory , DefaultMessageListenerContainer 连接与监听

Java代码  收藏代码

  1. @Configuration  
  2. // MQ连接信息的配置文件  
  3. @PropertySource("file:${ic.assistant.config.path}/ic-assistant.properties")  
  4. public class MqConfig{  
  5.       
  6.     @Autowired  
  7.     private Environment env;  
  8.       
  9.     /** 
  10.      * 连接MQ 
  11.      */  
  12.     @Bean  
  13.     public ConnectionFactory connectionFactory() throws JMSException{  
  14.         MQConnectionFactory cf = new MQConnectionFactory();  
  15.         cf.setHostName(env.getRequiredProperty("ic.mq.host"));  
  16.         cf.setPort(Integer.parseInt(env.getRequiredProperty("ic.mq.port")));  
  17.         cf.setQueueManager(env.getRequiredProperty("ic.mq.queue.manager"));  
  18.         cf.setChannel(env.getRequiredProperty("ic.mq.channel"));  
  19.         cf.setTransportType(1);  
  20.         return cf;  
  21.     }  
  22.       
  23.     /** 
  24.      * 获得MQ方法监听器 
  25.      */  
  26.     @Bean  
  27.     public MessageListener eventListener(){  
  28.         return new SynListener();  
  29.     }  
  30.       
  31.     /** 
  32.      *  配置连接,以及监听的序列,以及需要监听的方法 
  33.      */  
  34.     @Bean  
  35.     public DefaultMessageListenerContainer messageListenerContainer() throws JMSException{  
  36.         DefaultMessageListenerContainer mc = new DefaultMessageListenerContainer();  
  37.         mc.setConnectionFactory(connectionFactory());  
  38.         mc.setDestinationName("queue:///"+env.getRequiredProperty("ic.mq.queue"));  
  39.         mc.setMessageListener(eventListener());  
  40.         mc.setConcurrentConsumers(1);  
  41.         mc.setMaxConcurrentConsumers(2);  
  42.         return mc;  
  43.     }  
  44.       

 step:3 实现MessageListener接口,重写onMessage方法

Java代码  收藏代码

  1. public class SynListener implements MessageListener {  
  2.   
  3.     public void onMessage(Message message) {  
  4.         if (message instanceof TextMessage) {  
  5.             String msgContext;  
  6.             try {  
  7.                 msgContext = ((TextMessage) message).getText();  
  8.                 if(msgContext.indexOf(FinalManager.SBC) >= 0){  
  9.                     synMsgService.saveSubc(msgContext);  
  10.                 } else if (msgContext.indexOf(FinalManager.SBD) >= 0) {  
  11.                     synMsgService.saveSubd(msgContext);  
  12.                 }  
  13.             } catch (JMSException e) {  
  14.             }  
  15.         }  
  16.     }  
  17. }  

 step:4 启动服务自动监听MQ

》》源码地址获取

springmvc + mybatis整合详细,及遇到的问题请参看以下资料:

参考资料:

http://www.springmvc,net/detail/6074493.html

http://my.spring.net/wangbiglei/blog/489583

http://my.springmvc.net/wangbiglei/blog/489604