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队列

Spring AMQP ActiveMQ教程_第1张图片

 

Spring AMQP ActiveMQ教程_第2张图片

  • 使用新的ActiveMQ管理控制台创建JMS队列
  • 单击“+创建”按钮,提供详细信息并单击“创建队列”按钮
  • 单击“浏览”按钮查看消息

为ActiveMQ创建Spring AMQP配置

通过提供ActiveMQ提供程序URL来配置JMS连接工厂


<amq:connectionFactory id="jmsFactory" brokerURL="tcp://localhost:61616" />

这里的URL包含协议,主机名和端口号。默认情况下,此应用程序需要tcp协议。主机名是localhost。我们使用的是默认端口号61616。


queue id="destination" physicalName="jms/TPActiveMQQueue" />

"jmsProducerConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
	<property name="targetConnectionFactory" ref="jmsFactory" />
bean>

"jmsConsumerConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
	<property name="targetConnectionFactory" ref="jmsFactory" />
bean>

注意: -
生产者和消费者都应该引用先前创建的JMS Factory对象,如上面的Spring AMQP XML配置中所示。


"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示例教程的第二部分。

目录[ 隐藏 ]

  • 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";
    @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.0modelVersion>
    <groupId>com.tpgroupId>
    <artifactId>simple-jmsartifactId>
    <version>1.0.2version>
	<properties>
	<java-version>1.6java-version>
	<org.springframework-version>3.1.1.RELEASEorg.springframework-version>
	<org.aspectj-version>1.6.10org.aspectj-version>
	<org.slf4j-version>1.6.6org.slf4j-version>
	<activemq.version>5.2.0activemq.version>
	properties>
	<dependencies>
	
	  <dependency>
	    <groupId>org.springframeworkgroupId>
		<artifactId>spring-contextartifactId>
		<version>${org.springframework-version}version>
		<exclusions>
		  
			<exclusion>
		 	  <groupId>commons-logginggroupId>
			  <artifactId>commons-loggingartifactId>
			 exclusion>
		  exclusions>
		dependency>
		<dependency>
		  <groupId>org.springframeworkgroupId>
		  <artifactId>spring-webmvcartifactId>
		  <version>${org.springframework-version}version>
		dependency>
		 <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jmsartifactId>
            <version>${org.springframework-version}version>
        dependency>
     		
        
	<dependency>
	  <groupId>org.aspectjgroupId>
	  <artifactId>aspectjrtartifactId>
	  <version>${org.aspectj-version}version>
	dependency>	
		
	
	<dependency>
	  <groupId>org.slf4jgroupId>
	  <artifactId>slf4j-apiartifactId>
	  <version>${org.slf4j-version}version>
	dependency>
	<dependency>
	  <groupId>org.slf4jgroupId>
	  <artifactId>jcl-over-slf4jartifactId>
	  <version>${org.slf4j-version}version>
	  <scope>runtimescope>
	dependency>
	<dependency>
	  <groupId>org.slf4jgroupId>
	  <artifactId>slf4j-log4j12artifactId>
	  <version>${org.slf4j-version}version>
	  <scope>runtimescope>
	dependency>
	<dependency>
		<groupId>log4jgroupId>
		<artifactId>log4jartifactId>
		<version>1.2.15version>
		<exclusions>
			<exclusion>
				<groupId>javax.mailgroupId>
				<artifactId>mailartifactId>
			exclusion>
			<exclusion>
				<groupId>javax.jmsgroupId>
			<artifactId>jmsartifactId>
			exclusion>
			<exclusion>
				<groupId>com.sun.jdmkgroupId>
				<artifactId>jmxtoolsartifactId>
			exclusion>
			<exclusion>
			<groupId>com.sun.jmxgroupId>
				<artifactId>jmxriartifactId>
			exclusion>
		exclusions>
		<scope>runtimescope>
	dependency>
		
	<dependency>
		<groupId>javax.injectgroupId>
		<artifactId>javax.injectartifactId>
			<version>1version>
	dependency>
			
	
	<dependency>
		<groupId>javax.servletgroupId>
		<artifactId>servlet-apiartifactId>
		<version>2.5version>
		<scope>providedscope>
	dependency>
	<dependency>
		<groupId>javax.servlet.jspgroupId>
		<artifactId>jsp-apiartifactId>
		<version>2.1version>
		<scope>providedscope>
	dependency>
	<dependency>
		<groupId>javax.servletgroupId>
		<artifactId>jstlartifactId>
		<version>1.2version>
	dependency>
	
	<dependency>
		<groupId>junitgroupId>
		<artifactId>junitartifactId>
		<version>4.7version>
		<scope>testscope>
	dependency> 
	
	
	<dependency>
           <groupId>org.apache.activemqgroupId>
           <artifactId>activemq-coreartifactId>
           <version>${activemq.version}version>
        dependency>
        <dependency>
            <groupId>org.apache.activemqgroupId>
            <artifactId>activemq-optionalartifactId>
            <version>${activemq.version}version>
        dependency>
        <dependency>
            <groupId>org.apache.xbeangroupId>
            <artifactId>xbean-springartifactId>
            <version>3.7version>
        dependency>
        
       <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${org.springframework-version}version>
        dependency> 
               
	dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-pluginartifactId>
                <version>2.9version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnatureprojectnature>
                    additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilderbuildcommand>
                    additionalBuildcommands>
                    <downloadSources>truedownloadSources>
                    <downloadJavadocs>truedownloadJavadocs>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>2.5.1version>
                <configuration>
                    <source>1.6source>
                    <target>1.6target>
                    <compilerArgument>-Xlint:allcompilerArgument>
                    <showWarnings>trueshowWarnings>
                    <showDeprecation>trueshowDeprecation>
                configuration>
            plugin>
            <plugin>
                <groupId>org.codehaus.mojogroupId>
                <artifactId>exec-maven-pluginartifactId>
                <version>1.2.1version>
                <configuration>
                    <mainClass>org.test.int1.MainmainClass>
                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项目结构。

Spring AMQP ActiveMQ教程_第3张图片

  • 开发测试应用程序
  • 最终项目结构

使用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管理控制台:

Spring AMQP ActiveMQ教程_第4张图片

单元测试将失败,因为我们不消耗任何消息。但这很好,因为我们需要在ActiveMQ队列中看到消息。

  • 观察上述测试程序。我们使用断言概念来测试消息计数
  • 运行单元并查看成功消息。
  • 要在ActiveMQ管理控制台中查看消息,请在XML文件中注释侦听器配置
  • 然后再次运行测试类并观察10个消息的ActiveMQ管理控制台:

这就是开发Spring AMQP ActiveMQ Messaging示例的全部内容。

你可能感兴趣的:(Spring,ActiveMQ)