提交 d9bb9763 编写于 作者: N Nikita Koksharov

TasksInjector introduced

上级 f7e0d32d
......@@ -305,8 +305,10 @@ public class RedissonExecutorService implements RScheduledExecutorService {
service.setSchedulerQueueName(schedulerQueueName);
service.setTasksExpirationTimeName(tasksExpirationTimeName);
service.setTasksRetryIntervalName(tasksRetryIntervalName);
service.setBeanFactory(options.getBeanFactory());
if (options.getTasksInjector() != null) {
service.setTasksInjector(options.getTasksInjector());
}
ExecutorService es = commandExecutor.getConnectionManager().getExecutor();
if (options.getExecutorService() != null) {
es = options.getExecutorService();
......
......@@ -22,6 +22,8 @@ import java.util.concurrent.TimeUnit;
import org.redisson.api.executor.TaskListener;
import org.redisson.config.Config;
import org.redisson.executor.SpringTasksInjector;
import org.redisson.executor.TasksInjector;
import org.springframework.beans.factory.BeanFactory;
/**
......@@ -34,6 +36,7 @@ public final class WorkerOptions {
private int workers = 1;
private ExecutorService executorService;
private TasksInjector tasksInjector;
private BeanFactory beanFactory;
private long taskTimeout;
private List<TaskListener> listeners = new ArrayList<>();
......@@ -74,9 +77,14 @@ public final class WorkerOptions {
*/
public WorkerOptions beanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
this.tasksInjector = new SpringTasksInjector(beanFactory);
return this;
}
public TasksInjector getTasksInjector() {
return tasksInjector;
}
public ExecutorService getExecutorService() {
return executorService;
}
......
/**
* 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);
}
}
/**
* 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.remote.RequestId;
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.ObjectInput;
......@@ -76,7 +74,7 @@ public class TasksRunnerService implements RemoteExecutorService {
private String tasksRetryIntervalName;
private String tasksExpirationTimeName;
private BeanFactory beanFactory;
private TasksInjector tasksInjector;
private 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;
}
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
public void setTasksInjector(TasksInjector tasksInjector) {
this.tasksInjector = tasksInjector;
}
public void setTasksExpirationTimeName(String tasksExpirationTimeName) {
......@@ -314,10 +312,8 @@ public class TasksRunnerService implements RemoteExecutorService {
Injector.inject(task, RedissonClient.class, redisson);
Injector.inject(task, String.class, params.getRequestId());
if (beanFactory != null) {
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(beanFactory);
bpp.processInjection(task);
if (tasksInjector != null) {
tasksInjector.inject(task);
}
return task;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册