Spring AMQP ActiveMQ教程
Spring AMQP ActiveMQ教程
欢迎使用Spring AMQP ActiveMQ教程。之前我们研究过安装Apache ActiveMQ服务器。今天我们将创建一个Spring应用程序来使用ActiveMQ JMS队列。
目录[ 隐藏 ]
- 1 Spring AMQP ActiveMQ教程
- 1.1简介
- 1.2 Spring AMQP标签库
- 1.3使用ActiveMQ开发和测试Spring AMQP的步骤
- 1.4使用ActiveMQ的Spring AMQP的优点和缺点
- 1.5在ActiveMQ服务器中创建JMS队列
- 1.6为ActiveMQ创建Spring AMQP配置
Spring AMQP ActiveMQ教程
Spring AMQP ActiveMQ教程分为以下几个部分。
- 介绍
- Spring AMQP标签库
- 使用ActiveMQ开发和测试Spring AMQP的步骤
- Spring AMQP与ActiveMQ的优点和缺点
- 在ActiveMQ服务器中创建JMS队列
- 为ActiveMQ创建Spring AMQP配置
介绍
我们在之前的帖子中已经讨论过一些“Spring AMQP Basics”和“如何安装和设置ActiveMQ Server”。请参考以下内容:
- Spring AMQP基础知识
- 如何安装和设置Apache ActiveMQ Server
在这篇文章和我的下一篇文章中,我们将使用一对一消息应用程序队列开发Spring AMQP ActiveMQ Messaging应用程序。我们现在就开始吧。
最初我想为整个例子提供一个帖子。然而,在准备好整个步骤之后,我觉得它的篇幅太长了。所以它分为两个职位。在这篇文章中,我们将讨论一些Apache ActiveMQ基础知识,然后如何配置我们的应用程序队列,然后执行一些Spring Framework AMQP和ActiveMQ API XML配置。在我的下一篇文章中,我们将开发实际的Java程序并对其进行测试。
请单击此处“Spring AMQP ActiveMQ Messaging Example”以访问第2部分帖子。
Spring AMQP标签库
Spring Framework提供了两种AMQP标记库来与以下AMQP服务器一起使用:
Spring RabbitMQ AMQP标记库用于使用Spring AMQP API和Spring RabbitMQ API为RabbitMQ Server开发消息传递应用程序。
Spring RabbitMQ AMQP标记库在“spring-rabbit.xsd”文件中定义。
Spring ActiveMQ AMQP标记库用于使用Spring AMQP API和Apache ActiveMQ API为Apache ActiveMQ Server开发消息传递应用程序。
Spring RabbitMQ AMQP标记库在“activemq-core.xsd”文件中定义。
- RabbitMQ AMQP标签库
- ActiveMQ AMQP标签库
使用ActiveMQ开发和测试Spring AMQP的步骤
在本节中,我将列出使用Apache ActiveMQ Server开发和测试Spring AMQP Messaging应用程序所需的所有步骤。我们将在接下来的部分和即将发布的帖子中讨论这些步骤。
请按照以下步骤使用Apache ActiveMQ Server开发和测试Spring AMQP Messaging应用程序。
- 安装Apache ActiveMQ Server(参考ActiveMQ部分)
- 在Eclipse中创建Mavenized Java项目
- 为ActiveMQ创建Spring AMQP配置
- 在ActiveMQ服务器中创建JMS队列
- 使用ActiveMQ开发Spring AMQP消息传递应用程序
- 使用ActiveMQ测试Spring AMQP消息传递应用程序
这些是使用Apache ActiveMQ Server开发和测试Spring AMQP Messaging应用程序的简要步骤。如果你对它们不了解,不用担心。我们将在接下来的部分逐一讨论这些步骤。
Spring AMQP与ActiveMQ的优点和缺点
在本节中,我们将讨论:与“Spring AMQP与RabbitMQ Server”组合相比,“Spring AMQP与ActiveMQ Server”组合的优点和缺点是什么
“Spring AMQP与ActiveMQ”组合具有以下优势:
- Apache ActiveMQ Server非常好地支持XA事务。
- 我们可以轻松地将Apache ActiveMQ MQ Broker嵌入到Java应用程序中。
- 将“Spring AMQP与ActiveMQ Application”与Apache Camel集成非常容易。
尽管“Spring AMQP With ActiveMQ”组合有许多好处,但它有一个主要缺点:
- ActiveMQ需要更多内存来维护应用程序。
在ActiveMQ服务器中创建JMS队列
- 使用新的ActiveMQ管理控制台创建JMS队列
- 单击“+创建”按钮,提供详细信息并单击“创建队列”按钮
- 单击“浏览”按钮查看消息
为ActiveMQ创建Spring AMQP配置
通过提供ActiveMQ提供程序URL来配置JMS连接工厂
<amq:connectionFactory id="jmsFactory" brokerURL="tcp://localhost:61616" />
这里的URL包含协议,主机名和端口号。默认情况下,此应用程序需要tcp协议。主机名是localhost。我们使用的是默认端口号61616。
<amq:queue id="destination" physicalName="jms/TPActiveMQQueue" />
<bean id="jmsProducerConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><property name="targetConnectionFactory" ref="jmsFactory" />
</bean>
<bean id="jmsConsumerConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><property name="targetConnectionFactory" ref="jmsFactory" />
</bean>
注意: -
生产者和消费者都应该引用先前创建的JMS Factory对象,如上面的Spring AMQP XML配置中所示。
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="producerJmsConnectionFactory" /><property name="defaultDestination" ref="destination" />
</bean>
<jms:listener-container container-type="default" connection-factory="consumerJmsConnectionFactory" acknowledge="auto">
<jms:listener destination="jms/TPActiveMQQueue" ref="activeMQMessageListener" />
</jms:listener-container>
<?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:jms="http://www.springframework.org/schema/jms"xmlns:amq="http://activemq.apache.org/schema/core"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsdhttp://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"><context:component-scan base-package="com.tp.jms.activemq" />
<amq:connectionFactory id="jmsFactory" brokerURL="tcp://localhost:61616" />
<amq:queue id="destination" physicalName="jms/TPActiveMQQueue" /><bean id="producerJmsConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><property name="targetConnectionFactory" ref="jmsFactory" />
</bean><bean id="consumerJmsConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><property name="targetConnectionFactory" ref="jmsFactory" />
</bean><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="producerJmsConnectionFactory" /><property name="defaultDestination" ref="destination" />
</bean><jms:listener-container container-type="default" connection-factory="consumerJmsConnectionFactory" acknowledge="auto">
<jms:listener destination="jms/TPActiveMQQueue" ref="activeMQMessageListener" />
</jms:listener-container><bean id="counter" class="java.util.concurrent.atomic.AtomicInteger"/>
</beans>
- 为ActiveMQ Server配置Spring AMQP XML配置
- 配置目标(这里我们使用队列)
- 使用Spring JMS API的SingleConnectionFactory声明Producer Connection Factory
- 使用Spring JMS API的SingleConnectionFactory声明Consumer Connection Factory
- 为JMS Producer配置Spring JmsTemplate以向Destination发送消息
- 配置JMS侦听器以使用消息
- 完成Spring配置文件:
这就是Spring XML Configuration开发Spring AMQP ActiveMQ Messaging示例的全部内容。
请转到本教程第二部分的Spring ActiveMQ示例。
这是Spring ActiveMQ示例教程的第二部分。
目录[ 隐藏 ]
- 1 Spring ActiveMQ示例
- 1.1简介
- 1.2使用ActiveMQ开发Spring AMQP消息传递应用程序
- 1.3为Spring AMQP ActiveMQ消息应用程序开发测试客户端
- 1.4使用ActiveMQ测试Spring AMQP消息传递应用程序
Spring ActiveMQ示例
我们将在这个Spring ActiveMQ示例帖子中有以下部分。
- 介绍
- 使用ActiveMQ 开发Spring AMQP消息传递应用程序
- 为Spring AMQP ActiveMQ消息应用程序开发测试客户端
- 使用ActiveMQ测试Spring AMQP消息传递应用程序
介绍
我们在之前的帖子中已经讨论过一些“Spring AMQP Basics”和“如何安装和设置ActiveMQ Server”。请参考以下内容:
- Spring AMQP基础知识
- 如何安装和设置Apache ActiveMQ Server
- Spring AMQP ActiveMQ消息传递示例(第1部分)
在这篇文章中,我们将开发一个Spring AMQP ActiveMQ Messaging应用程序。我们现在就开始吧。
使用ActiveMQ开发Spring AMQP消息传递应用程序
让我们开始使用Maven,Eclipse IDE和ActiveMQ Server开发Spring AMQP ActiveMQ Messaging应用程序。对于所有其他Java IDE,它都是相同的。
请逐一执行以下步骤:
package com.tp.jms.activemq;import javax.annotation.PostConstruct;
import javax.jms.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
@Component
public class ActiveMQMessageProducer {protected static final String MSG_COUNT = "messageCount";@Autowiredprivate JmsTemplate jmsTemplate = null;@PostConstructpublic void generateMessages() throws JMSException {for (int messageCount = 0; messageCount < 10; messageCount++) {final String text = "TP Message " + messageCount;jmsTemplate.send(new MessageCreator() {public Message createMessage(Session session) throws JMSException{TextMessage textMessage = session.createTextMessage(text);textMessage.setIntProperty(MSG_COUNT, messageCount); return textMessage;}});}}
}
package com.tp.jms.activemq;import java.util.concurrent.atomic.AtomicInteger;
import javax.jms.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class ActiveMQMessageListener implements MessageListener
{ @Autowiredprivate AtomicInteger count = null;public void onMessage(Message message){try { if (message instanceof TextMessage) {TextMessage txtMsg = (TextMessage)message;System.out.println("Received message from Destination : " +txtMsg.getText()); count.incrementAndGet();}} catch (JMSException e) { }}}
<?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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.tp</groupId><artifactId>simple-jms</artifactId><version>1.0.2</version><properties><java-version>1.6</java-version><org.springframework-version>3.1.1.RELEASE</org.springframework-version><org.aspectj-version>1.6.10</org.aspectj-version><org.slf4j-version>1.6.6</org.slf4j-version><activemq.version>5.2.0</activemq.version></properties><dependencies><!-- Spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${org.springframework-version}</version><exclusions><!-- Exclude Commons Logging in favor of SLF4j --><exclusion><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${org.springframework-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${org.springframework-version}</version></dependency><!-- AspectJ --><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>${org.aspectj-version}</version></dependency> <!-- Logging --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${org.slf4j-version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>jcl-over-slf4j</artifactId><version>${org.slf4j-version}</version><scope>runtime</scope></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${org.slf4j-version}</version><scope>runtime</scope></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.15</version><exclusions><exclusion><groupId>javax.mail</groupId><artifactId>mail</artifactId></exclusion><exclusion><groupId>javax.jms</groupId><artifactId>jms</artifactId></exclusion><exclusion><groupId>com.sun.jdmk</groupId><artifactId>jmxtools</artifactId></exclusion><exclusion><groupId>com.sun.jmx</groupId><artifactId>jmxri</artifactId></exclusion></exclusions><scope>runtime</scope></dependency><!-- @Inject --><dependency><groupId>javax.inject</groupId><artifactId>javax.inject</artifactId><version>1</version></dependency><!-- Servlet --><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet.jsp</groupId><artifactId>jsp-api</artifactId><version>2.1</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- Test --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.7</version><scope>test</scope></dependency> <!-- ActiveMQ --><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-core</artifactId><version>${activemq.version}</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-optional</artifactId><version>${activemq.version}</version></dependency><dependency><groupId>org.apache.xbean</groupId><artifactId>xbean-spring</artifactId><version>3.7</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${org.springframework-version}</version></dependency> </dependencies><build><plugins><plugin><artifactId>maven-eclipse-plugin</artifactId><version>2.9</version><configuration><additionalProjectnatures><projectnature>org.springframework.ide.eclipse.core.springnature</projectnature></additionalProjectnatures><additionalBuildcommands><buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand></additionalBuildcommands><downloadSources>true</downloadSources><downloadJavadocs>true</downloadJavadocs></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.5.1</version><configuration><source>1.6</source><target>1.6</target><compilerArgument>-Xlint:all</compilerArgument><showWarnings>true</showWarnings><showDeprecation>true</showDeprecation></configuration></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>1.2.1</version><configuration><mainClass>org.test.int1.Main</mainClass></configuration></plugin></plugins></build>
</project>
- 开发Spring ActiveMQ AMQP Publisher程序
- 使用Spring JMS API MDP开发JMS异步JMS使用者。
- 最终的pom.xml文件
为Spring AMQP ActiveMQ消息应用程序开发测试客户端
package com.tp.jms.activemq;
import static org.junit.Assert.*;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ActiveMQJmsMessageListenerTest {@Autowiredprivate AtomicInteger count = null; @Testpublic void testMessage() throws Exception {assertEquals(10, count.get());}
}
注意: -由于此单元测试名称为ActiveMQJmsMessageListenerTest,因此@ContextConfiguration批注在相同的包结构中搜索ActiveMQJmsMessageListenerTest-context.xml文件。
在这里,我们可以看到我们的Spring AMQP ActiveMQ Messaging应用程序的最终Eclipse项目结构。
- 开发测试应用程序
- 最终项目结构
使用ActiveMQ测试Spring AMQP消息传递应用程序
在本节中,我们将使用上一节中开发的Test客户端测试Spring AMQP ActiveMQ Messaging应用程序。
assertEquals(10, count.get());
如果已发布和已消息的消息都不相等,则以下内容不会抛出AssertError。
右键单击Test程序并将其作为Junit测试运行。
<jms:listener-container container-type="default" connection-factory="consumerJmsConnectionFactory" acknowledge="auto"><jms:listener destination="jms/TPActiveMQQueue" ref="activeMQMessageListener" />
</jms:listener-container>
然后再次运行测试类并观察10个消息的ActiveMQ管理控制台:
单元测试将失败,因为我们不消耗任何消息。但这很好,因为我们需要在ActiveMQ队列中看到消息。
- 观察上述测试程序。我们使用断言概念来测试消息计数
- 运行单元并查看成功消息。
- 要在ActiveMQ管理控制台中查看消息,请在XML文件中注释侦听器配置
- 然后再次运行测试类并观察10个消息的ActiveMQ管理控制台:
这就是开发Spring AMQP ActiveMQ Messaging示例的全部内容。
Spring AMQP ActiveMQ教程相关推荐
- amqp activemq_Spring AMQP ActiveMQ教程(第1部分)
amqp activemq Welcome to Spring AMQP ActiveMQ Tutorial. Earlier we looked into installing Apache Act ...
- Spring ActiveMQ教程
Spring ActiveMQ 今天,我们将了解如何下载和安装Apache ActiveMQ Server并在Apache ActiveMQ Server中创建队列或主题. Spring Active ...
- Spring AMQP RabbitMQ示例
Spring AMQP RabbitMQ示例 今天我们将研究Spring AMQP RabbitMQ示例应用程序.我们之前的帖子中已经讨论了一些"Spring AMQP基础知识理论" ...
- Spring AMQP 教程
Spring AMQP 我们今天将研究Spring AMQP. 目录[ 隐藏 ] 1 Spring AMQP 1.1什么是AMQP? 1.2为什么我们需要AMQP? 1.3 JMS和AMQP之间的区别 ...
- Spring教程– Spring Core Framework教程
Spring is one of the most widely used Java EE frameworks. I have written a lot on Spring Tutorial an ...
- day72 JavaWeb框架阶段——RabbitMQ消息队列【了解常见的MQ产品,了解RabbitMQ的5种消息模型,会使用Spring AMQP】
文章目录 0.学习目标 1.RabbitMQ 1.1.搜索与商品服务的问题 1.2.消息队列(MQ) 1.2.1.什么是消息队列 1.2.2.AMQP和JMS 1.2.3.常见MQ产品 1.2.4.R ...
- Apache ActiveMQ教程
一.特性及优势1.实现JMS1.1规范,支持J2EE1.4以上2.可运行于任何jvm和大部分web容器(ActiveMQ works great in any JVM)3.支持多种语言客户端(java ...
- activimq java集成_Java消息队列-Spring整合ActiveMq
1.概述 首先和大家一起回顾一下Java 消息服务,在我之前的博客<Java消息队列-JMS概述>中,我为大家分析了: 消息服务:一个中间件,用于解决两个活多个程序之间的耦合,底层由Jav ...
- Java消息队列-Spring整合ActiveMq
1.概述 首先和大家一起回顾一下Java 消息服务,在我之前的博客<Java消息队列-JMS概述>中,我为大家分析了: 消息服务:一个中间件,用于解决两个或多个程序之间的耦合,底层由Jav ...
最新文章
- rtems线程管理与调度(一)
- Android布局管理器-使用TableLayout表格布局管理器实现简单的用户登录页面
- 银行交易系统 TiDB 在线缩容迁移
- cdn节点人少延迟高_如何正确配置CDN高速缓存,避免越用越慢的尴尬
- Qt样例学习1(数字时钟)
- 【kafka系列】kafka之生产者发送消息实践
- vs ajax工具包引用,vs2008中使用AJAX Control Tookit工具的问题?
- 空间复杂度,实现从excel导出到txt文件中的java代码自动构建,逻辑条件不同实现则不同...
- window server 下搭建sftp服务器,Freesshd安装及配置
- ABAP - 3D Graphs with SAP
- 【转】JMeter学习(十八)JMeter测试Java(二)
- (三)云计算技术学习--OpenStack之KeyStone
- 人人开源搭建后台管理系统
- JavaWeb复习题(含答案)
- python人口普查数据显示_如何使用FCC的API在Python中查找人口普查数据块并遍历dict列表?...
- 我那曲折的英语学习经历:学不学语法,天差地远 转载自豆瓣
- 传感器研究NO1.陀螺仪
- 虚拟机安装ubantu系统的详细操作
- Wordpress建站教程:网站SEO优化
- 【晓松奇谈】历史是什么,人生是什么,世界是什么,未来是什么?高晓松的回答
热门文章
- Magento 创建唯一优惠券 Create unique coupon code in Magento
- 部分主流sns平台的账号登录及api操作
- 安装MongoDB Install MongoDB on Ubuntu
- Typora如何插入公式
- 统计学习方法-李航(6)
- 背包——完全背包Warcraft III(哈理工1053)
- tomcat调优的几个方面
- Spring boot配置项目访问路径server.context-path不起作用(改为server.servlet.context-path)
- 原码、反码、补码详述
- 【今日CV 视觉论文速览】Thu, 21 Feb 2019