-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for #1172, avoid load spi multiple times to cause gc
- Loading branch information
Showing
4 changed files
with
80 additions
and
14 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
sharding-core/src/main/java/io/shardingsphere/spi/NewInstanceServiceLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.shardingsphere.spi; | ||
|
||
import io.shardingsphere.core.exception.ShardingException; | ||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.Collection; | ||
import java.util.LinkedList; | ||
import java.util.ServiceLoader; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
/** | ||
* SPI service loader for new instance for every call. | ||
* | ||
* @author zhangliang | ||
* @param <T> type of class | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public final class NewInstanceServiceLoader<T> { | ||
|
||
private final Collection<Class<T>> serviceClasses = new CopyOnWriteArrayList<>(); | ||
|
||
/** | ||
* Creates a new service class loader for the given service type. | ||
* | ||
* @param service service type | ||
* @param <T> type of service | ||
* @return new service class loader | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static synchronized <T> NewInstanceServiceLoader<T> load(final Class<T> service) { | ||
NewInstanceServiceLoader result = new NewInstanceServiceLoader(); | ||
for (T each : ServiceLoader.load(service)) { | ||
result.serviceClasses.add(each.getClass()); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* New service instances. | ||
* | ||
* @return service instances | ||
*/ | ||
public Collection<T> newServiceInstances() { | ||
Collection<T> result = new LinkedList<>(); | ||
for (Class<T> each : serviceClasses) { | ||
try { | ||
result.add(each.newInstance()); | ||
} catch (final ReflectiveOperationException ex) { | ||
throw new ShardingException(ex); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters