Spring ActiveMQ示例

时间:2020-02-23 14:35:43  来源:igfitidea点击:

介绍

在以前的文章中,我们已经从理论上讨论了一些" Spring AMQP基础知识"和"如何安装和设置ActiveMQ Server"。
在本文中,我们将开发一个Spring AMQP ActiveMQ Messaging应用程序。
让我们现在开始。

使用ActiveMQ开发Spring AMQP消息传递应用程序

让我们开始使用Maven,Eclipse IDE和ActiveMQ Server开发Spring AMQP ActiveMQ Messaging应用程序。
对于所有其他Java IDE都是一样的。

请一一执行以下步骤:

  • 开发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;
              }
          });
      }
  }
}
  • 使用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) { }
  }

}
  • 最终的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消息传递应用程序开发测试客户端

  • 开发测试应用程序
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。

  • 运行单元并查看成功消息。

右键单击"测试"程序,然后将其作为Junit测试运行。

  • 要在ActiveMQ管理控制台中查看消息,请在XML文件中注释侦听器配置
<jms:listener-container container-type="default" 
     connection-factory="consumerJmsConnectionFactory" acknowledge="auto">
	 <jms:listener destination="jms/TPActiveMQQueue" 
	               ref="activeMQMessageListener" 
</jms:listener-container>

并再次运行测试类,并观察ActiveMQ管理控制台中的10条消息:

  • 并再次运行测试类,并观察ActiveMQ管理控制台中的10条消息:
    单元测试将失败,因为我们不使用任何消息。但这很好,因为我们需要在ActiveMQ队列中看到消息。