Skip to content

Latest commit

 

History

History
43 lines (30 loc) · 1.7 KB

spring-how-to-have-listener-dynamic-properties.md

File metadata and controls

43 lines (30 loc) · 1.7 KB

Spring - How to have Dynamic Properties in the listener

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.

Steps

  1. 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);
        }
    }
  2. Include this parser as a bean

    class MyConfiguration {
    
        public PrefetchingQueueListenerParser customParser(final Environment environment) {
            return new CustomPrefetchingQueueListenerParser(environment);
        }
    }
  3. Now all message listeners with the PrefetchingQueueListener annotation will use a random concurrency rate.