当我门在application.yme配置好mq的信息之后如何获取到一个Channel rabbitmq: # host: 192.168.101.31 host: 127.0.0.1 # 服务器IP port: 5672 # 端口 virtual-host: / username: wt #新增用户名 password: wt!@#321$2019 publisher-confirms: true # 开启确认消息是否到达交换器,需要设置 true publisher-returns: true # 开启确认消息是否到达队列,需要设置 true listener: simple: acknowledge-mode: manual #采用手动应答 concurrency: 20 #指定最小的消费者数量 max-concurrency: 20 #指定最大的消费者数量 retry: enabled: true #是否支持重试 max-attempts: 3 initial-interval: 3000 template: reply-timeout: 120000
@Autowired private ConnectionFactory connectionFactory; //可以获取到一些mq的配置信息 // System.out.println(connectionFactory.getUsername()); // System.out.println(connectionFactory.getHost()); // System.out.println(connectionFactory.getPort());
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitAdmin; import com.rabbitmq.client.Connection; import com.rabbitmq.client.Channel;
------以上是引入的包下边是代码--------- RabbitAdmin rabbitAdmin =new RabbitAdmin(connectionFactory); RabbitTemplate template = rabbitAdmin.getRabbitTemplate();
Connection connection = (Connection) connectionFactory.createConnection(); Channel channel =connection.createChannel();