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 BasicsSpring 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";@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;}});}}
}
  • 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
    { @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) { }}}
  • 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 {@Autowiredprivate AtomicInteger count = null;    @Testpublic 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

Spring ActiveMQ示例(第2部分)相关推荐

  1. Spring ActiveMQ教程

    Spring ActiveMQ 今天,我们将了解如何下载和安装Apache ActiveMQ Server并在Apache ActiveMQ Server中创建队列或主题. Spring Active ...

  2. 使用spring + ActiveMQ 总结

    使用spring + ActiveMQ 总结 摘要 Spring 整合JMS 基于ActiveMQ 实现消息的发送接收 目录[-] Spring 整合JMS 基于ActiveMQ 实现消息的发送接收 ...

  3. Struts2+Hibernate+Spring 整合示例

    转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...

  4. spring boot示例_Spring Boot完成示例

    spring boot示例 这篇文章提供了一个使用Spring Boot开发松耦合的REST服务的完整示例. 使用spring boot,我们可以开发可独立运行的生产就绪的Java应用程序,使其成为独 ...

  5. spring aop示例_Spring查找方法示例

    spring aop示例 当一个bean依赖于另一个bean时,我们使用setter属性或通过构造函数注入bean. getter方法将向我们返回已设置的引用,但是假设您每次调用getter方法时都想 ...

  6. spring boot示例_Spring Boot上的Spring社交示例,或者我如何停止担心和喜欢自动配置...

    spring boot示例 对于Spring Boot 1.1.0.RC1,添加了自动配置和Spring Social的启动程序pom,这意味着我不必向pom添加一百个依赖关系,并且将为我处理许多毫无 ...

  7. spring aop示例_Spring JpaRepository示例(内存中)

    spring aop示例 这篇文章描述了一个使用内存中HSQL数据库的简单Spring JpaRepository示例. 该代码示例可从GitHub的Spring-JpaRepository目录中获得 ...

  8. spring aop示例_Spring Profile模式示例

    spring aop示例 最近,我们介绍了Spring Profiles的概念. 此概念是针对不同部署环境的轻松配置区分符. 直接的用例(已提出)是对相关的类进行注释,以便Spring根据活动的配置文 ...

  9. Spring JpaRepository示例(内存中)

    这篇文章描述了一个使用内存中HSQL数据库的简单Spring JpaRepository示例. 该代码示例可从GitHub的Spring-JpaRepository目录中获得. 它基于带有注释的Spr ...

最新文章

  1. Saltstack笔记
  2. Spring3 表达式语言(SpEL)介绍
  3. 荐 Intellij IDEA创建Maven Web项目(带有webapp文件夹目录的项目)
  4. uinty粒子系统子物体变大_Unity2018粒子系统全息讲解,坑深慎入(3)
  5. all方法 手写promise_promise.all的实现
  6. 我学到的5件事,指导2,500名有抱负的开发人员
  7. 智能手机收邮件之Mobile
  8. easyui datagrid reload后自动全选解决
  9. Java 机器学习库 Tribuo
  10. ENVI的seamless mosaic工具详解
  11. smtp服务器组件,配置exchange Smtp服务器
  12. 制作 maxdos 启动盘 Linux 安装盘
  13. cad隐藏图层命令快捷键_CAD关闭图层快捷键,隐藏显示的CAD图层
  14. 系统优化的基本思想点
  15. 和老外聊天、发邮件常用英语缩写(超实用)
  16. 马哥教育42期第三周作业
  17. 技术领导力 程序员如何才能带团队 文摘 (一)
  18. Nodejs+express+vue+Elementui酒店客房管理系统
  19. 随机展示一个汉字,可以用来让一二年级孩子识字
  20. 创建达梦到Oracle的DBLINK(OCI方式)

热门文章

  1. 【整理】C#2.0特性之局部类、空属类型和静态类
  2. [转载] C++转JAVA的转换方法及约定
  3. 在github上保存vscode的配置(后续重新安装vscode时,可以十分方便地从github上下载安装这个保存的配置)...
  4. MyEclipse的html页面 design视图中 关闭可视化界面
  5. centos 设置时间为北京时间
  6. 3.c语言结构体成员内存对齐详解
  7. JVM笔记7:类加载器
  8. 关于“抵制”易语言的通告
  9. 数据结构笔记(六)-- 双向链表
  10. Python配置opencv并在命令行运行