This is the second part in the Spring ActiveMQ example tutorial. Please head over to the first part at Spring AMQP ActiveMQ Tutorial.
这是Spring ActiveMQ示例教程的第二部分。 请转到Spring AMQP ActiveMQ教程的第一部分。
Spring ActiveMQ示例 (Spring ActiveMQ Example)
We will have following sections in this Spring ActiveMQ Example post.
在这个Spring ActiveMQ示例帖子中,我们将有以下部分。
- Introduction 介绍
- Develop Spring AMQP Messaging Application With ActiveMQ 使用ActiveMQ开发Spring AMQP消息传递应用程序
- Develop Test client for Spring AMQP ActiveMQ Messaging Application 为Spring AMQP ActiveMQ消息传递应用程序开发测试客户端
- Test Spring AMQP Messaging Application With ActiveMQ 使用ActiveMQ测试Spring AMQP消息传递应用程序
介绍 (Introduction)
We have already discussed some “Spring AMQP Basics” Theoretically and “How to install and setup ActiveMQ Server” in my previous posts. Please refer them in the following:
在以前的文章中,我们已经从理论上讨论了一些“ Spring AMQP基础知识”和“如何安装和设置ActiveMQ Server”。 请参考以下内容:
- Spring AMQP Basics Spring AMQP基础
- How to install and setup Apache ActiveMQ Server 如何安装和设置Apache ActiveMQ Server
- Spring AMQP ActiveMQ Messaging Example (Part-1) Spring AMQP ActiveMQ消息传递示例(第1部分)
In this post, we are going to develop a Spring AMQP ActiveMQ Messaging application. Let us start it now.
在本文中,我们将开发一个Spring AMQP ActiveMQ Messaging应用程序。 让我们现在开始。
使用ActiveMQ开发Spring AMQP消息传递应用程序 (Develop Spring AMQP Messaging Application With ActiveMQ)
Let us start developing a Spring AMQP ActiveMQ Messaging application using Maven, Eclipse IDE and ActiveMQ Server. It is same for all other Java IDEs.
让我们开始使用Maven,Eclipse IDE和ActiveMQ Server开发Spring AMQP ActiveMQ Messaging应用程序。 对于所有其他Java IDE都是一样的。
Please do the following the steps one by one:
请一一执行以下步骤:
- Develop Spring ActiveMQ AMQP Publisher program 开发Spring ActiveMQ AMQP Publisher程序
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;
}
});
}
}
}
- Develop JMS Asynchronous JMS Consumer by using Spring JMS APIs MDPs. 通过使用Spring JMS API MDP开发JMS异步JMS使用者。
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) { } } }
- Final pom.xml file 最终的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://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 AMQP ActiveMQ消息传递应用程序开发测试客户端 (Develop Test client for Spring AMQP ActiveMQ Messaging Application)
- Develop a Test application 开发测试应用程序
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()); } }
NOTE:- As this Unit test name is ActiveMQJmsMessageListenerTest, then @ContextConfiguration annotation searches for ActiveMQJmsMessageListenerTest-context.xml file in same package structure.
注意:-由于此单元测试名称为ActiveMQJmsMessageListenerTest,因此@ContextConfiguration批注在相同的程序包结构中搜索ActiveMQJmsMessageListenerTest-context.xml文件。
- Final Project Structure 最终项目结构
Here we can see our Spring AMQP ActiveMQ Messaging application’s final Eclipse project structure.
在这里,我们可以看到Spring AMQP ActiveMQ Messaging应用程序的最终Eclipse项目结构。
使用ActiveMQ测试Spring AMQP消息传递应用程序 (Test Spring AMQP Messaging Application With ActiveMQ)
In this section, we will test our Spring AMQP ActiveMQ Messaging application using our Test client developed in the previous section.
在本节中,我们将使用上一节中开发的Test客户端来测试Spring AMQP ActiveMQ Messaging应用程序。
- Observe above Test program. We are using asserting concept to test the message count 遵守上述测试程序。 我们正在使用断言概念来测试消息数
assertEquals(10, count.get());
If both published and consumed messages are NOT equal then the below does not throw AssertError.
如果发布的消息和使用的消息都不相等,则以下消息不会引发AssertError。
- Run the Unit and see the successful message. 运行单元并查看成功消息。
Right click on the Test program and run it as Junit test.
右键单击“测试”程序,然后将其作为Junit测试运行。
- To see message in ActiveMQ Admin console, please comment listener configuration in XML file 要在ActiveMQ管理控制台中查看消息,请在XML文件中注释侦听器配置
<jms:listener-container container-type="default" connection-factory="consumerJmsConnectionFactory" acknowledge="auto"> <jms:listener destination="jms/TPActiveMQQueue" ref="activeMQMessageListener" /> </jms:listener-container>
And run test class again and observe ActiveMQ Admin console for 10 messages:
并再次运行测试类,并观察ActiveMQ管理控制台中的10条消息:
- And run test class again and observe ActiveMQ Admin console for 10 messages: 并再次运行测试类,并观察ActiveMQ管理控制台中的10条消息:
Unit test will fail as we don’t consume any messages. But that is fine as we need see messages in ActiveMQ Queue.
单元测试将失败,因为我们不使用任何消息。 但这很好,因为我们需要在ActiveMQ Queue中看到消息。
That’s it all about developing Spring AMQP ActiveMQ Messaging Example.
这就是开发Spring AMQP ActiveMQ消息传递示例的全部内容。
翻译自: https://www.journaldev.com/12743/spring-activemq-example