Skip to content

Commit 82b7ef0

Browse files
sonus21Sonu Kumar
and
Sonu Kumar
authored
Use event bus for gradle (#214)
Co-authored-by: Sonu Kumar <sonu@git>
1 parent c3d69a4 commit 82b7ef0

File tree

36 files changed

+641
-275
lines changed

36 files changed

+641
-275
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ ext {
7474

7575
subprojects {
7676
group = 'com.github.sonus21'
77-
version = '2.13.1-RELEASE'
77+
version = '2.13.2-SNAPSHOT'
7878

7979
dependencies {
8080
// https://mvnrepository.com/artifact/org.springframework/spring-messaging
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2023 Sonu Kumar
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and limitations under the License.
14+
*
15+
*/
16+
17+
package com.github.sonus21.rqueue.config;
18+
19+
import lombok.Getter;
20+
import lombok.Setter;
21+
import org.springframework.beans.factory.annotation.Value;
22+
import org.springframework.context.annotation.Configuration;
23+
24+
@Configuration
25+
@Getter
26+
@Setter
27+
public class RqueueEventBusConfig {
28+
29+
@Value("${rqueue.event.bus.core.pool.size:2}")
30+
private int corePoolSize;
31+
32+
@Value("${rqueue.event.bus.max.pool.size:10}")
33+
private int maxPoolSize;
34+
35+
@Value("${rqueue.event.bus.queue.capacity:100}")
36+
private int queueCapacity;
37+
38+
@Value("${rqueue.event.bus.keep.alive.time:60000}")
39+
private int keepAliveTime;
40+
}

rqueue-core/src/main/java/com/github/sonus21/rqueue/config/RqueueListenerBaseConfig.java

+46-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 Sonu Kumar
2+
* Copyright 2023 Sonu Kumar
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,8 @@
2828
import com.github.sonus21.rqueue.core.RqueueMessageTemplate;
2929
import com.github.sonus21.rqueue.core.RqueueRedisListenerContainerFactory;
3030
import com.github.sonus21.rqueue.core.ScheduledQueueMessageScheduler;
31+
import com.github.sonus21.rqueue.core.eventbus.EventBusErrorHandler;
32+
import com.github.sonus21.rqueue.core.eventbus.RqueueEventBus;
3133
import com.github.sonus21.rqueue.core.impl.RqueueMessageTemplateImpl;
3234
import com.github.sonus21.rqueue.dao.RqueueStringDao;
3335
import com.github.sonus21.rqueue.dao.impl.RqueueStringDaoImpl;
@@ -37,6 +39,8 @@
3739
import com.github.sonus21.rqueue.utils.condition.ReactiveEnabled;
3840
import com.github.sonus21.rqueue.utils.pebble.ResourceLoader;
3941
import com.github.sonus21.rqueue.utils.pebble.RqueuePebbleExtension;
42+
import com.google.common.eventbus.AsyncEventBus;
43+
import com.google.common.eventbus.EventBus;
4044
import com.mitchellbosecke.pebble.PebbleEngine;
4145
import com.mitchellbosecke.pebble.spring.extension.SpringExtension;
4246
import com.mitchellbosecke.pebble.spring.reactive.PebbleReactiveViewResolver;
@@ -45,11 +49,13 @@
4549
import org.springframework.beans.factory.annotation.Qualifier;
4650
import org.springframework.beans.factory.annotation.Value;
4751
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
52+
import org.springframework.context.ApplicationEventPublisher;
4853
import org.springframework.context.annotation.Bean;
4954
import org.springframework.context.annotation.Conditional;
5055
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
5156
import org.springframework.data.redis.connection.RedisConnectionFactory;
5257
import org.springframework.data.redis.core.RedisTemplate;
58+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
5359

5460
/**
5561
* This is a base configuration class for Rqueue, that is used in Spring and Spring boot Rqueue libs
@@ -105,8 +111,8 @@ protected MessageConverterProvider getMessageConverterProvider() {
105111
* Database for different ops.
106112
*
107113
* @param beanFactory configurable bean factory
108-
* @param versionKey Rqueue db version key
109-
* @param dbVersion database version
114+
* @param versionKey Rqueue db version key
115+
* @param dbVersion database version
110116
* @return {@link RedisConnectionFactory} object.
111117
*/
112118
@Bean
@@ -150,6 +156,11 @@ public RqueueWebConfig rqueueWebConfig() {
150156
return new RqueueWebConfig();
151157
}
152158

159+
@Bean
160+
public RqueueEventBusConfig rqueueEventBusConfig() {
161+
return new RqueueEventBusConfig();
162+
}
163+
153164
@Bean
154165
public RqueueSchedulerConfig rqueueSchedulerConfig() {
155166
return new RqueueSchedulerConfig();
@@ -190,8 +201,14 @@ public RqueueRedisListenerContainerFactory rqueueRedisListenerContainerFactory()
190201
* @return {@link ScheduledQueueMessageScheduler} object
191202
*/
192203
@Bean
193-
public ScheduledQueueMessageScheduler scheduledMessageScheduler() {
194-
return new ScheduledQueueMessageScheduler();
204+
public ScheduledQueueMessageScheduler scheduledMessageScheduler(
205+
RqueueSchedulerConfig rqueueSchedulerConfig,
206+
RqueueConfig rqueueConfig,
207+
RqueueEventBus eventBus,
208+
RqueueRedisListenerContainerFactory rqueueRedisListenerContainerFactory,
209+
@Qualifier("rqueueRedisLongTemplate")
210+
RedisTemplate<String, Long> redisTemplate) {
211+
return new ScheduledQueueMessageScheduler(rqueueSchedulerConfig, rqueueConfig, eventBus, rqueueRedisListenerContainerFactory, redisTemplate);
195212
}
196213

197214
/**
@@ -201,8 +218,14 @@ public ScheduledQueueMessageScheduler scheduledMessageScheduler() {
201218
* @return {@link ProcessingQueueMessageScheduler} object
202219
*/
203220
@Bean
204-
public ProcessingQueueMessageScheduler processingMessageScheduler() {
205-
return new ProcessingQueueMessageScheduler();
221+
public ProcessingQueueMessageScheduler processingMessageScheduler(
222+
RqueueSchedulerConfig rqueueSchedulerConfig,
223+
RqueueConfig rqueueConfig,
224+
RqueueEventBus eventBus,
225+
RqueueRedisListenerContainerFactory rqueueRedisListenerContainerFactory,
226+
@Qualifier("rqueueRedisLongTemplate")
227+
RedisTemplate<String, Long> redisTemplate) {
228+
return new ProcessingQueueMessageScheduler(rqueueSchedulerConfig, rqueueConfig, eventBus, rqueueRedisListenerContainerFactory, redisTemplate);
206229
}
207230

208231
@Bean
@@ -265,12 +288,27 @@ public RqueueInternalPubSubChannel rqueueInternalPubSubChannel(
265288
RqueueConfig rqueueConfig,
266289
RqueueBeanProvider rqueueBeanProvider,
267290
@Qualifier("stringRqueueRedisTemplate")
268-
RqueueRedisTemplate<String> stringRqueueRedisTemplate) {
291+
RqueueRedisTemplate<String> stringRqueueRedisTemplate) {
269292
return new RqueueInternalPubSubChannel(
270293
rqueueRedisListenerContainerFactory,
271294
rqueueMessageListenerContainer,
272295
rqueueConfig,
273296
stringRqueueRedisTemplate,
274297
rqueueBeanProvider);
275298
}
299+
300+
@Bean
301+
public RqueueEventBus rqueueEventBus(ApplicationEventPublisher applicationEventPublisher,
302+
RqueueEventBusConfig rqueueEventBusConfig) {
303+
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
304+
threadPoolTaskExecutor.setCorePoolSize(rqueueEventBusConfig.getCorePoolSize());
305+
threadPoolTaskExecutor.setMaxPoolSize(rqueueEventBusConfig.getMaxPoolSize());
306+
threadPoolTaskExecutor.setKeepAliveSeconds(rqueueEventBusConfig.getKeepAliveTime());
307+
threadPoolTaskExecutor.setQueueCapacity(rqueueEventBusConfig.getQueueCapacity());
308+
threadPoolTaskExecutor.setThreadNamePrefix("RqueueEventBusAsyncExecutor-");
309+
threadPoolTaskExecutor.initialize();
310+
EventBus eventBus = new AsyncEventBus(threadPoolTaskExecutor);
311+
eventBus.register(new EventBusErrorHandler());
312+
return new RqueueEventBus(eventBus, applicationEventPublisher);
313+
}
276314
}

rqueue-core/src/main/java/com/github/sonus21/rqueue/core/MessageScheduler.java

+23-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019-2023 Sonu Kumar
2+
* Copyright (c) 2023 Sonu Kumar
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* You may not use this file except in compliance with the License.
@@ -21,10 +21,13 @@
2121
import com.github.sonus21.rqueue.config.RqueueConfig;
2222
import com.github.sonus21.rqueue.config.RqueueSchedulerConfig;
2323
import com.github.sonus21.rqueue.core.RedisScriptFactory.ScriptType;
24+
import com.github.sonus21.rqueue.core.eventbus.RqueueEventBus;
2425
import com.github.sonus21.rqueue.listener.QueueDetail;
2526
import com.github.sonus21.rqueue.models.event.RqueueBootstrapEvent;
2627
import com.github.sonus21.rqueue.utils.Constants;
2728
import com.github.sonus21.rqueue.utils.ThreadUtils;
29+
import com.google.common.annotations.VisibleForTesting;
30+
import com.google.common.eventbus.Subscribe;
2831
import java.time.Duration;
2932
import java.util.Arrays;
3033
import java.util.List;
@@ -33,29 +36,23 @@
3336
import java.util.concurrent.ConcurrentHashMap;
3437
import java.util.concurrent.Future;
3538
import java.util.concurrent.ScheduledFuture;
36-
import com.google.common.annotations.VisibleForTesting;
3739
import org.slf4j.Logger;
3840
import org.springframework.beans.factory.DisposableBean;
39-
import org.springframework.beans.factory.annotation.Autowired;
40-
import org.springframework.beans.factory.annotation.Qualifier;
41-
import org.springframework.context.ApplicationListener;
4241
import org.springframework.data.redis.RedisSystemException;
4342
import org.springframework.data.redis.core.RedisTemplate;
4443
import org.springframework.data.redis.core.script.DefaultScriptExecutor;
4544
import org.springframework.data.redis.core.script.RedisScript;
46-
import org.springframework.scheduling.annotation.Async;
4745
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
4846
import org.springframework.util.Assert;
4947
import org.springframework.util.CollectionUtils;
5048

51-
public abstract class MessageScheduler implements DisposableBean,
52-
ApplicationListener<RqueueBootstrapEvent> {
49+
public abstract class MessageScheduler implements DisposableBean {
5350

5451
private final Object monitor = new Object();
55-
@Autowired
56-
protected RqueueSchedulerConfig rqueueSchedulerConfig;
57-
@Autowired
58-
protected RqueueConfig rqueueConfig;
52+
protected final RqueueSchedulerConfig rqueueSchedulerConfig;
53+
protected final RqueueConfig rqueueConfig;
54+
private final RqueueRedisListenerContainerFactory rqueueRedisListenerContainerFactory;
55+
private final RedisTemplate<String, Long> redisTemplate;
5956
private RedisScript<Long> redisScript;
6057
private DefaultScriptExecutor<String> defaultScriptExecutor;
6158
private Map<String, Boolean> queueRunningState;
@@ -66,14 +63,21 @@ public abstract class MessageScheduler implements DisposableBean,
6663
protected RedisScheduleTriggerHandler redisScheduleTriggerHandler;
6764

6865
private ThreadPoolTaskScheduler scheduler;
69-
@Autowired
70-
private RqueueRedisListenerContainerFactory rqueueRedisListenerContainerFactory;
7166

72-
@Autowired
73-
@Qualifier("rqueueRedisLongTemplate")
74-
private RedisTemplate<String, Long> redisTemplate;
7567
private Map<String, Integer> errorCount;
7668

69+
protected MessageScheduler(RqueueSchedulerConfig rqueueSchedulerConfig,
70+
RqueueConfig rqueueConfig,
71+
RqueueEventBus eventBus,
72+
RqueueRedisListenerContainerFactory rqueueRedisListenerContainerFactory,
73+
RedisTemplate<String, Long> redisTemplate) {
74+
this.rqueueSchedulerConfig = rqueueSchedulerConfig;
75+
this.rqueueConfig = rqueueConfig;
76+
this.rqueueRedisListenerContainerFactory = rqueueRedisListenerContainerFactory;
77+
this.redisTemplate = redisTemplate;
78+
eventBus.register(this);
79+
}
80+
7781
protected abstract Logger getLogger();
7882

7983
protected abstract long getNextScheduleTime(String queueName, long currentTime, Long value);
@@ -224,9 +228,9 @@ private void initQueue(String queueName) {
224228
queueRunningState.put(queueName, false);
225229
}
226230

227-
@Override
228-
@Async
231+
@Subscribe
229232
public void onApplicationEvent(RqueueBootstrapEvent event) {
233+
getLogger().info("{} Even received", event);
230234
synchronized (monitor) {
231235
doStop();
232236
if (!rqueueSchedulerConfig.isEnabled()) {

rqueue-core/src/main/java/com/github/sonus21/rqueue/core/ProcessingQueueMessageScheduler.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 Sonu Kumar
2+
* Copyright 2023 Sonu Kumar
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,20 +18,31 @@
1818

1919
import static java.lang.Long.max;
2020

21+
import com.github.sonus21.rqueue.config.RqueueConfig;
22+
import com.github.sonus21.rqueue.config.RqueueSchedulerConfig;
23+
import com.github.sonus21.rqueue.core.eventbus.RqueueEventBus;
2124
import com.github.sonus21.rqueue.listener.QueueDetail;
22-
import java.time.Duration;
2325
import java.util.List;
2426
import java.util.Map;
2527
import java.util.concurrent.ConcurrentHashMap;
26-
import com.github.sonus21.rqueue.utils.Constants;
2728
import lombok.extern.slf4j.Slf4j;
2829
import org.slf4j.Logger;
30+
import org.springframework.data.redis.core.RedisTemplate;
2931

3032
@Slf4j
3133
public class ProcessingQueueMessageScheduler extends MessageScheduler {
3234

3335
private Map<String, Long> queueNameToDelay;
3436

37+
public ProcessingQueueMessageScheduler(RqueueSchedulerConfig rqueueSchedulerConfig,
38+
RqueueConfig rqueueConfig, RqueueEventBus eventBus,
39+
RqueueRedisListenerContainerFactory rqueueRedisListenerContainerFactory,
40+
RedisTemplate<String, Long> redisTemplate) {
41+
super(rqueueSchedulerConfig, rqueueConfig, eventBus, rqueueRedisListenerContainerFactory,
42+
redisTemplate);
43+
}
44+
45+
3546
@Override
3647
protected void initialize() {
3748
super.initialize();

rqueue-core/src/main/java/com/github/sonus21/rqueue/core/RqueueBeanProvider.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 Sonu Kumar
2+
* Copyright 2023 Sonu Kumar
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import com.github.sonus21.rqueue.common.RqueueLockManager;
2020
import com.github.sonus21.rqueue.config.RqueueConfig;
2121
import com.github.sonus21.rqueue.config.RqueueWebConfig;
22+
import com.github.sonus21.rqueue.core.eventbus.RqueueEventBus;
2223
import com.github.sonus21.rqueue.core.support.MessageProcessor;
2324
import com.github.sonus21.rqueue.dao.RqueueJobDao;
2425
import com.github.sonus21.rqueue.dao.RqueueSystemConfigDao;
@@ -28,7 +29,6 @@
2829
import lombok.Getter;
2930
import lombok.Setter;
3031
import org.springframework.beans.factory.annotation.Autowired;
31-
import org.springframework.context.ApplicationEventPublisher;
3232

3333
@Getter
3434
@Setter
@@ -38,7 +38,7 @@ public class RqueueBeanProvider {
3838
@Autowired private RqueueSystemConfigDao rqueueSystemConfigDao;
3939
@Autowired private RqueueJobDao rqueueJobDao;
4040
@Autowired private RqueueWebConfig rqueueWebConfig;
41-
@Autowired private ApplicationEventPublisher applicationEventPublisher;
41+
@Autowired private RqueueEventBus rqueueEventBus;
4242
@Autowired private RqueueLockManager rqueueLockManager;
4343

4444
@Autowired(required = false)

rqueue-core/src/main/java/com/github/sonus21/rqueue/core/ScheduledQueueMessageScheduler.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 Sonu Kumar
2+
* Copyright 2023 Sonu Kumar
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,12 +16,25 @@
1616

1717
package com.github.sonus21.rqueue.core;
1818

19+
import com.github.sonus21.rqueue.config.RqueueConfig;
20+
import com.github.sonus21.rqueue.config.RqueueSchedulerConfig;
21+
import com.github.sonus21.rqueue.core.eventbus.RqueueEventBus;
1922
import lombok.extern.slf4j.Slf4j;
2023
import org.slf4j.Logger;
24+
import org.springframework.data.redis.core.RedisTemplate;
2125

2226
@Slf4j
2327
public class ScheduledQueueMessageScheduler extends MessageScheduler {
2428

29+
30+
public ScheduledQueueMessageScheduler(RqueueSchedulerConfig rqueueSchedulerConfig,
31+
RqueueConfig rqueueConfig, RqueueEventBus eventBus,
32+
RqueueRedisListenerContainerFactory rqueueRedisListenerContainerFactory,
33+
RedisTemplate<String, Long> redisTemplate) {
34+
super(rqueueSchedulerConfig, rqueueConfig, eventBus, rqueueRedisListenerContainerFactory,
35+
redisTemplate);
36+
}
37+
2538
@Override
2639
protected Logger getLogger() {
2740
return log;

0 commit comments

Comments
 (0)