zoukankan      html  css  js  c++  java
  • Spring mvc4 + ActiveMQ 整合

    1导入pom夹包

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>web</groupId>
        <artifactId>SpringMQ</artifactId>
        <packaging>war</packaging>
        <version>0.0.1-SNAPSHOT</version>
        <name>SpringMQ Maven Webapp</name>
        <url>http://maven.apache.org</url>
        <!-- 版本管理 -->
        <properties>
            <springframework>4.1.8.RELEASE</springframework>
            <javax.servlet>3.1.0</javax.servlet>
        </properties>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.10</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
    
            <!-- spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${springframework}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${springframework}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${springframework}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${springframework}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jms</artifactId>
                <version>${springframework}</version>
            </dependency>
            <!-- xbean 如<amq:connectionFactory /> -->
            <dependency>
                <groupId>org.apache.xbean</groupId>
                <artifactId>xbean-spring</artifactId>
                <version>3.16</version>
            </dependency>
    
            <!-- activemq -->
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-core</artifactId>
                <version>5.7.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-pool</artifactId>
                <version>5.12.1</version>
            </dependency>
        </dependencies>
        <build>
            <finalName>SpringMQ</finalName>
        </build>
    </project>

    2.配置activemq.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:amq="http://activemq.apache.org/schema/core"
        xmlns:jms="http://www.springframework.org/schema/jms"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
            http://www.springframework.org/schema/jms
            http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
            http://activemq.apache.org/schema/core
            http://activemq.apache.org/schema/core/activemq-core-5.12.1.xsd"
            >
    <!--     开启扫 -->
        <context:component-scan base-package="com.gzframe.demo.activemq" />
    <!--     注解 -->
        <mvc:annotation-driven />
            
    <!--         配置amq -->
        <amq:connectionFactory id="amqConnectionFactory" 
            brokerURL="tcp://localhost:61616" 
            userName="admin" 
            password="admin" />
        
        <!-- 配置JMS连接工厂 -->
        <bean id="connectionFactory"
            class="org.springframework.jms.connection.CachingConnectionFactory">
            <constructor-arg ref="amqConnectionFactory" />
            <property name="sessionCacheSize" value="100" />
        </bean>
        
        <!-- 定义消息队列(Queue) -->
        <bean id="demoQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
            <!-- 设置消息队列的名字 -->
            <constructor-arg>
                <value>gzframe.demo</value>
            </constructor-arg>
        </bean>
        
        <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
        <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
            <property name="connectionFactory" ref="connectionFactory" />
            <property name="defaultDestination" ref="demoQueueDestination" />
            <property name="receiveTimeout" value="10000" />
            <!-- true是topic,false是queue,默认是false,此处显示写出false -->
            <property name="pubSubDomain" value="false" />
        </bean>
        
     </beans>

    3.接收消息ConsumerService.java

    package com.gzframe.demo.activemq.consumer;
    
    import javax.annotation.Resource;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.TextMessage;
    
    import org.springframework.jms.core.JmsTemplate;
    import org.springframework.stereotype.Service;
    
    
    @Service
    public class ConsumerService {
    
        @Resource(name="jmsTemplate")
        private JmsTemplate jmsTemplate;
         
        /**
         * 接收消息
         */
        public TextMessage receive(Destination destination) {
            TextMessage tm = (TextMessage) jmsTemplate.receive(destination);
            try {
                System.out.println("从队列" + destination.toString() + "收到了消息:	"
                        + tm.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
            
            return tm;
            
        }
     
    }

    4.发送消息ProducerService.java

    package com.gzframe.demo.activemq.producer;
    
    import javax.annotation.Resource;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.Session;
    
    import org.springframework.jms.core.JmsTemplate;
    import org.springframework.jms.core.MessageCreator;
    import org.springframework.stereotype.Service;
    
    @Service
    public class ProducerService {
    
        @Resource(name="jmsTemplate")
        private JmsTemplate jmsTemplate;
           
          /**
           * 向指定队列发送消息
           */
          public void sendMessage(Destination destination, final String msg) {
            System.out.println("向队列" + destination.toString() + "发送了消息------------" + msg);
            jmsTemplate.send(destination, new MessageCreator() {
              public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(msg);
              }
            });
          }
         
        /**
         * 向默认队列发送消息
         */
          public void sendMessage(final String msg) {
            String destination =  jmsTemplate.getDefaultDestination().toString();
            System.out.println("向队列" +destination+ "发送了消息------------" + msg);
            jmsTemplate.send(new MessageCreator() {
              public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(msg);
              }
            });
         
          }
        
    }

    6.controller

    package com.gzframe.demo.mvc.controller;
    
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.*;
    
    import javax.annotation.Resource;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.TextMessage;
    import javax.management.MBeanServerConnection;
    import javax.management.remote.JMXConnector;
    import javax.management.remote.JMXConnectorFactory;
    import javax.management.remote.JMXServiceURL;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.gzframe.demo.activemq.consumer.ConsumerService;
    import com.gzframe.demo.activemq.producer.ProducerService;
    
    @Controller
    public class DemoController {
    
    
        //队列名gzframe.demo
        @Resource(name="demoQueueDestination")
        private Destination demoQueueDestination;
    
        //队列消息生产者
        @Resource(name="producerService")
        private ProducerService producer;
        
      //队列消息消费者
        @Resource(name="consumerService")
        private ConsumerService consumer;
        
        @RequestMapping(value="/producer",method=RequestMethod.GET)
        public ModelAndView producer(){
            System.out.println("------------go producer");
            
            Date now = new Date(); 
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = dateFormat.format( now ); 
            System.out.println(time);
            
            ModelAndView mv = new ModelAndView();
            mv.addObject("time", time);
            mv.setViewName("jms_producer");        
            return mv;
        }
        
        @RequestMapping(value="/onsend",method=RequestMethod.POST)
        public ModelAndView producer(@RequestParam("message") String message) {
            System.out.println("------------send to jms");
            ModelAndView mv = new ModelAndView();
            producer.sendMessage(demoQueueDestination, message);
            mv.setViewName("welcome");
            return mv;
        }
        
        @RequestMapping(value="/receive",method=RequestMethod.GET)
        public ModelAndView queue_receive() throws JMSException {
            System.out.println("------------receive message");
            ModelAndView mv = new ModelAndView();
            
            TextMessage tm = consumer.receive(demoQueueDestination);
            mv.addObject("textMessage", tm.getText());
            
            mv.setViewName("queue_receive");
            return mv;
        }
        
        /*
         * ActiveMQ Manager Test
         */
        @RequestMapping(value="/jms",method=RequestMethod.GET)
        public ModelAndView jmsManager() throws IOException {
            System.out.println("------------jms manager");
            ModelAndView mv = new ModelAndView();
            mv.setViewName("welcome");
            
            JMXServiceURL url = new JMXServiceURL("");
            JMXConnector connector = JMXConnectorFactory.connect(url);
            connector.connect();
            MBeanServerConnection connection = connector.getMBeanServerConnection();
            
            return mv;
        }
        
        
        
    }
  • 相关阅读:
    C# 图像处理(图像缩放、屏幕截取、图像合并、保存图像)
    C# string ASCII相互转换
    BitNami Redmine Stack迁移
    win10开始菜单打不开怎么办?
    (.text+0x18): undefined reference to `main'
    CUDA 计算pi (π)
    C++ 对TXT 的串并行读写
    Matlab 与 c++对txt 文档的读写格式
    Git 初级使用 windows & Ubuntu
    Leetcode 题解 Remove Duplicates from Sorted List
  • 原文地址:https://www.cnblogs.com/javaweb2/p/6640886.html
Copyright © 2011-2022 走看看