基于Maven,Spring+ActiveMQ实现,贴近实际

本文只实现了Topic,queue改点配置就行了

一、pom依赖

Spring的太长了,具体可以看下面源码里面

            <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-core</artifactId>
            <version>5.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq.protobuf</groupId>
            <artifactId>activemq-protobuf</artifactId>
            <version>1.1</version>
            <type>maven-plugin</type>
        </dependency>
        <dependency>
            <groupId>org.apache.xbean</groupId>
            <artifactId>xbean-spring</artifactId>
            <version>4.4</version>
        </dependency>

二、目录结构

三、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>ActiveMQ</display-name>

    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter
        </filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <!--扫描配置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:conf/spring/application-*.xml
        </param-value>
    </context-param>
    
    <servlet>
        <servlet-name>Spring-Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:conf/spring/springMvc-context.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring-Servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
View Code

四、生产者类

package com.activeMq.service;


import org.springframework.jms.core.JmsTemplate;

public class SendTopic {

    
    private JmsTemplate template;
    
    private String destination;
    
    

    private  String node;

    private String allNodeStr;

    
    public void sendNode(String message){
        
        template.convertAndSend(node,message);
    }
    
    public void sendAllNodes(String message){
        
        for(String node:allNodeStr.split(",")){
            
            template.convertAndSend(node,message);
        }
        
    }

    public JmsTemplate getTemplate() {
        return template;
    }

    public void setTemplate(JmsTemplate template) {
        this.template = template;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public String getNode() {
        return node;
    }

    public void setNode(String node) {
        this.node = node;
    }

    public String getAllNodeStr() {
        return allNodeStr;
    }

    public void setAllNodeStr(String allNodeStr) {
        this.allNodeStr = allNodeStr;
    }
    
    
    
    
}
View Code

五、消费者类

package com.activeMq.service;

import javax.jms.Message;
import javax.jms.MessageListener;


public class ReceiveMessage implements MessageListener{
    private String destName;

    public void setDestName(String destName) {
        this.destName = destName;
    }

    public void onMessage(Message arg0) {
        // TODO Auto-generated method stub
        System.out.println(arg0 + " -- " + destName);
        System.out.println();
    }
    
    
    
}
View Code

六、生产者配置文件 application-jms.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
        http://activemq.apache.org/schema/core
        http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd">
    <!-- ActiveMQ connectionFactory -->
    <amq:connectionFactory id="jmsConnectionFactory" brokerURL="${jms_server}" />
    
    <bean id="myJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory">
            <bean class="org.springframework.jms.connection.SingleConnectionFactory">
                <property name="targetConnectionFactory" ref="jmsConnectionFactory" />
            </bean>
        </property>
    </bean>
    
    <bean id="sendTopic" class="com.activeMq.service.SendTopic">
        <property name="template" ref="myJmsTemplate" />
        <property name="destination" value="viki_sense" />
        <property name="node" value="${jms_send_node}" />
        <property name="allNodeStr" value="${jms_send_allNodeStr}" />
    </bean>
</beans> 
View Code

七、消费者配置文件 application-processer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd">
 
   <!-- 监听节点  -->
    <amq:queue name="viki_sense" physicalName="${jms_receive_node}" />
    
  

    <!-- 消息接收 -->
    <bean id="receiveMessage" class="com.activeMq.service.ReceiveMessage">
        <property name="destName"><value>${jms_receive_node}</value></property>
    </bean>

    <!-- 订阅 -->
    <bean id="senseTopicMessageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
        <constructor-arg ref="receiveMessage" />
    </bean>

    <!-- 消息监听 TOPIC 模式-->
    <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="jmsConnectionFactory" />
        <property name="destination" ref="${jms_receive_node}" />
        <property name="messageListener" ref="senseTopicMessageListener" />
    </bean>
</beans>
View Code

八、其他配置文件

springMvc-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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/aop 
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <description>Spring-web MVC配置</description>
    
    
    
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>

            </list>
        </property>
    </bean>
    
    <mvc:annotation-driven />
    
    
    <context:component-scan base-package="com.activeMq.controller">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Service" />
    </context:component-scan>


    
    
</beans>
View Code

application-beans.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
           
    
           
     <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
     
     <context:annotation-config />
     
     <context:component-scan base-package="com.activeMq">  
          <!-- 排除vst.back目录下Controller的service注入 -->         
         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <bean id="configProperties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath*:conf/activeMq.properties</value>
            </list>
        </property>
    </bean>
    
   <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="properties" ref="configProperties" />
   </bean> 
     

     
     
     
     
     
</beans>
View Code

activeMq.properties

jms_server=tcp://localhost:61616
jms_send_node=viki_sense
jms_receive_node=viki_sense
jms_send_allNodeStr=x,x,x
View Code

log4j.properties

### direct log messages to stdout and logFile###
log4j.rootCategory=INFO, stdout,logFile

# OpenSymphony Stuff
log4j.logger.com.opensymphony=INFO
log4j.logger.org.apache.struts2=INFO
log4j.logger.org.apache.commons=INFO 

# Spring Stuff
log4j.logger.org.springframework=INFO
log4j.logger.org.springframework.oxm=INFO

# Hibernate Stuff
log4j.logger.org.hibernate=INFO
log4j.logger.org.hibernate.type=INFO
log4j.logger.org.hibernate.tool.hbm2ddl=INFO

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[u65F6u95F4:%d{yyyy-MM-dd hh:mm:ss}] [u7EA7u522B:%p] [u7C7B:%c]  [u6D88u606F:%m] %n 

log4j.appender.logFile=org.apache.log4j.RollingFileAppender
log4j.appender.logFile.File=D:\demo.log
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern=[u65F6u95F4:%d{yyyy-MM-dd hh:mm:ss}] [u7EA7u522B:%p] [u7C7B:%c]  [u6D88u606F:%m] %n 
log4j.appender.logFile.MaxFileSize = 5MB
log4j.appender.logFile.MaxBackupIndex =3
View Code

九、测试用到的action

package com.activeMq.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.activeMq.service.SendTopic;



@Controller
public class ActiveMqController {

    @Autowired
    private SendTopic sendTopic; 

    
    
    @RequestMapping("/topicSend")
    @ResponseBody
    public String topicSend(String message){
        String result = "";
        sendTopic.sendNode("testtesttest123123123123");
        result = "success";
        return result;
    }
}
View Code

没写页面,直接用url访问测试即可

十、源码

源码

原文地址:https://www.cnblogs.com/ggwow/p/7987425.html