【ActiveMQ】1.下载安装启动使用

  1. 云栖社区>
  2. 博客>
  3. 正文

【ActiveMQ】1.下载安装启动使用

angel挤一挤 2017-11-29 11:37:00 浏览1139
展开阅读全文

官网下载:http://activemq.apache.org/activemq-5121-release.html

官网指导文档:http://activemq.apache.org/version-5-getting-started.html

 

《windows下安装使用版本Apache ActiveMQ 5.12.1》

========================================================================================================================

安装启动ActiveMQ

 1。根据上面官网地址,下载【请大家下载较新的版本,这样好多后续的BUG就能避免了】

2.解压到本地:

 

 3.启动:

【有个小坑,你应该找到bin目录下的对应64位或32位目录下的activemq.bat启动黑窗口】

 

 

4.访问:

【http://localhost:8161/admin/】帐号密码:admin

 

================================================================================================================================================================================

在spring项目中使用ActiveMQ

【搭建spring项目http://www.cnblogs.com/sxdcgaq8080/p/7979506.html

 

 

 

1.pom.xml文件引入架包

你可以添加完整的依赖,pom.xml 添加依赖:

     <!--ActiveMQ-->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.12.1</version>
        </dependency>

 当然,也可以根据实际的环境,引入部分需要的依赖即可,添加部分依赖!!

 

这里我选用版本如下:

  spring 版本4.3.13

  activeMQ版本5.15

完整的pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sxd</groupId>
    <artifactId>springDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.13.RELEASE</spring.version>
        <hibernate.version>4.3.8.Final</hibernate.version>
        <jackson.version>2.5.0</jackson.version>

    </properties>

    <dependencies>

        <!--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>

        <dependency>
            <groupId>org.apache.xbean</groupId>
            <artifactId>xbean-spring</artifactId>
            <version>4.6</version>
        </dependency>
        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- 使用SpringMVC需配置 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- spring  cglib 代理 -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>
        <!-- 关系型数据库整合时需配置 如hibernate jpa等 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <!-- 二级缓存ehcache -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.9.0</version>
        </dependency>

        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!-- mysql连接 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>

        <!-- c3p0数据源 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5-pre10</version>
        </dependency>

        <!-- json -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.3</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <!-- aop -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.4</version>
        </dependency>

        <!-- servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

</project>
View Code

【关于spring整合avticeMQ的过程中,产生的各种架包冲突】

http://www.cnblogs.com/sxdcgaq8080/p/8023726.html】 

http://www.cnblogs.com/sxdcgaq8080/p/8022121.html

http://www.cnblogs.com/sxdcgaq8080/p/8005886.html

 

2.搭建spring项目的流程和配置文件

参考:http://www.cnblogs.com/sxdcgaq8080/p/7979506.html

 

3.在第二步基础上,添加avticeMQ的配置

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.12.1.xsd"
>


    <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="destinationEx" class="org.apache.activemq.command.ActiveMQQueue">
        <!-- 设置消息队列的名字   名字就是order.queue-->
        <constructor-arg>
            <value>jms.queue</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="destinationEx" />
        <property name="receiveTimeout" value="10000" />
        <!-- true是topic,false是queue,默认是false,此处显示写出false -->
        <property name="pubSubDomain" value="false" />
    </bean>

    <!-- 配置消息队列监听者(Queue)
    <bean id="queueMessageListener" class="com.sxd.jms.QueueMessageListener" />

     显示注入消息监听容器(Queue),配置连接工厂,监听的目标是demoQueueDestination,监听器是上面定义的监听器
    <bean id="queueListenerContainer"
          class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="destinationEx" />
        <property name="messageListener" ref="queueMessageListener" />
    </bean>
    -->
</beans>
View Code

【这一部分,先不看监听器的使用,所以先注掉】

 

4.web,xml文件读取spring-ActiveMQ.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!-- web容器是web项目的大脑 -->
    <!-- 在web.xml中,执行顺序是:1.先执行listener 2。再执行filter  3.最后执行servlet 这就是执行顺序
        就像spring-hibernate.xml文件是在执行Listener的时候加载的,那注意这几个配置文件在web.xml中的加载顺序,因为资源文件是在配置文件【xml文件】中加载的,肯定是先加载的资源文件【properties文件】,才能在配置文件中调用资源文件中的变量的值使用

     -->
    <display-name>springDemo</display-name>
    <!-- web容器,首先需要引入spring,让spring管理各个框架,并将其注入为bean -->
    <!-- 这个相当于定一个 变量在web.xml中,在下面的listener  org.springframework.web.context.ContextLoaderListener中,加载这个spring-hibernate.xml文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring-hibernate.xml,
            classpath:spring-ActiveMQ.xml
        </param-value>
    </context-param>
    <!-- 配置控制层的filter信息 -->
    <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>

    <!-- openSessionInView配置 作用是延迟session关闭到view层 -->
    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <!-- 监听servletContext,启动contextConfigLocation中的spring配置信息 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>



    <!-- 类似与servlet 需要在web.xml中都需要配置一样,spring的思想是让这些servlet【controller也是servlet】继承了一个基础的servlet,那就只需在web.xml中配置一个servlet即可。 -->
    <!-- 这个servlet和下面的servlet-mapping相配合使用,注明了所有的以.htmls结尾的请求都被截取到,去到servlet中去找相对应的方法 进行处理 -->
    <servlet>
        <description>spring mvc servlet</description>
        <servlet-name>rest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!-- 此处配置的是SpringMVC的配置文件 -->
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- 配置了springmvc拦截的url为以.hmls结尾的请求 -->
    <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>*.htmls</url-pattern>
    </servlet-mapping>

    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 配置session超时时间,单位分钟 -->
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <!-- 欢迎界面 默认的项目访问界面 -->
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
View Code

 

5.消息生产者Producter.java

package com.sxd.jms;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

@Service
public class Producter {

    @Resource(name = "jmsTemplate")
    private JmsTemplate jmsTemplate;

    public void sendMessage(Destination destination, final String msg){
        System.out.println(Thread.currentThread().getName()+" 向队列"+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.getDefaultDestinationName();
        System.out.println(Thread.currentThread().getName()+" 向队列"+destination+"发送消息---------------------->"+msg);
        jmsTemplate.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(msg);
            }
        });
    }
}
View Code

 

6.消息消费者Consumer.java

package com.sxd.jms;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;

@Service
public class Consumer {

    @Resource(name="jmsTemplate")
    private JmsTemplate jmsTemplate;

    public String receive(Destination destination){
        TextMessage textMessage = (TextMessage) jmsTemplate.receive(destination);
        try{
            System.out.println("从队列" + destination.toString() + "收到了消息:\t"
                    + textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
        return textMessage.toString();
    }
}
View Code

 

7.控制器中send()方法是往MQ发送消息,get()方法是从MQ接收消息MainController.java

package com.sxd.controller;

import com.sxd.jms.Consumer;
import com.sxd.jms.Producter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.jms.Destination;

@Controller
public class MainController {

    @Resource(name = "producter")
    private Producter producter;

    @Resource(name = "consumer")
    private Consumer consumer;

    @Resource(name = "destinationEx")
    private Destination destination;

    @ResponseBody
    @RequestMapping("do")
    public String doMain(){
        return "启动成功";
    }

    @ResponseBody
    @RequestMapping("send")
    public String doSomething(){
        producter.sendMessage(destination,"Panama");
        return "Just  for  what";
    }

    @ResponseBody
    @RequestMapping("get")
    public String getSomething(){
        String str = consumer.receive(destination);
        return "MQ取出来消息"+str;
    }
}
View Code

 

8.启动服务,浏览器访问

    1》消息生产者往MQ中存入一条消息

查看MQ管理页面

 

     2》消息消费者从MQ中获取消息

 

 

 ============================================================================================================================================

设置消息监听器,自动监听MQ队列中的消息并处理

 上面 MQ中的消息,是自己去MQ队列中取出来的。

如果自动的让它只要有消息了就处理的话,需要上面的监听器完善一下

QueueMessageListener.java

package com.sxd.jms;

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

/**
 * 消息监听器
 */
public class QueueMessageListener implements MessageListener{


    public void onMessage(Message message) {
        TextMessage msg = (TextMessage) message;

        try{
            System.out.println("消息监听器监听到的消息:"+msg);
        }catch (Exception e){
            System.out.println(e);
        }
    }
}
View Code

 

然后把spring-ActiveMQ.xml中监听器放开

<!-- 配置消息队列监听者(Queue)-->
    <bean id="queueMessageListener" class="com.sxd.jms.QueueMessageListener" />

    <!--显式注入消息监听容器(Queue),配置连接工厂,监听的目标是demoQueueDestination,监听器是上面定义的监听器-->
    <bean id="queueListenerContainer"
          class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="destinationEx" />
        <property name="messageListener" ref="queueMessageListener" />
    </bean>
View Code

 

重启后,开始往队列中放入消息:

消息监听器自动处理了Queue中的消息!!!

 

===============================================================================================

完了哦!!

 

网友评论

登录后评论
0/500
评论
angel挤一挤
+ 关注