使用spring-amqp发送消息及同步接收消息

通过对spring-amqp看重要类的认识,下面来通过spring-amqp的发送消息及同步接收消息是如何实现的。有兴趣的朋友 可以去spring-amqp官网下载例子。

    先来看看HelloWorldConfiguration类

Java代码 复制代码  收藏代码
  1. package org.springframework.amqp.helloworld;   
  2.   
  3. import org.springframework.amqp.core.Queue;   
  4. import org.springframework.amqp.rabbit.config.AbstractRabbitConfiguration;   
  5. import org.springframework.amqp.rabbit.connection.ConnectionFactory;   
  6. import org.springframework.amqp.rabbit.connection.SingleConnectionFactory;   
  7. import org.springframework.amqp.rabbit.core.RabbitTemplate;   
  8. import org.springframework.context.annotation.Bean;   
  9. import org.springframework.context.annotation.Configuration;   
  10.   
  11. @Configuration  
  12. public class HelloWorldConfiguration extends AbstractRabbitConfiguration {   
  13.   
  14.     protected final String helloWorldQueueName = "hello.world.queue";   
  15.   
  16.     @Bean  
  17.     public ConnectionFactory connectionFactory() {   
  18.         SingleConnectionFactory connectionFactory = new SingleConnectionFactory(   
  19.                 "localhost");   
  20.         connectionFactory.setUsername("guest");   
  21.         connectionFactory.setPassword("guest");   
  22.         return connectionFactory;   
  23.     }   
  24.   
  25.     @Override  
  26.     public RabbitTemplate rabbitTemplate() {   
  27.         RabbitTemplate template = new RabbitTemplate(connectionFactory());   
  28.         // The routing key is set to the name of the queue by the broker for the  
  29.         // default exchange.   
  30.         template.setRoutingKey(this.helloWorldQueueName);   
  31.         // // Where we will synchronously receive messages from  
  32.         template.setQueue(this.helloWorldQueueName);   
  33.         return template;   
  34.     }   
  35.   
  36.     @Bean  
  37.     public Queue helloWorldQueue() {   
  38.         return new Queue(this.helloWorldQueueName);   
  39.     }   
  40. }  
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

发送消息的程序如下:

Java代码 复制代码  收藏代码
  1. package org.springframework.amqp.helloworld;   
  2.   
  3. import org.springframework.amqp.core.AmqpTemplate;   
  4. import org.springframework.context.ApplicationContext;   
  5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;   
  6.   
  7. public class Producer {   
  8.   
  9.     public static void main(String[] args) {   
  10.         ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);   
  11.         AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);   
  12.         amqpTemplate.convertAndSend("Hello World");   
  13.         System.out.println("Sent: Hello World");   
  14.     }   
  15.   
  16. }  
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");
	}

}

 同步接收消息如下:

Java代码 复制代码  收藏代码
  1. package org.springframework.amqp.helloworld;   
  2.   
  3. import org.springframework.amqp.core.AmqpTemplate;   
  4. import org.springframework.context.ApplicationContext;   
  5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;   
  6.   
  7. public class Consumer {   
  8.        
  9.     public static void main(String[] args) {   
  10.         ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);   
  11.         AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);   
  12.         System.out.println("Received: " + amqpTemplate.receiveAndConvert());   
  13.     }   
  14.   
  15. }  
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

 

 

Java代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">   
  5.   
  6.     <!-- 创建connectionFactory -->   
  7.     <bean id="connectionFactory"  
  8.         class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">   
  9.         <constructor-arg value="localhost" />   
  10.         <property name="username" value="guest" />   
  11.         <property name="password" value="guest" />   
  12.     </bean>   
  13.     <bean id="rabbitAdmin"  
  14.         class="org.springframework.amqp.rabbit.core.RabbitAdmin">   
  15.         <constructor-arg ref="connectionFactory" />   
  16.     </bean>   
  17.     <bean id="rabbitTemplate"  
  18.         class="org.springframework.amqp.rabbit.core.RabbitTemplate">   
  19.         <constructor-arg ref="connectionFactory"></constructor-arg>   
  20.         <property name="queue" value="hello.world.queue"></property>   
  21.         <property name="routingKey" value="hello.world.queue"></property>   
  22.     </bean>   
  23.   
  24.     <!-- 声明Queue并设定Queue的名称 -->   
  25.     <bean id="helloWorldQueue"  
  26.         class="org.springframework.amqp.core.Queue">   
  27.         <constructor-arg value="hello.world.queue"></constructor-arg>   
  28.     </bean>   
  29.        
  30. </beans>

参考:http://wubin850219.iteye.com/blog/1050328 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Bus是一个用于在分布式系统中传播状态变化的消息总线。它基于Spring Cloud Stream和Spring Cloud Config构建,可以将消息广播到整个系统中的所有服务实例。通过使用Spring Cloud Bus,可以实现配置的动态刷新、事件的传播和集群中的状态同步。 下面是使用Spring Cloud Bus自定义消息总线的步骤: 1. 添加依赖:在项目的pom.xml文件中添加Spring Cloud Bus的依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> ``` 2. 配置消息代理:在应用的配置文件中配置消息代理,例如使用RabbitMQ作为消息代理: ```yaml spring: rabbitmq: host: localhost port: 5672 username: guest password: guest ``` 3. 发送自定义消息:在需要发送自定义消息的地方,使用Spring Cloud Bus提供的API发送消息。例如,可以使用`/actuator/bus-refresh`端点发送刷新配置的消息: ```shell curl -X POST http://localhost:8080/actuator/bus-refresh ``` 4. 接收自定义消息:在需要接收自定义消息的地方,使用Spring Cloud Bus提供的注解和监听器来接收消息。例如,可以使用`@RefreshScope`注解来刷新配置: ```java @RefreshScope @RestController public class ConfigController { // ... } ``` 通过以上步骤,您可以使用Spring Cloud Bus自定义消息总线来实现配置的动态刷新、事件的传播和集群中的状态同步
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值