Skip to content

Commit 256f9a9

Browse files
authored
Fix acking behavior of PubSub stream binder (spring-attic#2075)
Fixes a regression in the Pub/Sub stream channel adapter AUTO_ACK mode in which messages were being ACKed even though an exception was thrown.
1 parent c7b545c commit 256f9a9

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

spring-cloud-gcp-pubsub/src/main/java/org/springframework/cloud/gcp/pubsub/integration/inbound/PubSubInboundChannelAdapter.java

+4-8
Original file line numberDiff line numberDiff line change
@@ -145,26 +145,22 @@ public void nack() {
145145
});
146146
}
147147

148-
boolean messageNacked = false;
149-
150148
try {
151149
sendMessage(getMessageBuilderFactory()
152150
.withPayload(message.getPayload())
153151
.copyHeaders(messageHeaders)
154152
.build());
153+
154+
if (this.ackMode == AckMode.AUTO_ACK || this.ackMode == AckMode.AUTO) {
155+
message.ack();
156+
}
155157
}
156158
catch (RuntimeException re) {
157159
if (this.ackMode == AckMode.AUTO) {
158160
message.nack();
159-
messageNacked = true;
160161
}
161162
throw new PubSubException("Sending Spring message failed.", re);
162163
}
163-
finally {
164-
if (!messageNacked && (this.ackMode == AckMode.AUTO || this.ackMode == AckMode.AUTO_ACK)) {
165-
message.ack();
166-
}
167-
}
168164
}
169165

170166
}

spring-cloud-gcp-pubsub/src/test/java/org/springframework/cloud/gcp/pubsub/integration/inbound/PubSubInboundChannelAdapterTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ public void testAckModeAutoAck_nacksWhenDownstreamProcessingFails() {
128128
this.adapter.start();
129129
}).hasMessageContaining(EXCEPTION_MESSAGE);
130130

131+
// When exception thrown, verify that neither ack() nor nack() is called.
132+
verify(mockAcknowledgeableMessage, times(0)).ack();
131133
verify(mockAcknowledgeableMessage, times(0)).nack();
132134
}
133135

0 commit comments

Comments
 (0)