Spring AMQP ActiveMQ教程
欢迎使用Spring AMQP ActiveMQ教程。之前我们研究过安装Apache ActiveMQ服务器。今天我们将创建一个Spring应用程序来使用ActiveMQ JMS队列。
目录[ 隐藏 ]
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 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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://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示例教程的第二部分。
目录[ 隐藏 ]
Spring ActiveMQ示例
我们将在这个Spring ActiveMQ示例帖子中有以下部分。
- 介绍
- 使用ActiveMQ 开发Spring AMQP消息传递应用程序
- 为Spring AMQP ActiveMQ消息应用程序开发测试客户端
- 使用ActiveMQ测试Spring AMQP消息传递应用程序
介绍
我们在之前的帖子中已经讨论过一些“Spring AMQP Basics”和“如何安装和设置ActiveMQ Server”。请参考以下内容:
在这篇文章中,我们将开发一个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";
@Autowired
private JmsTemplate jmsTemplate = null;
@PostConstruct
public 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
{
@Autowired
private 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 {
@Autowired
private AtomicInteger count = null;
@Test
public 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示例的全部内容。
53

被折叠的 条评论
为什么被折叠?



