diff --git a/src/main/java/com/rabbitmq/client/impl/AMQCommand.java b/src/main/java/com/rabbitmq/client/impl/AMQCommand.java index bf0ddbb7b..dd4a0ea38 100644 --- a/src/main/java/com/rabbitmq/client/impl/AMQCommand.java +++ b/src/main/java/com/rabbitmq/client/impl/AMQCommand.java @@ -152,9 +152,7 @@ public void transmit(AMQChannel channel) throws IOException { throw new IllegalArgumentException(msg); } Frame methodFrame = m.toFrame(channelNumber); - if (onEncoded != null) { - onEncoded.run(); - } + runOnEncoded(); connection.writeFrame(methodFrame); connection.writeFrame(headerFrame); @@ -168,7 +166,9 @@ public void transmit(AMQChannel channel) throws IOException { } } } else { - connection.writeFrame(m.toFrame(channelNumber)); + Frame methodFrame = m.toFrame(channelNumber); + runOnEncoded(); + connection.writeFrame(methodFrame); } } finally { assemblerLock.unlock(); @@ -177,6 +177,12 @@ public void transmit(AMQChannel channel) throws IOException { connection.flush(this.writeListener); } + private void runOnEncoded() { + if (onEncoded != null) { + onEncoded.run(); + } + } + @Override public String toString() { return toString(false); } diff --git a/src/main/java/com/rabbitmq/client/impl/ChannelN.java b/src/main/java/com/rabbitmq/client/impl/ChannelN.java index f73961afa..fd73921e5 100644 --- a/src/main/java/com/rabbitmq/client/impl/ChannelN.java +++ b/src/main/java/com/rabbitmq/client/impl/ChannelN.java @@ -799,18 +799,10 @@ public void basicPublish(String exchange, String routingKey, .mandatory(mandatory) .immediate(immediate) .build(); - // the number is taken once the message is encoded, as the broker only counts messages it receives - final long[] deliveryTag = new long[1]; - Runnable takeDeliveryTag = () -> { - if (nextPublishSeqNo > 0) { - deliveryTag[0] = getNextPublishSeqNo(); - unconfirmedSet.add(deliveryTag[0]); - nextPublishSeqNo++; - } - }; + PublishSeqNoAssigner seqNoAssigner = new PublishSeqNoAssigner(); try { ObservationCollector.PublishCall publishCall = properties -> { - AMQCommand command = new AMQCommand(publish, properties, body, listener, takeDeliveryTag); + AMQCommand command = new AMQCommand(publish, properties, body, listener, seqNoAssigner); transmit(command); }; observationCollector.publish(publishCall, publish, props, body, this.connectionInfo()); @@ -818,7 +810,7 @@ public void basicPublish(String exchange, String routingKey, metricsCollector.basicPublishFailure(this, e); throw e; } - metricsCollector.basicPublish(this, deliveryTag[0]); + metricsCollector.basicPublish(this, seqNoAssigner.seqNo); } /** Public API - {@inheritDoc} */ @@ -1776,6 +1768,23 @@ private void handleAckNack(long seqNo, boolean multiple, boolean nack) { } } + /** + * Takes the publish sequence number once the message is encoded, right before it is written, + * as the broker only counts the messages it receives. + */ + private final class PublishSeqNoAssigner implements Runnable { + + private long seqNo = 0; + + @Override + public void run() { + if (nextPublishSeqNo > 0) { + seqNo = nextPublishSeqNo++; + unconfirmedSet.add(seqNo); + } + } + } + private static void validateQueueNameLength(String queue) { if(queue.length() > 255) { throw new IllegalArgumentException("queue name must be no more than 255 characters long"); diff --git a/src/test/java/com/rabbitmq/client/test/ChannelNTest.java b/src/test/java/com/rabbitmq/client/test/ChannelNTest.java index f87d7dd1c..05d354766 100644 --- a/src/test/java/com/rabbitmq/client/test/ChannelNTest.java +++ b/src/test/java/com/rabbitmq/client/test/ChannelNTest.java @@ -165,6 +165,7 @@ public void confirmSelectOnlySendsRPCCallOnce() throws Exception { @Test public void publishFailingBeforeSendingDoesNotConsumeSequenceNumber() throws Exception { ChannelN channel = channelInConfirmMode(ObservationCollector.NO_OP); + assertThat(channel.getNextPublishSeqNo()).isEqualTo(1); AMQP.BasicProperties unsupportedHeader = new AMQP.BasicProperties.Builder() .headers(Collections.singletonMap("id", UUID.randomUUID())) @@ -183,7 +184,16 @@ public void publishFailingBeforeSendingDoesNotConsumeSequenceNumber() throws Exc .isInstanceOf(IllegalArgumentException.class); assertThat(channel.getNextPublishSeqNo()).isEqualTo(1); + Mockito.when(channel.getConnection().getFrameMax()).thenReturn(4096); + AMQP.BasicProperties headersLargerThanFrameMax = new AMQP.BasicProperties.Builder() + .headers(Collections.singletonMap("h", String.join("", Collections.nCopies(5000, "a")))) + .build(); + assertThatThrownBy(() -> channel.basicPublish("", "q", headersLargerThanFrameMax, new byte[0])) + .isInstanceOf(IllegalArgumentException.class); + assertThat(channel.getNextPublishSeqNo()).isEqualTo(1); + channel.basicPublish("", "q", null, new byte[0]); + assertThat(channel.getNextPublishSeqNo()).isEqualTo(2); channel.handleCompleteInboundCommand(new AMQCommand(new AMQImpl.Basic.Ack(1, false))); assertThat(channel.waitForConfirms(1000)).isTrue(); } @@ -198,6 +208,7 @@ public void publishFailingAfterSendingKeepsSequenceNumber() throws Exception { }).when(observationCollector).publish( Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()); ChannelN channel = channelInConfirmMode(observationCollector); + assertThat(channel.getNextPublishSeqNo()).isEqualTo(1); assertThatThrownBy(() -> channel.basicPublish("", "q", null, new byte[0])) .isInstanceOf(IllegalArgumentException.class); @@ -206,6 +217,7 @@ public void publishFailingAfterSendingKeepsSequenceNumber() throws Exception { channel.handleCompleteInboundCommand(new AMQCommand(new AMQImpl.Basic.Ack(1, false))); assertThat(channel.waitForConfirms(1000)).isTrue(); + assertThat(channel.getNextPublishSeqNo()).isEqualTo(2); } private ChannelN channelInConfirmMode(ObservationCollector observationCollector) throws Exception {