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