spring boot 2.0 整合 rabbitMq

配置pom包

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

配置文件

配置rabbitmq的地址、端口、账号密码

spring.application.name=Spring-boot-rabbitmq

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

队列配置

@Configuration
public class RabbitConfig {

    @Bean
    public Queue Queue() {
        return new Queue("hello");
    }

}

发送消息

@Component
public class HelloSend {

	@Autowired
	private AmqpTemplate rabbitAmqpTemplate;

	public void send() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String context = "hello" + sdf.format(new Date());
		System.out.println("Sender : " + context);
		this.rabbitAmqpTemplate.convertAndSend("hello", context);
	}

}

接收消息

@Component
@RabbitListener(queues = "hello")
public class HelloReceiver {

    @RabbitHandler
    public void process(String hello) {
        System.out.println("Receiver  : " + hello);
    }

}

测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMqHelloTest {

	@Autowired
	private HelloSender helloSender;

	@Test
	public void hello() throws Exception {
		helloSender.send();
	}

}

如果遇到:SimpleMessageConverter only supports String, byte[] and Serializable payloads, received:com.example.springBoot.pojo.User 问题
  把需要传输的对象实现序列化接口就好了

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值