mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Use bean factory to search executor service instead of hardcode it.
This commit is contained in:
parent
fdf8bda86b
commit
b83b1b78dc
@ -25,6 +25,11 @@ import org.springframework.context.annotation.Configuration;
|
||||
import com.alibaba.alicloud.oss.resource.OssStorageProtocolResolver;
|
||||
|
||||
import com.aliyun.oss.OSS;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static com.alibaba.alicloud.oss.OssConstants.OSS_TASK_EXECUTOR_BEAN_NAME;
|
||||
|
||||
/**
|
||||
* OSS Auto {@link Configuration}
|
||||
@ -42,4 +47,11 @@ public class OssAutoConfiguration {
|
||||
return new OssStorageProtocolResolver();
|
||||
}
|
||||
|
||||
@Bean(name = OSS_TASK_EXECUTOR_BEAN_NAME)
|
||||
@ConditionalOnMissingBean
|
||||
public ExecutorService ossTaskExecutor() {
|
||||
return new ThreadPoolExecutor(8, 128,
|
||||
60, TimeUnit.SECONDS, new SynchronousQueue<>());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,4 +26,6 @@ public interface OssConstants {
|
||||
String PREFIX = "spring.cloud.alibaba.oss";
|
||||
String ENABLED = PREFIX + ".enabled";
|
||||
|
||||
String OSS_TASK_EXECUTOR_BEAN_NAME = "ossTaskExecutor";
|
||||
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ public class OssStorageProtocolResolver
|
||||
if (!location.startsWith(PROTOCOL)) {
|
||||
return null;
|
||||
}
|
||||
return new OssStorageResource(getOSS(), location);
|
||||
return new OssStorageResource(getOSS(), location, beanFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -23,6 +23,7 @@ import com.aliyun.oss.model.Bucket;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.WritableResource;
|
||||
import org.springframework.util.Assert;
|
||||
@ -32,9 +33,8 @@ import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.SynchronousQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.alibaba.alicloud.oss.OssConstants.OSS_TASK_EXECUTOR_BEAN_NAME;
|
||||
|
||||
/**
|
||||
* Implements {@link Resource} for reading and writing objects in Aliyun Object Storage
|
||||
@ -58,19 +58,21 @@ public class OssStorageResource implements WritableResource {
|
||||
private final URI location;
|
||||
private final boolean autoCreateFiles;
|
||||
|
||||
private static final ExecutorService executorService = new ThreadPoolExecutor(8, 128,
|
||||
60, TimeUnit.SECONDS, new SynchronousQueue<>());
|
||||
private final ExecutorService ossTaskExecutor;
|
||||
|
||||
public OssStorageResource(OSS oss, String location) {
|
||||
this(oss, location, false);
|
||||
private final ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
public OssStorageResource(OSS oss, String location, ConfigurableListableBeanFactory beanFactory) {
|
||||
this(oss, location, beanFactory,false);
|
||||
}
|
||||
|
||||
public OssStorageResource(OSS oss, String location, boolean autoCreateFiles) {
|
||||
public OssStorageResource(OSS oss, String location,ConfigurableListableBeanFactory beanFactory, boolean autoCreateFiles) {
|
||||
Assert.notNull(oss, "Object Storage Service can not be null");
|
||||
Assert.isTrue(location.startsWith(OssStorageProtocolResolver.PROTOCOL),
|
||||
"Location must start with " + OssStorageProtocolResolver.PROTOCOL);
|
||||
this.oss = oss;
|
||||
this.autoCreateFiles = autoCreateFiles;
|
||||
this.beanFactory = beanFactory;
|
||||
try {
|
||||
URI locationUri = new URI(location);
|
||||
this.bucketName = locationUri.getAuthority();
|
||||
@ -87,6 +89,7 @@ public class OssStorageResource implements WritableResource {
|
||||
throw new IllegalArgumentException("Invalid location: " + location, e);
|
||||
}
|
||||
|
||||
this.ossTaskExecutor = this.beanFactory.getBean(OSS_TASK_EXECUTOR_BEAN_NAME, ExecutorService.class);
|
||||
}
|
||||
|
||||
public boolean isAutoCreateFiles() {
|
||||
@ -146,7 +149,7 @@ public class OssStorageResource implements WritableResource {
|
||||
@Override
|
||||
public Resource createRelative(String relativePath) throws IOException {
|
||||
return new OssStorageResource(this.oss,
|
||||
this.location.resolve(relativePath).toString());
|
||||
this.location.resolve(relativePath).toString(), this.beanFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -266,7 +269,7 @@ public class OssStorageResource implements WritableResource {
|
||||
PipedInputStream in = new PipedInputStream();
|
||||
final PipedOutputStream out = new PipedOutputStream(in);
|
||||
|
||||
executorService.submit(() -> {
|
||||
ossTaskExecutor.submit(() -> {
|
||||
try {
|
||||
OssStorageResource.this.oss.putObject(bucketName, objectKey, in);
|
||||
}
|
||||
|
@ -22,8 +22,12 @@ import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@ -35,7 +39,12 @@ import org.springframework.util.StreamUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.SynchronousQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.alibaba.alicloud.oss.OssConstants.OSS_TASK_EXECUTOR_BEAN_NAME;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@ -53,6 +62,9 @@ public class OssStorageResourceTest {
|
||||
@Rule
|
||||
public ExpectedException expectedEx = ExpectedException.none();
|
||||
|
||||
@Autowired
|
||||
private ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
@Autowired
|
||||
private OSS oss;
|
||||
|
||||
@ -99,13 +111,14 @@ public class OssStorageResourceTest {
|
||||
@Test
|
||||
public void testBucketNotEndingInSlash() {
|
||||
assertTrue(
|
||||
new OssStorageResource(this.oss, "oss://aliyun-test-bucket").isBucket());
|
||||
new OssStorageResource(this.oss, "oss://aliyun-test-bucket", beanFactory)
|
||||
.isBucket());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpecifyPathCorrect() {
|
||||
OssStorageResource ossStorageResource = new OssStorageResource(this.oss,
|
||||
"oss://aliyun-test-bucket/myfilekey", false);
|
||||
"oss://aliyun-test-bucket/myfilekey", beanFactory, false);
|
||||
|
||||
assertTrue(ossStorageResource.exists());
|
||||
}
|
||||
@ -113,7 +126,7 @@ public class OssStorageResourceTest {
|
||||
@Test
|
||||
public void testSpecifyBucketCorrect() {
|
||||
OssStorageResource ossStorageResource = new OssStorageResource(this.oss,
|
||||
"oss://aliyun-test-bucket", false);
|
||||
"oss://aliyun-test-bucket", beanFactory, false);
|
||||
|
||||
assertTrue(ossStorageResource.isBucket());
|
||||
assertEquals("aliyun-test-bucket", ossStorageResource.getBucket().getName());
|
||||
@ -176,7 +189,7 @@ public class OssStorageResourceTest {
|
||||
@Test
|
||||
public void testWritableOutputStream() throws Exception {
|
||||
String location = "oss://aliyun-test-bucket/test";
|
||||
OssStorageResource resource = new OssStorageResource(this.oss, location, true);
|
||||
OssStorageResource resource = new OssStorageResource(this.oss, location, beanFactory,true);
|
||||
OutputStream os = resource.getOutputStream();
|
||||
assertNotNull(os);
|
||||
|
||||
@ -197,7 +210,7 @@ public class OssStorageResourceTest {
|
||||
@Test
|
||||
public void testCreateBucket() {
|
||||
String location = "oss://my-new-test-bucket/";
|
||||
OssStorageResource resource = new OssStorageResource(this.oss, location, true);
|
||||
OssStorageResource resource = new OssStorageResource(this.oss, location, beanFactory, true);
|
||||
|
||||
resource.createBucket();
|
||||
|
||||
@ -212,6 +225,13 @@ public class OssStorageResourceTest {
|
||||
@Import(OssStorageProtocolResolver.class)
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean(name = OSS_TASK_EXECUTOR_BEAN_NAME)
|
||||
@ConditionalOnMissingBean
|
||||
public ExecutorService ossTaskExecutor() {
|
||||
return new ThreadPoolExecutor(8, 128,
|
||||
60, TimeUnit.SECONDS, new SynchronousQueue<>());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public static OSS mockOSS() {
|
||||
DummyOssClient dummyOssStub = new DummyOssClient();
|
||||
|
Loading…
x
Reference in New Issue
Block a user