关于amqp的一些概念请参照http://wubin850219.iteye.com/category/152656
使用到的工具 Myeclipse、Maven
maven配置如下:
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>io.github.itanxi</groupId> <artifactId>itanxi-amqp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>SpringAmqpDemo</name> <description /> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>io.spring.platform</groupId> <artifactId>platform-bom</artifactId> <version>1.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <resources> <resource> <directory>src/test/resources</directory> <excludes> <exclude>**/*</exclude> </excludes> </resource> <resource> <directory>src/test/java</directory> <excludes> <exclude>**/*</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> </resource> </resources> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.2</version> <configuration> <version>3.0</version> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
新建一个web项目,web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringAmqpDemo</display-name> <context-param> <param-name>webAppRootKey</param-name> <param-value>itanxi.amqp</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext*.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>SetCharacterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <async-supported>true</async-supported> </filter> <filter-mapping> <filter-name>SetCharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpeg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.bmp</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.swf</url-pattern> </servlet-mapping> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:springMVC.xml </param-value> </init-param> <async-supported>true</async-supported> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>
applicationContext-send-simple-direct.xml的配置如下,这个主要是用来发送信息的
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:rabbit="http://www.springframework.org/schema/rabbit" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd"> <!-- 端口号默认是5672,可以不写,但是记得打开防火墙端口 --> <rabbit:connection-factory id="connectionFactory" host="192.168.1.175" port="5672" channel-cache-size="25" /> <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" routing-key="simpleSend"/> <rabbit:admin connection-factory="connectionFactory" /> <rabbit:queue name="simpleSend"/> <!-- <rabbit:listener-container connection-factory="connectionFactory"> <rabbit:listener ref="foo" method="listen" queue-names="myQueue" /> </rabbit:listener-container> <bean id="foo" class="io.github.itnaxi.amqp.Foo" /> --> </beans>
rabbitmq发送信息需要exchange 和routing-key根据这两个决定发送到哪个队列上,当exchange为空时,rabbitmq默认使用direct exchange将消息发送到名称为routing-key的队列上;
这里是将消息发送到队列名为simpleSend的队列上,先从最简单的来,后续跟进
接下来使用junit测试
先定义一个SpringJUnit4Log4jClassRunner类,这个主要是在junit测试用使用自己的log4j配置文件
import java.io.FileNotFoundException; import org.junit.runners.model.InitializationError; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.util.Log4jConfigurer; public class SpringJUnit4Log4jClassRunner extends SpringJUnit4ClassRunner { static { try { Log4jConfigurer.initLogging("classpath:log4j.properties"); } catch (FileNotFoundException ex) { System.err.println("Cannot Initialize log4j"); } } public SpringJUnit4Log4jClassRunner(Class<?> clazz) throws InitializationError { super(clazz); } }
然后是AbstractConfig
import org.junit.runner.RunWith; @RunWith(SpringJUnit4Log4jClassRunner.class) public abstract class AbstractConfig { }
最后开始写
import io.github.itnaxi.amqp.AbstractConfig; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.junit.Test; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageBuilder; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.core.MessagePropertiesBuilder; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.test.context.ContextConfiguration; @ContextConfiguration(locations={"/springMVC.xml","/applicationContext-send-simple-direct.xml"}) public class HomeControllerSendTest extends AbstractConfig{ private final Logger log=Logger.getLogger(HomeControllerSendTest.class); @Resource private RabbitTemplate rabbitTemplate; @Test public void testSend() throws InterruptedException { MessageProperties props = MessagePropertiesBuilder.newInstance().setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN).setMessageId("123").setHeader("bar", "baz").build(); Message message = MessageBuilder.withBody("foo".getBytes()).andProperties(props).build(); //Message message = MessageBuilder.withBody("helloword".getBytes()).build(); rabbitTemplate.send(message); log.info("send foo"); } }
执行testSend,可以看到打印结果如下:
send foo
然后登录rabbitmq的图形管理界面可以看到
表示发送成功
相关推荐
标题中的“rabbitmq学习10:使用spring-amqp发送消息及异步接收消息”表明了本次讨论的主题,即如何在Spring框架中利用Spring AMQP组件与RabbitMQ进行交互,实现消息的发送和异步接收。RabbitMQ是一个开源的消息代理...
这个“java rabbitmq spring springAMQP 代码包 project”显然是一个综合性的项目,旨在展示如何在Java环境中集成和使用RabbitMQ消息队列服务,结合Spring框架以及Spring AMQP的高级抽象来实现。接下来,我们将详细...
Spring AMQP是Spring框架的一个模块,它提供了与RabbitMQ消息中间件集成的能力,使得在Java应用中处理AMQP(Advanced Message Queuing Protocol)变得更加便捷。 首先,让我们了解RabbitMQ的基本概念。RabbitMQ是一...
Spring RabbitMQ AMQP 是一个基于Java的开源框架,它整合了RabbitMQ消息中间件,实现了高级消息队列协议(AMQP)。这个框架是Spring生态的一部分,为Spring Boot应用程序提供了强大的消息处理能力。AMQP是一种标准的...
在Spring中使用RabbitMQ时,我们首先需要配置RabbitMQ连接工厂(ConnectionFactory),然后创建一个RabbitTemplate实例,这是Spring AMQP提供的核心工具类,用于发送和接收消息。通过RabbitTemplate,我们可以使用...
RabbitMQ是一个开源的消息代理和队列服务器,广泛应用于各种业务场景,而Spring框架则是一个企业级应用开发的Java平台,提供了丰富的功能和良好的社区支持。将RabbitMQ与Spring整合,可以方便地在Spring应用中使用...
Spring AMQP 1.5.3.RELEASE API是Spring框架中的一个重要组成部分,专注于RabbitMQ消息中间件的集成。Spring AMQP(Advanced Message Queuing Protocol)允许开发者利用AMQP协议来构建可扩展、高可用的消息驱动系统...
首先,文档中提到“Spring AMQP项目将核心Spring概念应用于基于AMQP的消息传递解决方案的开发”,这说明Spring AMQP利用了Spring框架的设计理念,将消息发送和接收抽象成一种简便的模板化操作。Spring框架中的依赖...
现在,你已经成功地整合了Spring与RabbitMQ,可以发送和接收消息了。在实际应用中,你可以根据需求调整配置,如添加多个交换机、队列和路由键,或者使用更复杂的路由策略(如Fanout、Topic等)。同时,考虑使用...
Spring AMQP是一个强大的框架,它将Apache RabbitMQ的消息中间件集成到Spring应用程序中,使得开发者可以轻松地在Java应用中实现异步通信和消息队列的功能。在这个名为"spring-rabbit-stocks"的项目中,我们将深入...
该项目是一款基于SpringAMQP框架的Java语言实现的RabbitMQ消息收发Demo,项目结构包含28个文件,其中XML配置文件8个,Java源文件7个,class文件7个,yml文件4个,gitignore文件1个,以及1个markdown文件,旨在展示...
RabbitMQ则是一个流行的开源消息队列系统,它基于AMQP(Advanced Message Queuing Protocol)协议,用于高效地处理异步任务和解耦系统组件。当我们将Spring与RabbitMQ整合时,可以利用Spring的IOC(Inversion of ...
- `RabbitTemplate`:这是Spring AMQP的主要入口点,提供发送和接收消息的方法,简化了与RabbitMQ服务器的交互。 - `Message`和`MessageProperties`:`Message`代表AMQP的消息,`MessageProperties`包含消息的元...
Spring AMQP(Advanced Message Queuing Protocol)是Spring框架的一个扩展,用于支持RabbitMQ等AMQP消息代理。在这个“Spring AMQP Hello World”示例中,我们将深入探讨如何使用Spring AMQP来创建一个简单的消息...
本篇文章将深入探讨“Spring AMQP与RabbitMQ的消息确认及外部事务”这一主题。 首先,让我们了解Spring AMQP中的消息确认机制。在RabbitMQ中,消费者在接收到消息后需要进行确认,以告知服务器消息已被正确处理。...
Spring RabbitMQ RPC(远程过程调用)是一种使用RabbitMQ实现客户端与服务器间通信的方式,它允许客户端发送请求到服务器,然后服务器处理请求并返回结果。在这个场景中,RabbitMQ作为一个消息中间件,帮助解耦应用...
5. **RabbitTemplate**:这是 Spring AMQP 的主要操作接口,提供了一系列的方法来发送和接收消息。它封装了与 RabbitMQ 服务器的交互,包括直接发送、发布/订阅、路由等模式。 6. **消息转换器**:Spring AMQP 提供...
Spring AMQP 是 Spring 框架的一个重要组件,专门用于简化 RabbitMQ(一种基于 Advanced Message Queuing Protocol (AMQP) 的开源消息代理)的集成。它提供了一种高级抽象,使得开发者能够更方便地在 Spring 应用...
总的来说,“spring boot rabbitmq学习练习demo源码”是一个实用的学习资源,它可以帮助开发者理解如何在Spring Boot应用中有效地使用RabbitMQ进行消息传递和队列管理。通过实际操作和调试这个项目,你可以深入掌握...
步骤包括:安装RabbitMQ服务,引入Spring Amqp依赖,配置RabbitMQ连接信息,定义交换机、队列和绑定,以及创建发送和接收消息的组件。这样的配置能帮助你在分布式系统中实现可靠的消息传递,提高系统的稳定性和性能...