1.添加依赖 配置文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2. 改linux里面rabbitmq的配置 rabbitmq /etc/rabbitmq 里面 vi rabbitmq.config。再然后重启rabbitmq
[{rabbit,[{loopback_users,[]}]}].
2.
@Configuration
public class MQConfig {
public final static String QUEUE="queue";
@Bean
public Queue queue() {
return new Queue(QUEUE,true);
}
}
@Service
public class MQSender {
private static Logger logger=LoggerFactory.getLogger(MQReceiver.class);
@Autowired
AmqpTemplate amqpTemplate;
public void send(Object message) {
String beanToString = RedisService.beanToString(message);
logger.info("send message:"+beanToString);
amqpTemplate.convertAndSend(MQConfig.QUEUE,beanToString);
}
}
@Service
public class MQReceiver {
private static Logger logger=LoggerFactory.getLogger(MQReceiver.class);
@RabbitListener(queues=MQConfig.QUEUE)
public void receive(String message) {
logger.info("receive message:"+message);
}
}
@RequestMapping("/mq")
@ResponseBody
Result<String> rabbit(){
sender.send("hello mq");
return Result.success("hello world");
}
网友评论