|
1 | 1 | /*
|
2 |
| - * Copyright 2014-2022 the original author or authors. |
| 2 | + * Copyright 2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.kafka.listener;
|
18 | 18 |
|
19 | 19 | import java.time.Duration;
|
20 |
| -import java.time.LocalDateTime; |
| 20 | +import java.time.Instant; |
| 21 | +import java.time.ZoneId; |
21 | 22 | import java.util.Optional;
|
22 |
| -import java.util.concurrent.Executors; |
23 |
| -import java.util.concurrent.ScheduledExecutorService; |
24 |
| -import java.util.concurrent.TimeUnit; |
25 | 23 |
|
26 | 24 | import org.apache.commons.logging.LogFactory;
|
27 | 25 |
|
28 | 26 | import org.springframework.core.log.LogAccessor;
|
29 | 27 | import org.springframework.lang.NonNull;
|
30 | 28 | import org.springframework.lang.Nullable;
|
| 29 | +import org.springframework.scheduling.TaskScheduler; |
| 30 | +import org.springframework.util.Assert; |
31 | 31 |
|
32 | 32 | /**
|
33 | 33 | * Service for pausing and resuming of {@link MessageListenerContainer}.
|
34 | 34 | *
|
35 | 35 | * @author Jan Marincek
|
| 36 | + * @author Gary Russell |
36 | 37 | * @since 2.9
|
37 | 38 | */
|
38 | 39 | public class ListenerContainerPauseService {
|
39 | 40 |
|
40 | 41 | private static final LogAccessor LOGGER = new LogAccessor(LogFactory.getLog(ListenerContainerPauseService.class));
|
| 42 | + |
| 43 | + @Nullable |
41 | 44 | private final ListenerContainerRegistry registry;
|
42 |
| - private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); |
43 | 45 |
|
44 |
| - public ListenerContainerPauseService(ListenerContainerRegistry registry) { |
45 |
| - this.registry = registry; |
46 |
| - } |
| 46 | + private final TaskScheduler scheduler; |
47 | 47 |
|
48 | 48 | /**
|
49 |
| - * Pause the listener by given id. |
50 |
| - * Checks if the listener has already been requested to pause. |
51 |
| - * |
52 |
| - * @param listenerId the id of the listener |
| 49 | + * Create an instance with the provided registry and scheduler. |
| 50 | + * @param registry the registry or null. |
| 51 | + * @param scheduler the scheduler. |
53 | 52 | */
|
54 |
| - public void pause(String listenerId) { |
55 |
| - getListenerContainer(listenerId).ifPresent(this::pause); |
| 53 | + public ListenerContainerPauseService(@Nullable ListenerContainerRegistry registry, TaskScheduler scheduler) { |
| 54 | + Assert.notNull(scheduler, "'scheduler' cannot be null"); |
| 55 | + this.registry = registry; |
| 56 | + this.scheduler = scheduler; |
56 | 57 | }
|
57 | 58 |
|
58 | 59 | /**
|
59 | 60 | * Pause the listener by given id.
|
60 | 61 | * Checks if the listener has already been requested to pause.
|
61 | 62 | * Sets executor schedule for resuming the same listener after pauseDuration.
|
62 |
| - * |
63 | 63 | * @param listenerId the id of the listener
|
64 | 64 | * @param pauseDuration duration between pause() and resume() actions
|
65 | 65 | */
|
66 | 66 | public void pause(String listenerId, Duration pauseDuration) {
|
| 67 | + Assert.notNull(this.registry, "Pause by id is only supported when a registry is provided"); |
67 | 68 | getListenerContainer(listenerId)
|
68 | 69 | .ifPresent(messageListenerContainer -> pause(messageListenerContainer, pauseDuration));
|
69 | 70 | }
|
70 | 71 |
|
71 | 72 | /**
|
72 |
| - * Pause the listener by given container instance. |
73 |
| - * Checks if the listener has already been requested to pause. |
74 |
| - * |
| 73 | + * Pause the listener by given container instance. Checks if the listener has already |
| 74 | + * been requested to pause. Sets executor schedule for resuming the same listener |
| 75 | + * after pauseDuration. |
75 | 76 | * @param messageListenerContainer the listener container
|
| 77 | + * @param pauseDuration duration between pause() and resume() actions |
76 | 78 | */
|
77 |
| - public void pause(@NonNull MessageListenerContainer messageListenerContainer) { |
78 |
| - pause(messageListenerContainer, null); |
79 |
| - } |
80 |
| - |
81 |
| - /** |
82 |
| - * Pause the listener by given container instance. |
83 |
| - * Checks if the listener has already been requested to pause. |
84 |
| - * Sets executor schedule for resuming the same listener after pauseDuration. |
85 |
| - * |
86 |
| - * @param messageListenerContainer the listener container |
87 |
| - * @param pauseDuration duration between pause() and resume() actions |
88 |
| - */ |
89 |
| - public void pause(@NonNull MessageListenerContainer messageListenerContainer, @Nullable Duration pauseDuration) { |
| 79 | + public void pause(MessageListenerContainer messageListenerContainer, Duration pauseDuration) { |
90 | 80 | if (messageListenerContainer.isPauseRequested()) {
|
91 | 81 | LOGGER.debug(() -> "Container " + messageListenerContainer + " already has pause requested");
|
92 | 82 | }
|
93 | 83 | else {
|
94 |
| - LOGGER.debug(() -> "Pausing container " + messageListenerContainer); |
| 84 | + Instant resumeAt = Instant.now().plusMillis(pauseDuration.toMillis()); |
| 85 | + LOGGER.debug(() -> "Pausing container " + messageListenerContainer + "resume scheduled for " |
| 86 | + + resumeAt.atZone(ZoneId.systemDefault()).toLocalDateTime()); |
95 | 87 | messageListenerContainer.pause();
|
96 |
| - if (messageListenerContainer.getListenerId() != null && pauseDuration != null) { |
97 |
| - LOGGER.debug(() -> "Resuming of container " + messageListenerContainer + " scheduled for " + LocalDateTime.now().plus(pauseDuration)); |
98 |
| - this.executor.schedule(() -> resume(messageListenerContainer.getListenerId()), pauseDuration.toMillis(), TimeUnit.MILLISECONDS); |
99 |
| - } |
| 88 | + this.scheduler.schedule(() -> resume(messageListenerContainer), resumeAt); |
100 | 89 | }
|
101 | 90 | }
|
102 | 91 |
|
103 | 92 | /**
|
104 |
| - * Resume the listener by given id. |
105 |
| - * |
| 93 | + * Resume the listener container by given id. |
106 | 94 | * @param listenerId the id of the listener
|
107 | 95 | */
|
108 | 96 | public void resume(@NonNull String listenerId) {
|
| 97 | + Assert.notNull(this.registry, "Resume by id is only supported when a registry is provided"); |
109 | 98 | getListenerContainer(listenerId).ifPresent(this::resume);
|
110 | 99 | }
|
111 | 100 |
|
112 | 101 | /**
|
113 |
| - * Resume the listener. |
114 |
| - * |
| 102 | + * Resume the listener container. |
115 | 103 | * @param messageListenerContainer the listener container
|
116 | 104 | */
|
117 | 105 | public void resume(@NonNull MessageListenerContainer messageListenerContainer) {
|
118 |
| - if (messageListenerContainer.isContainerPaused()) { |
| 106 | + if (messageListenerContainer.isPauseRequested()) { |
119 | 107 | LOGGER.debug(() -> "Resuming container " + messageListenerContainer);
|
120 | 108 | messageListenerContainer.resume();
|
121 | 109 | }
|
|
0 commit comments