Windows下activemq搭建及应用场景

 

一:搭建及运行

1:解压下载好的activemq压缩包

2:进入${dir}/apache-activemq-5.5.0/bin目录,双击运行activemq.bat,

等待出现:

3:进入http://127.0.0.1:8161/admin/(这是activemq自带的一个web项目,进入的账号密码在配置文件中指定,稍后会讲),

图中红色标识的是:当前服务器有那些主题,及该主题的生产消费情况。主题可在自己的web项目中指定,稍后会讲。

二:开始建立自己的web工程

     2.1:建立maven项目,引入jar,具体如下:


  1.  
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3.  
    <modelVersion>4.0.0</modelVersion>
  4.  
    <groupId>web_pro</groupId>
  5.  
    <artifactId>pro</artifactId>
  6.  
    <packaging>war</packaging>
  7.  
    <version>1.0-SNAPSHOT</version>
  8.  
    <name>pro Maven Webapp</name>
  9.  
    <url>http://maven.apache.org</url>
  10.  
    <properties>
  11.  
    <spring.version>4.2.5.RELEASE</spring.version>
  12.  
    <mybatis.version>3.2.7</mybatis.version>
  13.  
    <shiro.version>1.2.5</shiro.version>
  14.  
    <slf4j.version>1.7.7</slf4j.version>
  15.  
    <jedis.version>2.7.2</jedis.version>
  16.  
    <log4j.version>1.2.17</log4j.version>
  17.  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18.  
    </properties>
  19.  
     
  20.  
    <dependencies>
  21.  
     
  22.  
    <dependency>
  23.  
    <groupId>redis.clients</groupId>
  24.  
    <artifactId>jedis</artifactId>
  25.  
    <version>${jedis.version}</version>
  26.  
    </dependency>
  27.  
     
  28.  
    <!-- <dependency>
  29.  
    <groupId>junit</groupId>
  30.  
    <artifactId>junit</artifactId>
  31.  
    <version>4.12</version>
  32.  
    <scope>test</scope>
  33.  
    </dependency>-->
  34.  
     
  35.  
     
  36.  
    <dependency>
  37.  
    <groupId>org.springframework</groupId>
  38.  
    <artifactId>spring-context</artifactId>
  39.  
    <version>4.2.1.RELEASE</version>
  40.  
    </dependency>
  41.  
     
  42.  
    <dependency>
  43.  
    <groupId>org.springframework</groupId>
  44.  
    <artifactId>spring-web</artifactId>
  45.  
    <version>4.2.1.RELEASE</version>
  46.  
    </dependency>
  47.  
     
  48.  
    <dependency>
  49.  
    <groupId>org.springframework</groupId>
  50.  
    <artifactId>spring-webmvc</artifactId>
  51.  
    <version>4.2.1.RELEASE</version>
  52.  
    </dependency>
  53.  
     
  54.  
    <dependency>
  55.  
    <groupId>org.springframework</groupId>
  56.  
    <artifactId>spring-websocket</artifactId>
  57.  
    <version>4.2.1.RELEASE</version>
  58.  
    </dependency>
  59.  
     
  60.  
    <dependency>
  61.  
    <groupId>org.springframework.security</groupId>
  62.  
    <artifactId>spring-security-web</artifactId>
  63.  
    <version>3.0.2.RELEASE</version>
  64.  
    </dependency>
  65.  
     
  66.  
    <dependency>
  67.  
    <groupId>org.springframework.security</groupId>
  68.  
    <artifactId>spring-security-config</artifactId>
  69.  
    <version>3.0.2.RELEASE</version>
  70.  
    </dependency>
  71.  
     
  72.  
    <dependency>
  73.  
    <groupId>org.springframework.security</groupId>
  74.  
    <artifactId>spring-security-taglibs</artifactId>
  75.  
    <version>3.0.2.RELEASE</version>
  76.  
    </dependency>
  77.  
     
  78.  
    <dependency>
  79.  
    <groupId>org.springframework</groupId>
  80.  
    <artifactId>spring-orm</artifactId>
  81.  
    <version>4.2.1.RELEASE</version>
  82.  
    </dependency>
  83.  
     
  84.  
     
  85.  
     
  86.  
    <!-- <dependency>
  87.  
    <groupId>org.hibernate</groupId>
  88.  
    <artifactId>hibernate-core</artifactId>
  89.  
    <version>5.0.1.Final</version>
  90.  
    </dependency>
  91.  
     
  92.  
    <dependency>
  93.  
    <groupId>mysql</groupId>
  94.  
    <artifactId>mysql-connector-java</artifactId>
  95.  
    <version>5.1.44</version>
  96.  
    </dependency> -->
  97.  
     
  98.  
    <dependency>
  99.  
    <groupId>org.slf4j</groupId>
  100.  
    <artifactId>slf4j-log4j12</artifactId>
  101.  
    <version>1.7.25</version>
  102.  
    </dependency>
  103.  
     
  104.  
    <dependency>
  105.  
    <groupId>net.sourceforge.jexcelapi</groupId>
  106.  
    <artifactId>jxl</artifactId>
  107.  
    <version>2.6.12</version>
  108.  
    </dependency>
  109.  
     
  110.  
    <dependency>
  111.  
    <groupId>com.mchange</groupId>
  112.  
    <artifactId>c3p0</artifactId>
  113.  
    <version>0.9.5.2</version>
  114.  
    </dependency>
  115.  
     
  116.  
    <dependency>
  117.  
    <groupId>javax.servlet</groupId>
  118.  
    <artifactId>javax.servlet-api</artifactId>
  119.  
    <version>4.0.0</version>
  120.  
    <scope>provided</scope>
  121.  
    </dependency>
  122.  
     
  123.  
    <dependency>
  124.  
    <groupId>javax.servlet</groupId>
  125.  
    <artifactId>jstl</artifactId>
  126.  
    <version>1.2</version>
  127.  
    </dependency>
  128.  
     
  129.  
    <dependency>
  130.  
    <groupId>javax.servlet.jsp</groupId>
  131.  
    <artifactId>jsp-api</artifactId>
  132.  
    <version>2.2</version>
  133.  
    <scope>provided</scope>
  134.  
    </dependency>
  135.  
     
  136.  
    <!-- <dependency>
  137.  
    <groupId>org.apache.struts</groupId>
  138.  
    <artifactId>struts2-core</artifactId>
  139.  
    <version>2.5.13</version>
  140.  
    </dependency> -->
  141.  
     
  142.  
    <dependency>
  143.  
    <groupId>com.fasterxml.jackson.core</groupId>
  144.  
    <artifactId>jackson-core</artifactId>
  145.  
    <version>2.9.1</version>
  146.  
    </dependency>
  147.  
     
  148.  
    <dependency>
  149.  
    <groupId>com.fasterxml.jackson.core</groupId>
  150.  
    <artifactId>jackson-annotations</artifactId>
  151.  
    <version>2.9.1</version>
  152.  
    </dependency>
  153.  
     
  154.  
    <dependency>
  155.  
    <groupId>com.fasterxml.jackson.core</groupId>
  156.  
    <artifactId>jackson-databind</artifactId>
  157.  
    <version>2.9.1</version>
  158.  
    </dependency>
  159.  
     
  160.  
    <dependency>
  161.  
    <groupId>commons-io</groupId>
  162.  
    <artifactId>commons-io</artifactId>
  163.  
    <version>2.5</version>
  164.  
    </dependency>
  165.  
     
  166.  
    <dependency>
  167.  
    <groupId>commons-codec</groupId>
  168.  
    <artifactId>commons-codec</artifactId>
  169.  
    <version>1.10</version>
  170.  
    </dependency>
  171.  
     
  172.  
    <dependency>
  173.  
    <groupId>commons-fileupload</groupId>
  174.  
    <artifactId>commons-fileupload</artifactId>
  175.  
    <version>1.3.3</version>
  176.  
    </dependency>
  177.  
     
  178.  
    <dependency>
  179.  
    <groupId>org.aspectj</groupId>
  180.  
    <artifactId>aspectjrt</artifactId>
  181.  
    <version>1.8.10</version>
  182.  
    </dependency>
  183.  
     
  184.  
    <!--<dependency>
  185.  
    <groupId>com.octo.captcha</groupId>
  186.  
    <artifactId>jcaptcha</artifactId>
  187.  
    <version>1.0</version>
  188.  
    </dependency>-->
  189.  
     
  190.  
    <dependency>
  191.  
    <groupId>net.sf.json-lib</groupId>
  192.  
    <artifactId>json-lib</artifactId>
  193.  
    <version>2.4</version>
  194.  
    <classifier>jdk15</classifier>
  195.  
    </dependency>
  196.  
    <!-- mybatis -->
  197.  
    <dependency>
  198.  
    <groupId>org.mybatis</groupId>
  199.  
    <artifactId>mybatis</artifactId>
  200.  
    <version>${mybatis.version}</version>
  201.  
    </dependency>
  202.  
     
  203.  
    <!-- mybatis-spring -->
  204.  
    <dependency>
  205.  
    <groupId>org.mybatis</groupId>
  206.  
    <artifactId>mybatis-spring</artifactId>
  207.  
    <version>1.2.2</version>
  208.  
    </dependency>
  209.  
     
  210.  
    <!-- 分页 -->
  211.  
    <dependency>
  212.  
    <groupId>com.github.pagehelper</groupId>
  213.  
    <artifactId>pagehelper</artifactId>
  214.  
    <version>5.1.2</version>
  215.  
    </dependency>
  216.  
    <!-- mysql 数据连接 -->
  217.  
    <dependency>
  218.  
    <groupId>mysql</groupId>
  219.  
    <artifactId>mysql-connector-java</artifactId>
  220.  
    <version>5.1.21</version>
  221.  
    </dependency>
  222.  
     
  223.  
    <!-- 阿里 连接池 -->
  224.  
    <dependency>
  225.  
    <groupId>com.alibaba</groupId>
  226.  
    <artifactId>druid</artifactId>
  227.  
    <version>1.1.3</version>
  228.  
    </dependency>
  229.  
    <!-- c3p0 -->
  230.  
    <dependency>
  231.  
    <groupId>com.mchange</groupId>
  232.  
    <artifactId>c3p0</artifactId>
  233.  
    <version>0.9.5.2</version>
  234.  
    </dependency>
  235.  
    <!-- mybatis逆向工程jar包 -->
  236.  
    <dependency>
  237.  
    <groupId>org.mybatis.generator</groupId>
  238.  
    <artifactId>mybatis-generator-core</artifactId>
  239.  
    <version>1.3.4</version>
  240.  
    </dependency>
  241.  
     
  242.  
     
  243.  
    <!-- <dependency>
  244.  
    <groupId>junit</groupId>
  245.  
    <artifactId>junit</artifactId>
  246.  
    <version>4.9</version>
  247.  
    <scope>test</scope>
  248.  
    </dependency> -->
  249.  
    <dependency>
  250.  
    <groupId>org.springframework</groupId>
  251.  
    <artifactId>spring-test</artifactId>
  252.  
    <version> 3.2.4.RELEASE </version>
  253.  
    <scope>provided</scope>
  254.  
    </dependency>
  255.  
     
  256.  
    <dependency>
  257.  
    <groupId>org.junit.jupiter</groupId>
  258.  
    <artifactId>junit-jupiter-api</artifactId>
  259.  
    <version>RELEASE</version>
  260.  
    </dependency>
  261.  
    <dependency>
  262.  
    <groupId>junit</groupId>
  263.  
    <artifactId>junit</artifactId>
  264.  
    <version>4.12</version>
  265.  
    </dependency>
  266.  
    <dependency>
  267.  
    <groupId>com.alibaba</groupId>
  268.  
    <artifactId>fastjson</artifactId>
  269.  
    <version>1.2.39</version>
  270.  
    </dependency>
  271.  
     
  272.  
    <!-- 极光推送 -->
  273.  
    <dependency>
  274.  
    <groupId>cn.jpush.api</groupId>
  275.  
    <artifactId>jpush-client</artifactId>
  276.  
    <version>3.2.17</version>
  277.  
    </dependency>
  278.  
     
  279.  
    <dependency>
  280.  
    <groupId>cn.jpush.api</groupId>
  281.  
    <artifactId>jiguang-common</artifactId>
  282.  
    <version>1.0.3</version>
  283.  
    </dependency>
  284.  
    <dependency>
  285.  
    <groupId>io.netty</groupId>
  286.  
    <artifactId>netty-all</artifactId>
  287.  
    <version>4.1.6.Final</version>
  288.  
    <scope>compile</scope>
  289.  
    </dependency>
  290.  
    <dependency>
  291.  
    <groupId>com.google.code.gson</groupId>
  292.  
    <artifactId>gson</artifactId>
  293.  
    <version>2.3</version>
  294.  
    </dependency>
  295.  
    <!--压缩图片-->
  296.  
    <dependency>
  297.  
    <groupId>net.coobird</groupId>
  298.  
    <artifactId>thumbnailator</artifactId>
  299.  
    <version>0.4.8</version>
  300.  
    </dependency>
  301.  
     
  302.  
    <dependency>
  303.  
    <groupId>org.apache.poi</groupId>
  304.  
    <artifactId>poi</artifactId>
  305.  
    <version>3.17</version>
  306.  
    </dependency>
  307.  
     
  308.  
    <dependency>
  309.  
    <groupId>net.sourceforge.jexcelapi</groupId>
  310.  
    <artifactId>jxl</artifactId>
  311.  
    <version>2.6.12</version>
  312.  
    </dependency>
  313.  
     
  314.  
     
  315.  
    <!-- 方法1
  316.  
    <dependency>
  317.  
    <groupId>pull-parser</groupId>
  318.  
    <artifactId>pull-parser</artifactId>
  319.  
    <version>2.1.10</version>
  320.  
    </dependency>
  321.  
    <dependency>
  322.  
    <groupId>dom4j</groupId>
  323.  
    <artifactId>dom4j</artifactId>
  324.  
    <version>1.5</version>
  325.  
    <exclusions>
  326.  
    <exclusion>
  327.  
    <groupId>pull-parser</groupId>
  328.  
    <artifactId>pull-parser</artifactId>
  329.  
    </exclusion>
  330.  
    </exclusions>
  331.  
    </dependency>-->
  332.  
    <!--方法二-->
  333.  
    <dependency>
  334.  
    <groupId>dom4j</groupId>
  335.  
    <artifactId>dom4j</artifactId>
  336.  
    <version>1.6.1</version>
  337.  
    </dependency>
  338.  
    <dependency>
  339.  
    <groupId>org.testng</groupId>
  340.  
    <artifactId>testng</artifactId>
  341.  
    <version>RELEASE</version>
  342.  
    </dependency>
  343.  
     
  344.  
     
  345.  
    <!--消息队列-->
  346.  
    <dependency>
  347.  
    <groupId>org.apache.activemq</groupId>
  348.  
    <artifactId>activemq-all</artifactId>
  349.  
    <version>5.9.0</version>
  350.  
    </dependency>
  351.  
    <dependency>
  352.  
    <groupId>org.apache.activemq</groupId>
  353.  
    <artifactId>activemq-pool</artifactId>
  354.  
    <version>5.9.0</version>
  355.  
    </dependency>
  356.  
     
  357.  
    <dependency>
  358.  
    <groupId>org.springframework</groupId>
  359.  
    <artifactId>spring-jms</artifactId>
  360.  
    <version>4.2.1.RELEASE</version>
  361.  
    </dependency>
  362.  
     
  363.  
    </dependencies>
  364.  
    <build>
  365.  
    <plugins>
  366.  
    <plugin>
  367.  
    <groupId>org.apache.maven.plugins</groupId>
  368.  
    <artifactId>maven-compiler-plugin</artifactId>
  369.  
    <version>3.2</version>
  370.  
    <configuration>
  371.  
    <source>1.7</source>
  372.  
    <target>1.7</target>
  373.  
    </configuration>
  374.  
    </plugin>
  375.  
    <!-- mybatis 逆向工程maven工具 -->
  376.  
    <plugin>
  377.  
    <groupId>org.mybatis.generator</groupId>
  378.  
    <artifactId>mybatis-generator-maven-plugin</artifactId>
  379.  
    <version>1.3.5</version>
  380.  
    <dependencies>
  381.  
    <dependency>
  382.  
    <groupId>mysql</groupId>
  383.  
    <artifactId>mysql-connector-java</artifactId>
  384.  
    <version>5.1.21</version>
  385.  
    </dependency>
  386.  
    </dependencies>
  387.  
    <configuration>
  388.  
    <overwrite>true</overwrite>
  389.  
    </configuration>
  390.  
    </plugin>
  391.  
    </plugins>
  392.  
    <finalName>pro</finalName>
  393.  
    </build>
  394.  
    </project>   

     注意:上面的pom.xml主要是引入了这几个包

  

  2.2:然后修改application-context,xml,主要是在项目中配置activemq

  1.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  2.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  3.  
    xmlns:context="http://www.springframework.org/schema/context"
  4.  
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5.  
    xmlns:task="http://www.springframework.org/schema/task"
  6.  
    xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.  
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  8.  
    http://www.springframework.org/schema/mvc
  9.  
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  10.  
    http://www.springframework.org/schema/context
  11.  
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
  12.  
    http://www.springframework.org/schema/aop
  13.  
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  14.  
    http://www.springframework.org/schema/tx
  15.  
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  16.  
    http://www.springframework.org/schema/task
  17.  
    http://www.springframework.org/schema/task/spring-task-4.0.xsd">
  18.  
     
  19.  
    <!-- 开启扫描 -->
  20.  
    <context:component-scan base-package="activemq" />
  21.  
    <!-- Activemq 连接工厂 -->
  22.  
    <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  23.  
    <constructor-arg value="lcm" /><!--用户名权限验证,在mq配置文件对应-->
  24.  
    <constructor-arg value="lcm" />
  25.  
    <constructor-arg value="failover:(tcp://localhost:61616,tcp://localhost:61617)?timeout=2000" /><!--配置集群,以,分割-->
  26.  
    </bean>
  27.  
     
  28.  
    <!-- ConnectionFactory Definition -->
  29.  
    <bean id="connectionFactory"
  30.  
    class="org.springframework.jms.connection.CachingConnectionFactory">
  31.  
    <constructor-arg ref="activeMQConnectionFactory" />
  32.  
    </bean>
  33.  
    <!-- Default Destination Queue Definition -->
  34.  
    <!-- 测试配置多个Destination -->
  35.  
    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
  36.  
    <constructor-arg index="0" value="myQueueName" /><!--自己的主题-->
  37.  
    </bean>
  38.  
    <!-- JmsTemplate Definition -->
  39.  
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  40.  
    <property name="connectionFactory" ref="connectionFactory" />
  41.  
    <property name="defaultDestination" ref="destination" />
  42.  
    </bean>
  43.  
    <!-- Message Sender Definition -->
  44.  
    <bean id="messageSender" class="activemq.publisher.MessageSender">
  45.  
    <constructor-arg index="0" ref="jmsTemplate" />
  46.  
    <constructor-arg index="1" ref="destination" />
  47.  
    </bean>
  48.  
    <!-- 消息监听器,主要监听的目的地址 Message Receiver Definition -->
  49.  
    <bean id="messageReceiver" class="activemq.consumer.MessageReceiver">
  50.  
    </bean>
  51.  
    <bean class="org.springframework.jms.listener.SimpleMessageListenerContainer">
  52.  
    <property name="connectionFactory" ref="connectionFactory" />
  53.  
    <property name="destinationName" value="myQueueName" /><!--消费发送者指定的主题-->
  54.  
    <property name="messageListener" ref="messageReceiver" />
  55.  
    </bean>
  56.  
    </beans>

三:解释配置文件

        到这一步基本完成了整合,具体要注意的步骤有一下几点:

        3.1: org.apache.activemq.ActiveMQConnectionFactory:是连接工厂,该类的构造方法传递了连接服务的用户名和密码,服务器的ip,注意该ip可以是个集群.用户名密码的指定配置在${dir}\apache-activemq-5.9.0\conf\activemq.xml中的<broker></broker>标签内添加如下标签:

  1.  
    <plugins>
  2.  
    <simpleAuthenticationPlugin>
  3.  
    <users>
  4.  
    <authenticationUser username="lcm" password="lcm" groups="admins"></authenticationUser>
  5.  
    </users>
  6.  
    </simpleAuthenticationPlugin>
  7.  
    </plugins>

       3.2:activemq.xml文件中的用户名密码与自己项目配置文件里关于activemq配置的用户名密码一致,然后连接工厂的第三个参数为activemq的服务器ip,它采用tcp连接,并且failover进行集群,并附带超时参数2000ms,其中的连接端口可以更改,具体在:${dir}\apache-activemq-5.9.0\conf\activemq.xml的


        3.3:构造队列:org.apache.activemq.command.ActiveMQQueue:构造方法中指定了自己队列的名称

         3.4:整合jms:


  1.  
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  2.  
    <property name="connectionFactory" ref="connectionFactory" />
  3.  
    <property name="defaultDestination" ref="destination" />
  4.  
    </bean>

        3.5:再接着整合自己编写的消息接受类和发送类,这个两个类主要是是实现了一些监听方法,然后做自己的业务处理

  1.  
    <bean id="messageSender" class="activemq.publisher.MessageSender">
  2.  
    <constructor-arg index="0" ref="jmsTemplate" />
  3.  
    <constructor-arg index="1" ref="destination" />
  4.  
    </bean>
  5.  
    <bean id="messageReceiver" class="activemq.consumer.MessageReceiver">

        3.6:最后将自己的监听类和配置activemq的消息队列及actiemq的连接工厂整合到jms监听中

  1.  
    <bean class="org.springframework.jms.listener.SimpleMessageListenerContainer">
  2.  
    <property name="connectionFactory" ref="connectionFactory" />
  3.  
    <property name="destinationName" value="myQueueName" /><!--消费发送者指定的队列名称-->
  4.  
    <property name="messageListener" ref="messageReceiver" />
  5.  
    </bean>

 

四:项目中的使用场景及额外注意步骤:

       activemq是在高并发下将请求处理的队列化,它自身处理多线程等一系列问题,所以我们直接用即可,监听好它的消息,然后一步步将队列中的请求消费完。

      提到高并发就不由想到nginx,有了nginx负载不就完了吗?为什么还用消息队列呢?

      答案如下:1:系统的瓶颈往往出在数据库,nginx的架构适合于多系统多数据库,对于数据库集群可适用,但这会导致数据同步等一系列问题增多.activemq针对的是单数据库,比如有些场景只使用了一个数据库(类似一些中心数据库),mq使用场景图如下:

 

 
 
 
 
 

                    2:比如持久层数据库和redis同步时,如果出现大量请求,数据同步方案也可以放在activemq队列中处理,防止并发出现的问题

五:注意事项

    使用activemq时,需防止单机宕机导致数据丢失,所以需要处理额外步骤:

        1:需要做集群

        2:做数据持久化,持久方案有多种,目前推荐activemq自带的KahaDB方式处理

posted on 2018-12-21 10:52  我是司  阅读(502)  评论(0)    收藏  举报

导航