通过对spring-amqp看重要类的认识,下面来通过spring-amqp的发送消息及同步接收消息是如何实现的。有兴趣的朋友 可以去spring-amqp官网下载例子。
先来看看HelloWorldConfiguration类
- package org.springframework.amqp.helloworld;
- import org.springframework.amqp.core.Queue;
- import org.springframework.amqp.rabbit.config.AbstractRabbitConfiguration;
- import org.springframework.amqp.rabbit.connection.ConnectionFactory;
- import org.springframework.amqp.rabbit.connection.SingleConnectionFactory;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class HelloWorldConfiguration extends AbstractRabbitConfiguration {
- protected final String helloWorldQueueName = "hello.world.queue";
- @Bean
- public ConnectionFactory connectionFactory() {
- SingleConnectionFactory connectionFactory = new SingleConnectionFactory(
- "localhost");
- connectionFactory.setUsername("guest");
- connectionFactory.setPassword("guest");
- return connectionFactory;
- }
- @Override
- public RabbitTemplate rabbitTemplate() {
- RabbitTemplate template = new RabbitTemplate(connectionFactory());
- // The routing key is set to the name of the queue by the broker for the
- // default exchange.
- template.setRoutingKey(this.helloWorldQueueName);
- // // Where we will synchronously receive messages from
- template.setQueue(this.helloWorldQueueName);
- return template;
- }
- @Bean
- public Queue helloWorldQueue() {
- return new Queue(this.helloWorldQueueName);
- }
- }
此类定义了ConnectionFactory 、RabbitTemplate 、Queue
发送消息的程序如下:
- package org.springframework.amqp.helloworld;
- import org.springframework.amqp.core.AmqpTemplate;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class Producer {
- public static void main(String[] args) {
- ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);
- AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
- amqpTemplate.convertAndSend("Hello World");
- System.out.println("Sent: Hello World");
- }
- }
同步接收消息如下:
- package org.springframework.amqp.helloworld;
- import org.springframework.amqp.core.AmqpTemplate;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class Consumer {
- public static void main(String[] args) {
- ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);
- AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
- System.out.println("Received: " + amqpTemplate.receiveAndConvert());
- }
- }
这个例子是Exchange类型为DirectExchange. routingkey的名称默认为Queue的名称。
对于 HelloWorldConfiguration类的配置,也可以通过SPRING XML文件来配置。例如
rabbitConfiguration.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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <!-- 创建connectionFactory -->
- <bean id="connectionFactory"
- class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
- <constructor-arg value="localhost" />
- <property name="username" value="guest" />
- <property name="password" value="guest" />
- </bean>
- <bean id="rabbitAdmin"
- class="org.springframework.amqp.rabbit.core.RabbitAdmin">
- <constructor-arg ref="connectionFactory" />
- </bean>
- <bean id="rabbitTemplate"
- class="org.springframework.amqp.rabbit.core.RabbitTemplate">
- <constructor-arg ref="connectionFactory"></constructor-arg>
- <property name="queue" value="hello.world.queue"></property>
- <property name="routingKey" value="hello.world.queue"></property>
- </bean>
- <!-- 声明Queue并设定Queue的名称 -->
- <bean id="helloWorldQueue"
- class="org.springframework.amqp.core.Queue">
- <constructor-arg value="hello.world.queue"></constructor-arg>
- </bean>
- </beans>
相关推荐
NULL 博文链接:https://wubin850219.iteye.com/blog/1050328
spring-cloud-config + spring-cloud-bus-amqp实现分布式集群配置动态更新,可更新实例对象,包含源码包+Rabbitmq安装包和安装说明
NULL 博文链接:https://wubin850219.iteye.com/blog/1076093
rabbitmq 路由spring-amqp 配置实现
springboot-rabbitmq-amqp
Jemter测试MQ的插件 JMeter-Rabbit-AMQP在github上17年便停止更新了,不支持rabbitmq的交换机类型“x-consistent-hash”,为此我更改了源码使其支持"x-consistent-hash
spring-rabbit-1.5.1.RELEASE.jar, spring-amqp-1.5.1.RELEASE.jar, rabbitmq-client.jar spring-retry-1.1.2.RELEASE.jar 四个包 之前的资源不好意思传错了一个
net-amqp-rabbitmq:与librabbitmq-c AMQP库的Perl绑定
使用JAVA进行运用了RabbitMQ的程序时所需的源码包,此包导入工程之后便可使用。
rabbitmq-server-3.9.11.exe
jmeter测试rabbitmq资源包,导入即用,亲测有效 导入目录:apache-jmeter-5.1.1\lib\ext 包括: ApacheJMeter-rabbitmq amqp-client-5.2.0 apache-jmeter-5.1.1(5.4.1 也可用)
RabbitMQ rabbitmq-server-3.6.12-1.el6.noarch 及其安装所需要的软件打包都在这里面,主要报卡一下软件:socat-1.7.3.2.tar.gz、rabbitmq-server-3.6.12-1.el6.noarch.rpm、rabbitmq-release-signing-key.asc、otp_...
spring-amqp ./gradlew build 如果在构建过程中遇到内存不足错误,请增加 Gradle 的可用堆和 permgen: GRADLE_OPTS='-XX:MaxPermSize=1024m\n-Xmx1024m' 要构建 jar 并将其安装到本地 Maven 缓存中: ./gradlew ...
第三章:RabbitMQ的几个API:连接、通道、交换、队列、发送消息、消费消息。 【课程目录】:---第一章:RabbitMQ介绍----1-什么是消息中间件.mp4----2-RabbitMQ消息队列安装:window环境.mp4----3-RabbitMQ消息队列...
RabbitMq与Spring整合实例,整个工程采用maven,具体过程看博文: http://blog.csdn.net/evankaka/article/details/50495437
你会使用Spring AMQP的RabbitTemplate内置应用系统来发布消息和使用一个MessageListenerAdapter POJO来订阅消息 git克隆 gradle bootRun 需要 大约几十分钟 一款文本编辑器或者IDE 你也可以从这个项目中入门代码...
AMQP 服务:Spring Boot + Spring AMQP + Rabbitmq 部署:单个可运行 jar + 嵌入式 tomcat ###邮件服务后端 邮件服务:Spring Boot + Spring Mail AMQP 服务:Spring Boot + Spring AMQP + Rabbitmq 部署:单个...
spring-amqp-logback/log4jBest practice for logging with AMQP+LOGBACK/log4j (使用Rabbitmq+logback/log4j来中心化存储你的业务日志)包含 日志发布端(logback and log4j) 和 日志接收端 两个最佳实践例子。...