- 导入rabbitMQ依赖
<!--rabbitMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.连接MQ
rabbitmq:
host: ip地址
port: 5672
username: guest
password: guest
- 配置消息队列
@Configuration
public class DirectConfig {
public static final String QUEUE = "notify.conversion";
@Bean
public Queue conversionNotifyQueue() {
return new Queue(QUEUE,true);
}
}
4.依赖注入
@Autowired
private AmqpTemplate rabbitTemplate;
5.消息发送方发送消息
/*发送message到RabbitMQ*/
rabbitTemplate.convertAndSend(DirectConfig.QUEUE, conversionTaskQueue);
6.消息接收方监听消息队列获取队列数据
@Component
public class TransformNotifyReceive {
@Autowired
private FileConversionService fileConversionService;
@RabbitListener(queues = DirectConfig.QUEUE)
public void receive(ConversionTaskQueue ConversionTaskQueue) throws Exception {
fileConversionService.conversionFileUpload(ConversionTaskQueue);
}
}