I. submission by transaction
channel.txSelect(); String message ="hello Xiao Ming"+ System.currentTimeMillis(); channel.basicPublish("",QUEUE,null,message.getBytes()); channel.txCommit();
II. Adopt Confirm mode (we will generally adopt asynchronous monitoring mode)
Confirm sender confirmation mode is similar to transaction, and it is also used to set Channel for sender confirmation
There are three ways to implement Confirm:
Mode 1: channel.waitForConfirms() normal sender confirmation mode;
// Create connection ConnectionFactory factory = new ConnectionFactory(); factory.setUsername(config.UserName); factory.setPassword(config.Password); factory.setVirtualHost(config.VHost); factory.setHost(config.Host); factory.setPort(config.Port); Connection conn = factory.newConnection(); // Create channel Channel channel = conn.createChannel(); // Declaration queue channel.queueDeclare(config.QueueName, false, false, false, null); // Turn on sender confirmation mode channel.confirmSelect(); String message = String.format("time => %s", new Date().getTime()); channel.basicPublish("", config.QueueName, null, message.getBytes("UTF-8")); if (channel.waitForConfirms()) { System.out.println("Message sent successfully" ); }
As you can see from the code, we just need to open the sender's confirmation mode in the channel.confirmSelect() declaration before pushing the message, and then use channel.waitForConfirms() to wait for the message to be confirmed by the server.
Mode 2: channel.waitForConfirmsOrDie() batch confirmation mode;
// Create connection ConnectionFactory factory = new ConnectionFactory(); factory.setUsername(config.UserName); factory.setPassword(config.Password); factory.setVirtualHost(config.VHost); factory.setHost(config.Host); factory.setPort(config.Port); Connection conn = factory.newConnection(); // Create channel Channel channel = conn.createChannel(); // Declaration queue channel.queueDeclare(config.QueueName, false, false, false, null); // Turn on sender confirmation mode channel.confirmSelect(); for (int i = 0; i < 10; i++) { String message = String.format("time => %s", new Date().getTime()); channel.basicPublish("", config.QueueName, null, message.getBytes("UTF-8")); } channel.waitForConfirmsOrDie(); //Until all information is released, IOException will occur as long as there is one unconfirmed message System.out.println("Complete execution");
It can be seen from the above code that channel.waitForConfirmsOrDie() uses synchronization mode to wait for all messages to be sent before executing the following code. As long as a message is not confirmed, an IOException exception will be thrown.
Mode 3: channel.addConfirmListener() asynchronously listens to the sender's confirmation mode;
// Create connection ConnectionFactory factory = new ConnectionFactory(); factory.setUsername(config.UserName); factory.setPassword(config.Password); factory.setVirtualHost(config.VHost); factory.setHost(config.Host); factory.setPort(config.Port); Connection conn = factory.newConnection(); // Create channel Channel channel = conn.createChannel(); // Declaration queue channel.queueDeclare(config.QueueName, false, false, false, null); // Turn on sender confirmation mode channel.confirmSelect(); for (int i = 0; i < 10; i++) { String message = String.format("time => %s", new Date().getTime()); channel.basicPublish("", config.QueueName, null, message.getBytes("UTF-8")); } //Asynchronous listening for acknowledged and unacknowledged messages channel.addConfirmListener(new ConfirmListener() { @Override public void handleNack(long deliveryTag, boolean multiple) throws IOException { System.out.println("Unacknowledged message, identification:" + deliveryTag); } @Override public void handleAck(long deliveryTag, boolean multiple) throws IOException { System.out.println(String.format("Confirmed message, identification:%d,Multiple messages:%b", deliveryTag, multiple)); } });
The advantage of asynchronous mode is that it has high execution efficiency. You don't need to wait for the message to finish executing, you just need to listen to the message
III. personal summary
Conclusion:
Performance:
Confirm asynchronous listening mode > = confirm batch mode > confirm normal mode > transaction mode