Feature - allow to use Spring's @Autowired, @Value and JSR-330 @Inject annotations in ExecutorService tasks. #1657
parent
f58c083653
commit
2bcddb1d3b
@ -0,0 +1,89 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2013-2019 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.api;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
|
||||||
|
import org.redisson.config.Config;
|
||||||
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration for RExecutorService workers.
|
||||||
|
*
|
||||||
|
* @author Nikita Koksharov
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public final class WorkerOptions {
|
||||||
|
|
||||||
|
private int workers;
|
||||||
|
private ExecutorService executorService;
|
||||||
|
private BeanFactory beanFactory;
|
||||||
|
|
||||||
|
private WorkerOptions() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static WorkerOptions defaults() {
|
||||||
|
return new WorkerOptions();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWorkers() {
|
||||||
|
return workers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines workers amount used to execute tasks.
|
||||||
|
*
|
||||||
|
* @param workers - workers amount
|
||||||
|
* @return self instance
|
||||||
|
*/
|
||||||
|
public WorkerOptions workers(int workers) {
|
||||||
|
this.workers = workers;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BeanFactory getBeanFactory() {
|
||||||
|
return beanFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines Spring BeanFactory instance to execute tasks with Spring's '@Autowired',
|
||||||
|
* '@Value' or JSR-330's '@Inject' annotation.
|
||||||
|
*
|
||||||
|
* @param beanFactory - Spring BeanFactory instance
|
||||||
|
* @return self instance
|
||||||
|
*/
|
||||||
|
public WorkerOptions beanFactory(BeanFactory beanFactory) {
|
||||||
|
this.beanFactory = beanFactory;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ExecutorService getExecutorService() {
|
||||||
|
return executorService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines custom ExecutorService to execute tasks.
|
||||||
|
* {@link Config#setExecutor(ExecutorService)} is used by default.
|
||||||
|
*
|
||||||
|
* @param executorService - custom ExecutorService
|
||||||
|
* @return self instance
|
||||||
|
*/
|
||||||
|
public WorkerOptions executorService(ExecutorService executorService) {
|
||||||
|
this.executorService = executorService;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,123 @@
|
|||||||
|
package org.redisson.executor;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.redisson.BaseTest;
|
||||||
|
import org.redisson.RedisRunner.FailedToStartRedisException;
|
||||||
|
import org.redisson.RedissonNode;
|
||||||
|
import org.redisson.api.RExecutorFuture;
|
||||||
|
import org.redisson.api.RedissonClient;
|
||||||
|
import org.redisson.api.annotation.RInject;
|
||||||
|
import org.redisson.config.Config;
|
||||||
|
import org.redisson.config.RedissonNodeConfig;
|
||||||
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
public class RedissonExecutorServiceSpringTest extends BaseTest {
|
||||||
|
|
||||||
|
public static class SampleRunnable implements Runnable, Serializable {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SampleBean bean;
|
||||||
|
|
||||||
|
@RInject
|
||||||
|
private RedissonClient redisson;
|
||||||
|
|
||||||
|
public SampleRunnable() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
String res = bean.myMethod("runnable");
|
||||||
|
redisson.getBucket("result").set(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class SampleCallable implements Callable<String>, Serializable {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SampleBean bean;
|
||||||
|
|
||||||
|
public SampleCallable() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String call() throws Exception {
|
||||||
|
return bean.myMethod("callable");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public static class SampleBean {
|
||||||
|
|
||||||
|
public String myMethod(String key) {
|
||||||
|
return "hello " + key;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String EXECUTOR_NAME = "spring_test";
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan
|
||||||
|
public static class Application {
|
||||||
|
|
||||||
|
@Bean(destroyMethod = "shutdown")
|
||||||
|
RedissonNode redissonNode(BeanFactory beanFactory) {
|
||||||
|
Config config = BaseTest.createConfig();
|
||||||
|
RedissonNodeConfig nodeConfig = new RedissonNodeConfig(config);
|
||||||
|
nodeConfig.setExecutorServiceWorkers(Collections.singletonMap(EXECUTOR_NAME, 1));
|
||||||
|
nodeConfig.setBeanFactory(beanFactory);
|
||||||
|
RedissonNode node = RedissonNode.create(nodeConfig);
|
||||||
|
node.start();
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static AnnotationConfigApplicationContext context;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void beforeTest() throws FailedToStartRedisException, IOException, InterruptedException {
|
||||||
|
context = new AnnotationConfigApplicationContext(Application.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void afterTest() {
|
||||||
|
context.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRunnable() throws InterruptedException {
|
||||||
|
redisson.getExecutorService(EXECUTOR_NAME).execute(new SampleRunnable());
|
||||||
|
|
||||||
|
Thread.sleep(500);
|
||||||
|
|
||||||
|
assertThat(redisson.getBucket("result").get()).isEqualTo("hello runnable");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCallable() throws InterruptedException {
|
||||||
|
RExecutorFuture<String> future = redisson.getExecutorService(EXECUTOR_NAME).submit(new SampleCallable());
|
||||||
|
|
||||||
|
Thread.sleep(500);
|
||||||
|
|
||||||
|
assertThat(future.sync().getNow()).isEqualTo("hello callable");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue