Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/main/java/com/rabbitmq/client/impl/AMQCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();
Expand All @@ -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);
}
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/com/rabbitmq/client/impl/ChannelN.java
Original file line number Diff line number Diff line change
Expand Up @@ -799,26 +799,18 @@ 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());
} catch (IOException | AlreadyClosedException e) {
metricsCollector.basicPublishFailure(this, e);
throw e;
}
metricsCollector.basicPublish(this, deliveryTag[0]);
metricsCollector.basicPublish(this, seqNoAssigner.seqNo);
}

/** Public API - {@inheritDoc} */
Expand Down Expand Up @@ -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");
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/rabbitmq/client/test/ChannelNTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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();
}
Expand All @@ -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);
Expand All @@ -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 {
Expand Down
Loading