Skip to content

Commit 218ee6d

Browse files
committed
added "boolean isRegisteredWithDestination()" method (SPR-7065)
1 parent 3f06a92 commit 218ee6d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

org.springframework.jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe
178178

179179
private int activeInvokerCount = 0;
180180

181+
private int registeredWithDestination = 0;
182+
181183
private Runnable stopCallback;
182184

183185
private Object currentRecoveryMarker = new Object();
@@ -577,6 +579,27 @@ public final int getActiveConsumerCount() {
577579
}
578580
}
579581

582+
/**
583+
* Return whether at lease one consumer has entered a fixed registration with the
584+
* target destination. This is particularly interesting for the pub-sub case where
585+
* it might be important to have an actual consumer registered that is guaranteed
586+
* to not miss any messages that are just about to be published.
587+
* <p>This method may be polled after a {@link #start()} call, until asynchronous
588+
* registration of consumers has happened which is when the method will start returning
589+
* <code>true</code> - provided that the listener container actually ever establishes
590+
* a fixed registration. It will then keep returning <code>true</code> until shutdown,
591+
* since the container will hold on to at least one consumer registration thereafter.
592+
* <p>Note that a listener container is not bound to having a fixed registration in
593+
* the first place. It may also keep recreating consumers for every invoker execution.
594+
* This particularly depends on the {@link #setCacheLevel cache level} setting:
595+
* Only CACHE_CONSUMER will lead to a fixed registration.
596+
*/
597+
public boolean isRegisteredWithDestination() {
598+
synchronized (this.lifecycleMonitor) {
599+
return (this.registeredWithDestination > 0);
600+
}
601+
}
602+
580603

581604
/**
582605
* Create a default TaskExecutor. Called if no explicit TaskExecutor has been specified.
@@ -1026,6 +1049,9 @@ private void initResourcesIfNecessary() throws JMSException {
10261049
}
10271050
if (this.consumer == null && getCacheLevel() >= CACHE_CONSUMER) {
10281051
this.consumer = createListenerConsumer(this.session);
1052+
synchronized (lifecycleMonitor) {
1053+
registeredWithDestination++;
1054+
}
10291055
}
10301056
}
10311057
}
@@ -1047,6 +1073,11 @@ private void clearResources() {
10471073
JmsUtils.closeMessageConsumer(this.consumer);
10481074
JmsUtils.closeSession(this.session);
10491075
}
1076+
if (this.consumer != null) {
1077+
synchronized (lifecycleMonitor) {
1078+
registeredWithDestination--;
1079+
}
1080+
}
10501081
this.consumer = null;
10511082
this.session = null;
10521083
}

0 commit comments

Comments
 (0)