TasksInjector introduced

pull/3806/head
Nikita Koksharov
parent f7e0d32d41
commit d9bb9763c8

@ -305,8 +305,10 @@ public class RedissonExecutorService implements RScheduledExecutorService {
service.setSchedulerQueueName(schedulerQueueName); service.setSchedulerQueueName(schedulerQueueName);
service.setTasksExpirationTimeName(tasksExpirationTimeName); service.setTasksExpirationTimeName(tasksExpirationTimeName);
service.setTasksRetryIntervalName(tasksRetryIntervalName); service.setTasksRetryIntervalName(tasksRetryIntervalName);
service.setBeanFactory(options.getBeanFactory()); if (options.getTasksInjector() != null) {
service.setTasksInjector(options.getTasksInjector());
}
ExecutorService es = commandExecutor.getConnectionManager().getExecutor(); ExecutorService es = commandExecutor.getConnectionManager().getExecutor();
if (options.getExecutorService() != null) { if (options.getExecutorService() != null) {
es = options.getExecutorService(); es = options.getExecutorService();

@ -22,6 +22,8 @@ import java.util.concurrent.TimeUnit;
import org.redisson.api.executor.TaskListener; import org.redisson.api.executor.TaskListener;
import org.redisson.config.Config; import org.redisson.config.Config;
import org.redisson.executor.SpringTasksInjector;
import org.redisson.executor.TasksInjector;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
/** /**
@ -34,6 +36,7 @@ public final class WorkerOptions {
private int workers = 1; private int workers = 1;
private ExecutorService executorService; private ExecutorService executorService;
private TasksInjector tasksInjector;
private BeanFactory beanFactory; private BeanFactory beanFactory;
private long taskTimeout; private long taskTimeout;
private List<TaskListener> listeners = new ArrayList<>(); private List<TaskListener> listeners = new ArrayList<>();
@ -74,9 +77,14 @@ public final class WorkerOptions {
*/ */
public WorkerOptions beanFactory(BeanFactory beanFactory) { public WorkerOptions beanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory; this.beanFactory = beanFactory;
this.tasksInjector = new SpringTasksInjector(beanFactory);
return this; return this;
} }
public TasksInjector getTasksInjector() {
return tasksInjector;
}
public ExecutorService getExecutorService() { public ExecutorService getExecutorService() {
return executorService; return executorService;
} }

@ -0,0 +1,40 @@
/**
* Copyright (c) 2013-2021 Nikita Koksharov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.redisson.executor;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
/**
*
* @author Nikita Koksharov
*
*/
public class SpringTasksInjector implements TasksInjector {
private BeanFactory beanFactory;
public SpringTasksInjector(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
@Override
public void inject(Object task) {
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(beanFactory);
bpp.processInjection(task);
}
}

@ -0,0 +1,27 @@
/**
* Copyright (c) 2013-2021 Nikita Koksharov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.redisson.executor;
/**
*
* @author Nikita Koksharov
*
*/
public interface TasksInjector {
void inject(Object task);
}

@ -38,8 +38,6 @@ import org.redisson.misc.HashValue;
import org.redisson.misc.Injector; import org.redisson.misc.Injector;
import org.redisson.remote.RequestId; import org.redisson.remote.RequestId;
import org.redisson.remote.ResponseEntry; import org.redisson.remote.ResponseEntry;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ObjectInput; import java.io.ObjectInput;
@ -76,7 +74,7 @@ public class TasksRunnerService implements RemoteExecutorService {
private String tasksRetryIntervalName; private String tasksRetryIntervalName;
private String tasksExpirationTimeName; private String tasksExpirationTimeName;
private BeanFactory beanFactory; private TasksInjector tasksInjector;
private ConcurrentMap<String, ResponseEntry> responses; private ConcurrentMap<String, ResponseEntry> responses;
public TasksRunnerService(CommandAsyncExecutor commandExecutor, RedissonClient redisson, Codec codec, String name, ConcurrentMap<String, ResponseEntry> responses) { public TasksRunnerService(CommandAsyncExecutor commandExecutor, RedissonClient redisson, Codec codec, String name, ConcurrentMap<String, ResponseEntry> responses) {
@ -87,9 +85,9 @@ public class TasksRunnerService implements RemoteExecutorService {
this.codec = codec; this.codec = codec;
} }
public void setBeanFactory(BeanFactory beanFactory) { public void setTasksInjector(TasksInjector tasksInjector) {
this.beanFactory = beanFactory; this.tasksInjector = tasksInjector;
} }
public void setTasksExpirationTimeName(String tasksExpirationTimeName) { public void setTasksExpirationTimeName(String tasksExpirationTimeName) {
@ -314,10 +312,8 @@ public class TasksRunnerService implements RemoteExecutorService {
Injector.inject(task, RedissonClient.class, redisson); Injector.inject(task, RedissonClient.class, redisson);
Injector.inject(task, String.class, params.getRequestId()); Injector.inject(task, String.class, params.getRequestId());
if (beanFactory != null) { if (tasksInjector != null) {
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); tasksInjector.inject(task);
bpp.setBeanFactory(beanFactory);
bpp.processInjection(task);
} }
return task; return task;

Loading…
Cancel
Save