BillMan
BillMan

Reputation: 9944

Throttle @JmsListener in spring (prevent TaskRejectedException)

I currently have a JMSListener configured with the following threadpool:

    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5);
    executor.setMaxPoolSize(10);
    executor.initialize();

The queue I'm listening to has well over 100 messages, and when I start the listener it will process the first 10 messages with no problems, and then I will get TaskRejectedException exceptions for the the remaining messages.

My intent is that @JmsListener should not pull any new messages if there are no available threads to process said message. Does anyone know if this configuration is possible? I'm using version 1.5.3 of springboot.

-TIA

Upvotes: 0

Views: 792

Answers (1)

Gary Russell
Gary Russell

Reputation: 174769

If you don't want to lose messages you should not use an executor at all; the container will ack each message as soon as it is queued in the executor. If the system dies, any messages not yet processed will be lost.

Instead, use the container's concurrency settings to multi-thread your listener.

If you really must use an executor and don't mind losing messages, use a caller-runs policy in the executor - setRejectedExecutionHandler().

Upvotes: 1

Related Questions