Java annotations and Spring configuration properties can't provide dynamic properties, and therefore to provide dynamic runtime configuration in Spring apps, the annotation parser can be overridden. Some annotation parsers are QueueListenerParser, PrefetchingQueueListenerParser, and FifoQueueListenerParser.
-
Extend the corresponding parser class.
public class CustomPrefetchingQueueListenerParser extends PrefetchingQueueListenerParser { private static final Random random = new Random(); public CustomPrefetchingQueueListenerParser(Environment environment) { super(environment); } @Override protected Supplier<Integer> concurrencySupplier(PrefetchingQueueListener annotation) { return () -> random.nextInt(20); } }
-
Include this parser as a bean
class MyConfiguration { public PrefetchingQueueListenerParser customParser(final Environment environment) { return new CustomPrefetchingQueueListenerParser(environment); } }
-
Now all message listeners with the PrefetchingQueueListener annotation will use a random concurrency rate.