1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
@Slf4j public class MyConsumer { public static final String QUEUE_NAME = "hello"; public static void main(String[] args) throws Exception{ ConnectionFactory factory = new ConnectionFactory(); factory.setHost("192.168.81.100"); factory.setUsername("root"); factory.setPassword("root"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); DeliverCallback deliverCallback = (var1, var2) -> { log.debug("var1:{}", var1); log.debug("var2:{}", new String(var2.getBody())); }; CancelCallback cancelCallback = (var1) -> { log.error("消息被中断"); }; try {
channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback); } catch (IOException e) { e.printStackTrace(); } finally { log.debug("Consumer 消费者消费消息 成功!"); channel.close(); connection.close(); } } }
|