ActiveMq和SpringmVC 整合 消息队列(上)

来源:互联网 发布:做软装用什么软件 编辑:程序博客网 时间:2024/04/30 06:27

创建一个maven项目  创建配置文件

在pom.xml加入jar的依赖

<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>com.zhh.demo</groupId>  <artifactId>myActiveMq</artifactId>  <packaging>war</packaging>  <version>0.0.1-SNAPSHOT</version>  <name>myActiveMq 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>        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>servlet-api</artifactId>        <version>${javax.servlet}</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>myActiveMq</finalName>  </build></project>

在web.xml加入要读取的配置文件及编码的设置  红色部分是头信息固定的内容 如果

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"            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>Archetype Created Web Application</display-name>    <!-- 加载spring的配置文件,例如hibernate、jms等集成 -->  <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>          classpath:spring-context.xml;          classpath:spring-activemq.xml;     </param-value>    </context-param>    <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>    <servlet>        <servlet-name>springMVC</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring-mvc.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>springMVC</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>        <!-- 处理编码格式 -->    <filter>          <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>          <url-pattern>/*</url-pattern>      </filter-mapping>  </web-app>


创建spring-context.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"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.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">             <context:component-scan base-package="com.zhh.demo.entity" />    <mvc:annotation-driven />     </beans>


创建spring-mvc.xml 加入扫描controller包及视图的设置

<?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"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans.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">             <context:component-scan base-package="com.zhh.demo.mvc.controller" />    <mvc:annotation-driven />         <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">        <property name="viewClass"            value="org.springframework.web.servlet.view.JstlView" />        <property name="prefix" value="/WEB-INF/views/" />        <property name="suffix" value=".jsp" />    </bean></beans>

创建spring-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.zhh.demo.activemq" />    <mvc:annotation-driven />        
    <!-- 配置连接activemq -->
<amq:connectionFactory id="amqConnectionFactory" brokerURL="tcp://192.168.150.61: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>zhh.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>


另一种配置连接activemq

<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">          <!-- property name="brokerURL" value="tcp://127.0.0.1:61616" / -->          <property name="brokerURL" value="failover://(tcp://127.0.0.1:61616)?initialReconnectDelay=100" />           <property name="useAsyncSend" value="true" />          <property name="maxThreadPoolSize" value="50" />      </bean> 
这个brokerURL这个属性的值要注意下:是为了保证断线自动重连;如果没有设置,程序与ActiveMQ服务连接后期间出现过网络异常,

再程序无法与ActiveMQ正常通讯





0 0