第二章:ActiveMQ与spring的整合使用

2022-08-05 09:14:00 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

在之前建好的工程的主目录下建一个resource目录,然后建一个配置文件,我这里是activeMq.xml

配置文件的内容如下:

代码语言:javascript复制
<?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">  
	 <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->  
	 <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
	 	<property name="brokerURL" value="tcp://192.168.72.133:61616"></property>
	 </bean>
	  <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->  
	  <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
	  	 <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
	  	 <property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
	  </bean>
	   <!-- 配置生产者 -->  
       <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->  
       <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
       		<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  
       		<property name="connectionFactory" ref="connectionFactory"></property>
       </bean>
        <!--这个是队列目的地,点对点的 -->  
        <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        	<constructor-arg value="spring-queue"></constructor-arg>
        </bean>
         <!--这个是主题目的地,一对多的 --> 
         <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQQueue">
         		<constructor-arg value="topic"/>
         </bean>
</beans>

maven pom文件如下:

代码语言:javascript复制
<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.xiaoyexinxin</groupId>
	<artifactId>activeMQTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>activeMQTest</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		 <springframework>4.3.6.RELEASE</springframework>
	</properties>

	    <dependencies>  
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
          </dependency>
  
  
          <dependency>
              <groupId>jstl</groupId>
              <artifactId>jstl</artifactId>
              <version>1.2</version>
          </dependency>
  
          <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>javax.servlet-api</artifactId>
              <version>3.1.0</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-web</artifactId>
              <version>${springframework}</version>
          </dependency>
  
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-jms</artifactId>
              <version>${springframework}</version>
          </dependency>
  
  
          <!-- xbean -->
  
          <dependency>
              <groupId>org.apache.xbean</groupId>
              <artifactId>xbean-spring</artifactId>
              <version>3.16</version>
          </dependency>
  
          <dependency>
              <groupId>com.thoughtworks.xstream</groupId>
              <artifactId>xstream</artifactId>
              <version>1.3.1</version>
          </dependency>
  
          <!-- activemq -->
  
          <dependency>
              <groupId>org.apache.activemq</groupId>
              <artifactId>activemq-all</artifactId>
              <version>5.14.3</version>
          </dependency> 
  </dependencies>  
</project>

这里如果工程有感叹号,那么可能跟之前的jar有冲突,这里建议修改mavend本地仓库地址,即setting文件的配置:

换一个重新maven update试试。

测试代码:

代码语言:javascript复制
package com.xiaoyexinxin.activeMQTest;



import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

/**
 * 
 * @author liuxin
 * @date   2018年4月12日
 */
public class TestSpringActiveMq {

	@Test
	public void test() {
		//初始化一个spring容器
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:activeMq.xml"); 
		//从容器中获的JSMTemplate对象
		JmsTemplate jmsTemplate=(JmsTemplate) applicationContext.getBean("jmsTemplate");
		//从容器中获得一个Destination对象
		Queue queue=(Queue) applicationContext.getBean("queueDestination");
		//使用jmsTemplate对象发送消息,需要知道destination
		jmsTemplate.send(queue,new MessageCreator() {
			
			public Message createMessage(Session arg0) throws JMSException {
				// TODO Auto-generated method stub
				TextMessage message=arg0.createTextMessage("spring activemq test");
				return message;
			}
		});
	}
}

运行上面的代码,我们可以看到我们刚才发送的消息在activemq的后台管理系统可以看到。

二,下面测试接收消息

工作中我们的项目都是由几个工程组成的,这几个工程之间的通讯就会用到mq了。所以这里我们新建一个用来接收消息的工程。建好后如下:

通发送一样加上pom依赖

代码语言:javascript复制
<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.xiaoyexinxin</groupId>
  <artifactId>activeMQTestConsumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>activeMQTestConsumer</name>
  <url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		 <springframework>4.3.6.RELEASE</springframework>
	</properties>

	    <dependencies>  
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
          </dependency>
  
  
          <dependency>
              <groupId>jstl</groupId>
              <artifactId>jstl</artifactId>
              <version>1.2</version>
          </dependency>
  
          <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>javax.servlet-api</artifactId>
              <version>3.1.0</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-web</artifactId>
              <version>${springframework}</version>
          </dependency>
  
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-jms</artifactId>
              <version>${springframework}</version>
          </dependency>
  
  
          <!-- xbean -->
  
          <dependency>
              <groupId>org.apache.xbean</groupId>
              <artifactId>xbean-spring</artifactId>
              <version>3.16</version>
          </dependency>
  
          <dependency>
              <groupId>com.thoughtworks.xstream</groupId>
              <artifactId>xstream</artifactId>
              <version>1.3.1</version>
          </dependency>
    <!-- activemq -->
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-all</artifactId>
        <version>5.14.3</version>
    </dependency> 
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

下面我们新建一个监听器类,该类继承自MessageListener,如下图所示。

代码语言:javascript复制
package com.xiaoyexinxin.activeMQTestConsumer;

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

/**
 * 接收ActiveMQ发送的消
 * @author liuxin
 * @date   2018年4月14日
 */
public class MqConsumer implements MessageListener{

	public void onMessage(Message arg0) {
		// TODO Auto-generated method stub
		TextMessage textMessage=(TextMessage) arg0;
		String text;
		try {
			text = textMessage.getText();
			System.out.println(text);
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

最后新建一个测试类,如下图所示。

代码语言:javascript复制
package com.xiaoyexinxin.activeMQTestConsumer;


import java.io.IOException;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * 
 * @author liuxin
 * @date   2018年4月14日
 */
public class TestSpringActiveMQ {
	
	public static void main(String[] args) {
	  //初始化Spring容器  
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-activemq.xml");  
        
	}


}

直接运行测试类就能够接收到上面生产的消息了。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/106158.html原文链接:https://javaforall.cn

0 人点赞