API(一)
依赖
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.5.0</version> </dependency>
消费者
@Slf4j public class MessageConsumer { public static void main(String[] args) throws IOException, TimeoutException { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("192.168.223.144"); connectionFactory.setPort(5672); connectionFactory.setUsername("guest"); connectionFactory.setPassword("guest"); connectionFactory.setVirtualHost("/"); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); String exchangeName = "testExchange"; String queueName = "testQueue"; String routeKey = "test.#"; channel.exchangeDeclare(exchangeName, "topic", true); channel.queueDeclare(queueName, true, false, false, null); channel.queueBind(queueName, exchangeName, routeKey); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { log.info("接收消息:{}", new String(body, "UTF-8")); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } //消息确认 channel.basicAck(envelope.getDeliveryTag(), false); } }; channel.basicConsume(queueName, consumer); } }
basicConsumer
String basicConsume(String queue, boolean autoAck, String consumerTag, boolean noLocal,
boolean exclusive, Map<String, Object> arguments, Consumer callback) throws IOException;
- queue 队列名
- autoAck 是否自动确认消息, true自动确认, false 不自动要手动调用
- consumerTag 消费者标签,用来区分多个消费者
- noLocal 设置为true,表示不能将同一个Connection中生产者发送的消息传递给这个Connection中 的消费者
- exclusive 是否排他
- arguments 消费者的参数
- callback 消费者 DefaultConsumer建立使用,重写其中的方法
@Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
channel.basicAck();确认消息
deliveryTag: 该消息的index
multiple: 是否批量. true:将一次性ack所有小于deliveryTag的消息。
限流
void basicQos(int prefetchSize, int prefetchCount, boolean global) 具体设置限流参数; 手动 ACK;
生产者
@Slf4j public class MessageProducer { public static void main(String[] args) throws IOException, TimeoutException, InterruptedException { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("192.168.223.144"); connectionFactory.setPort(5672); connectionFactory.setUsername("guest"); connectionFactory.setPassword("guest"); connectionFactory.setVirtualHost("/"); String exchangeName = "testExchange"; String routeKey = "test.a"; String routeKeyError = "abc.test"; Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); /** * 开启确认监听 */ channel.confirmSelect(); String message = "hello message "; channel.addConfirmListener(new ConfirmListener() { @Override public void handleAck(long deliveryTag, boolean multiple) throws IOException { System.err.println("-------ack!-----------" + deliveryTag); } @Override public void handleNack(long deliveryTag, boolean multiple) throws IOException { System.err.println("-------no ack!---------" + deliveryTag); } }); channel.addReturnListener(new ReturnListener() { @Override public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException { log.info("----------handle return------------"); log.info("reply Code: {}", replyCode); log.info("replyText: {}", replyText); log.info("exchange:{}", exchange); log.info("routingKey: {}", routingKey); log.info("properties: {}", properties); log.info("body: {}", new String(body)); } }); channel.basicPublish(exchangeName, routeKeyError, true, null, message.getBytes()); } }
立志如山 静心求实
浙公网安备 33010602011771号