RabbitMQConfig:
@Configuration
public class RabbitConfig {
@Autowired
private RabbitProperties properties;
/*RabbitMQ连接池,从配置文件读取参数*/
@Bean
public ConnectionFactory connectionFactory(){
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
cachingConnectionFactory.setHost(properties.getHost());
cachingConnectionFactory.setPort(properties.getPort());
cachingConnectionFactory.setUsername(properties.getUsername());
cachingConnectionFactory.setPassword(properties.getPassword());
cachingConnectionFactory.setVirtualHost(properties.getVirtualHost());
return cachingConnectionFactory;
}
/* RabbitTemplate配置 */
@Bean
public RabbitTemplate rabbitTemplate(CachingConnectionFactory cachingConnectionFactory){
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory()); //让RabbitTemplate使用连接池
return rabbitTemplate;
}
}