2020import com .rabbitmq .client .DefaultConsumer ;
2121import com .rabbitmq .client .Envelope ;
2222import com .rabbitmq .client .ShutdownSignalException ;
23+ import org .slf4j .Logger ;
24+ import org .slf4j .LoggerFactory ;
2325
2426import java .io .ByteArrayInputStream ;
2527import java .io .DataInputStream ;
3133import java .util .concurrent .atomic .AtomicBoolean ;
3234import java .util .concurrent .atomic .AtomicInteger ;
3335import java .util .function .BiFunction ;
34- import java .util .function .BooleanSupplier ;
3536
3637public class Consumer extends AgentBase implements Runnable {
3738
38- private ConsumerImpl q ;
39+ private static final Logger LOGGER = LoggerFactory .getLogger (Consumer .class );
40+
41+ private volatile ConsumerImpl q ;
3942 private final Channel channel ;
4043 private final String id ;
4144 private final List <String > queueNames ;
@@ -53,15 +56,15 @@ public class Consumer extends AgentBase implements Runnable {
5356
5457 private final ConsumerState state ;
5558
56- private final BooleanSupplier showStopper ;
59+ private final Recovery . RecoveryProcess recoveryProcess ;
5760
5861 public Consumer (Channel channel , String id ,
5962 List <String > queueNames , int txSize , boolean autoAck ,
6063 int multiAckEvery , Stats stats , float rateLimit , int msgLimit ,
6164 int consumerLatencyInMicroSeconds ,
6265 TimestampProvider timestampProvider ,
6366 MulticastSet .CompletionHandler completionHandler ,
64- BooleanSupplier showStopper ) {
67+ Recovery . RecoveryProcess recoveryProcess ) {
6568
6669 this .channel = channel ;
6770 this .id = id ;
@@ -101,7 +104,8 @@ public Consumer(Channel channel, String id,
101104 }
102105
103106 this .state = new ConsumerState (rateLimit );
104- this .showStopper = showStopper ;
107+ this .recoveryProcess = recoveryProcess ;
108+ this .recoveryProcess .init (this );
105109 }
106110
107111 public void run () {
@@ -140,11 +144,11 @@ public void handleDelivery(String consumerTag, Envelope envelope, BasicPropertie
140144 } else if (currentMessageCount % multiAckEvery == 0 ) {
141145 channel .basicAck (envelope .getDeliveryTag (), true );
142146 }
143- }, showStopper );
147+ }, recoveryProcess );
144148 }
145149
146150 if (txSize != 0 && currentMessageCount % txSize == 0 ) {
147- dealWithWriteOperation (() -> channel .txCommit (), showStopper );
151+ dealWithWriteOperation (() -> channel .txCommit (), recoveryProcess );
148152 }
149153
150154 long diff_time = timestampProvider .getDifference (nowTimestamp , messageTimestamp );
@@ -163,7 +167,14 @@ public void handleDelivery(String consumerTag, Envelope envelope, BasicPropertie
163167
164168 @ Override
165169 public void handleShutdownSignal (String consumerTag , ShutdownSignalException sig ) {
166- countDown ();
170+ LOGGER .debug (
171+ "Consumer received shutdown signal, recovery process enabled? {}, condition to trigger connection recovery? {}" ,
172+ recoveryProcess .isEnabled (), isConnectionRecoveryTriggered (sig )
173+ );
174+ if (!recoveryProcess .isEnabled ()) {
175+ LOGGER .debug ("Counting down for consumer" );
176+ countDown ();
177+ }
167178 }
168179
169180 @ Override
@@ -185,6 +196,21 @@ private void countDown() {
185196 }
186197 }
187198
199+ @ Override
200+ public void recover (TopologyRecording topologyRecording ) {
201+ for (Map .Entry <String , String > entry : consumerTagBranchMap .entrySet ()) {
202+ TopologyRecording .RecordedQueue queue = topologyRecording .queue (entry .getValue ());
203+ try {
204+ channel .basicConsume (queue .name (), autoAck , entry .getKey (), q );
205+ } catch (IOException e ) {
206+ LOGGER .warn (
207+ "Error while recovering consumer {} on queue {} on connection {}" ,
208+ entry .getKey (), queue .name (), channel .getConnection ().getClientProvidedName (), e
209+ );
210+ }
211+ }
212+ }
213+
188214 private static class ConsumerState implements AgentState {
189215
190216 private final float rateLimit ;
0 commit comments