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
|
public void asyncPublic() throws Exception { Connection connection = RabbitMQUtil.getConnection(); Channel channel = connection.createChannel(); channel.queueDeclare("publish_queue3", false, false, false, null); channel.confirmSelect();
ConcurrentHashMap<Long, String> map = new ConcurrentHashMap<>();
channel.addConfirmListener( (deliveryTag, multiple) -> { }, (deliveryTag, multiple) -> { channel.basicPublish("", "publish_queue3", null, map.get(deliveryTag).getBytes()); } );
long begin = System.currentTimeMillis(); for (int i=0; i<1000; i++) { map.put(channel.getNextPublishSeqNo(), String.valueOf(i)); channel.basicPublish("", "publish_queue3", null, String.valueOf(i).getBytes()); } long end = System.currentTimeMillis(); System.out.println("异步发布确认共耗时:" + (end-begin) + "毫秒!"); }
|