Spring & ActiveMQ

一、简介

ActiveMQ和Spring的整合是非常方便的。Spring有对JMS提供的Template机制。sping 整合ActiveMQ 可见ActiveMQ 官网相关配置:http://activemq.apache.org/spring-support.html

在这里插入图片描述

二、创建Maven Web 工程

1.pom.xml 添加如下依赖:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.25.RELEASE</version>
</dependency>
	
<dependency>
   <groupId>org.apache.activemq</groupId>
   <artifactId>activemq-all</artifactId>
   <version>5.15.9</version>
</dependency>
		
	
<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-spring -->
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-spring</artifactId>
    <version>5.15.9</version>
</dependency>
	
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>4.3.25.RELEASE</version>
</dependency>

	
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.2</version>
</dependency>

2.web.xml 添加如下配置:

  <!-- 配置spring 容器 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:application.xml</param-value>
  </context-param>
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- springMVC 前端控制器 -->
  <servlet>
 	<servlet-name>spring-mvc</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>spring-mvc</servlet-name>
 	<url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <!-- 编码过滤器 解决post 乱码-->
 <filter>
 	<filter-name>encoding</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>
 </filter>
 <filter-mapping>
 	<filter-name>encoding</filter-name>
 	<url-pattern>/*</url-pattern>
 </filter-mapping>

3.application.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
        
    <context:component-scan base-package="dao"></context:component-scan>
    <context:component-scan base-package="service"></context:component-scan>
    
    <!-- a pooling based JMS provider -->
    <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactoryBean">
    	<property name="connectionFactory">
    		<bean class="org.apache.activemq.ActiveMQConnectionFactory">
		      <property name="brokerURL">
		        <value>tcp://localhost:61616</value>
		      </property>
		      <property name="userName" value="vincent" />
		      <property name="password" value="123456" />
		    </bean>
    	</property>
    	<property name="maximumActive" value="10" />
    </bean>
    
    
 	<!-- Spring JMS Template -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
	  <property name="connectionFactory" ref="pooledConnectionFactory" />
	  <property name="defaultDestinationName" value="spring.mq.test" />
	</bean>
    
</beans>

4.spring-mvc.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	    <context:component-scan base-package="controller"></context:component-scan>
		<mvc:annotation-driven />
			 
		 

 		<!-- 使用默认的servlet 相应静态资源 -->
   		<mvc:default-servlet-handler />
 

</beans>

5.TestController.java

package controller;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	@Autowired
	private JmsTemplate template;
	
	@GetMapping("/produce")
	public void provider(String name) {
		System.out.println("produce:" + name);
		if(name != null) {
			template.send(new MessageCreator() {
				
				@Override
				public Message createMessage(Session arg0) throws JMSException {
					ObjectMessage message = new ActiveMQObjectMessage();
					message.setObject(name);
					return message;
				}
			});
		}
	}

	@GetMapping("/names")
	public Object names() throws JMSException {
		ObjectMessage msg = (ObjectMessage) template.receive();
		
		return msg.getObject();
		
	}
}

6.访问 /produce?name=vincent :

在这里插入图片描述

7.访问 /names :
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值