18
18
import com .rabbitmq .client .AMQP ;
19
19
import com .rabbitmq .client .ShutdownSignalException ;
20
20
21
+ import java .util .List ;
21
22
import java .util .function .BiPredicate ;
22
23
import java .util .function .Predicate ;
23
24
25
+ import static com .rabbitmq .client .impl .recovery .TopologyRecoveryRetryHandlerBuilder .builder ;
26
+
24
27
/**
25
28
* Useful ready-to-use conditions and operations for {@link DefaultRetryHandler}.
26
29
* They're composed and used with the {@link TopologyRecoveryRetryHandlerBuilder}.
32
35
*/
33
36
public abstract class TopologyRecoveryRetryLogic {
34
37
38
+ /**
39
+ * Channel has been closed because of a resource that doesn't exist.
40
+ */
35
41
public static final BiPredicate <RecordedEntity , Exception > CHANNEL_CLOSED_NOT_FOUND = (entity , ex ) -> {
36
42
if (ex .getCause () instanceof ShutdownSignalException ) {
37
43
ShutdownSignalException cause = (ShutdownSignalException ) ex .getCause ();
@@ -42,13 +48,19 @@ public abstract class TopologyRecoveryRetryLogic {
42
48
return false ;
43
49
};
44
50
51
+ /**
52
+ * Recover a channel.
53
+ */
45
54
public static final DefaultRetryHandler .RetryOperation <Void > RECOVER_CHANNEL = context -> {
46
55
if (!context .entity ().getChannel ().isOpen ()) {
47
56
context .connection ().recoverChannel (context .entity ().getChannel ());
48
57
}
49
58
return null ;
50
59
};
51
60
61
+ /**
62
+ * Recover the destination queue of a binding.
63
+ */
52
64
public static final DefaultRetryHandler .RetryOperation <Void > RECOVER_BINDING_QUEUE = context -> {
53
65
if (context .entity () instanceof RecordedQueueBinding ) {
54
66
RecordedBinding binding = context .binding ();
@@ -63,11 +75,17 @@ public abstract class TopologyRecoveryRetryLogic {
63
75
return null ;
64
76
};
65
77
78
+ /**
79
+ * Recover a binding.
80
+ */
66
81
public static final DefaultRetryHandler .RetryOperation <Void > RECOVER_BINDING = context -> {
67
82
context .binding ().recover ();
68
83
return null ;
69
84
};
70
85
86
+ /**
87
+ * Recover the queue of a consumer.
88
+ */
71
89
public static final DefaultRetryHandler .RetryOperation <Void > RECOVER_CONSUMER_QUEUE = context -> {
72
90
if (context .entity () instanceof RecordedConsumer ) {
73
91
RecordedConsumer consumer = context .consumer ();
@@ -82,5 +100,37 @@ public abstract class TopologyRecoveryRetryLogic {
82
100
return null ;
83
101
};
84
102
103
+ /**
104
+ * Recover all the bindings of the queue of a consumer.
105
+ */
106
+ public static final DefaultRetryHandler .RetryOperation <Void > RECOVER_CONSUMER_QUEUE_BINDINGS = context -> {
107
+ if (context .entity () instanceof RecordedConsumer ) {
108
+ String queue = context .consumer ().getQueue ();
109
+ for (RecordedBinding recordedBinding : context .connection ().getRecordedBindings ()) {
110
+ if (recordedBinding instanceof RecordedQueueBinding && queue .equals (recordedBinding .getDestination ())) {
111
+ recordedBinding .recover ();
112
+ }
113
+ }
114
+ }
115
+ return null ;
116
+ };
117
+
118
+ /**
119
+ * Recover a consumer.
120
+ */
85
121
public static final DefaultRetryHandler .RetryOperation <String > RECOVER_CONSUMER = context -> context .consumer ().recover ();
122
+
123
+ /**
124
+ * Pre-configured {@link DefaultRetryHandler} that retries recovery of bindings and consumers
125
+ * when their respective queue is not found.
126
+ * This retry handler can be useful for long recovery processes, whereby auto-delete queues
127
+ * can be deleted between queue recovery and binding/consumer recovery.
128
+ */
129
+ public static final RetryHandler RETRY_ON_QUEUE_NOT_FOUND_RETRY_HANDLER = builder ()
130
+ .bindingRecoveryRetryCondition (CHANNEL_CLOSED_NOT_FOUND )
131
+ .consumerRecoveryRetryCondition (CHANNEL_CLOSED_NOT_FOUND )
132
+ .bindingRecoveryRetryOperation (RECOVER_CHANNEL .andThen (RECOVER_BINDING_QUEUE ).andThen (RECOVER_BINDING ))
133
+ .consumerRecoveryRetryOperation (RECOVER_CHANNEL .andThen (RECOVER_CONSUMER_QUEUE .andThen (RECOVER_CONSUMER )
134
+ .andThen (RECOVER_CONSUMER_QUEUE_BINDINGS )))
135
+ .build ();
86
136
}
0 commit comments