您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“sharding-jdbc中RootInvokeHook的用法”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
本文主要研究一下sharding-jdbc的RootInvokeHook
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-execute/src/main/java/org/apache/shardingsphere/core/execute/hook/RootInvokeHook.java
public interface RootInvokeHook { /** * Handle when root invoke started. */ void start(); /** * Handle when root invoke finished. * * @param connectionCount connection count */ void finish(int connectionCount); }
RootInvokeHook定義了start、finish接口
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-execute/src/main/java/org/apache/shardingsphere/core/execute/hook/SPIRootInvokeHook.java
public final class SPIRootInvokeHook implements RootInvokeHook { private final Collection<RootInvokeHook> rootInvokeHooks = NewInstanceServiceLoader.newServiceInstances(RootInvokeHook.class); static { NewInstanceServiceLoader.register(RootInvokeHook.class); } @Override public void start() { for (RootInvokeHook each : rootInvokeHooks) { each.start(); } } @Override public void finish(final int connectionCount) { for (RootInvokeHook each : rootInvokeHooks) { each.finish(connectionCount); } } }
SPIRootInvokeHook實現(xiàn)了RootInvokeHook接口,它使用NewInstanceServiceLoader注冊了RootInvokeHook;rootInvokeHooks集合由NewInstanceServiceLoader.newServiceInstances創(chuàng)建;start方法遍歷rootInvokeHooks,執(zhí)行其start方法;finish方法則遍歷rootInvokeHooks,執(zhí)行器finish方法
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/spi/NewInstanceServiceLoader.java
@NoArgsConstructor(access = AccessLevel.PRIVATE) public final class NewInstanceServiceLoader { private static final Map<Class, Collection<Class<?>>> SERVICE_MAP = new HashMap<>(); /** * Register SPI service into map for new instance. * * @param service service type * @param <T> type of service */ public static <T> void register(final Class<T> service) { for (T each : ServiceLoader.load(service)) { registerServiceClass(service, each); } } @SuppressWarnings("unchecked") private static <T> void registerServiceClass(final Class<T> service, final T instance) { Collection<Class<?>> serviceClasses = SERVICE_MAP.get(service); if (null == serviceClasses) { serviceClasses = new LinkedHashSet<>(); } serviceClasses.add(instance.getClass()); SERVICE_MAP.put(service, serviceClasses); } /** * New service instances. * * @param service service class * @param <T> type of service * @return service instances */ @SneakyThrows @SuppressWarnings("unchecked") public static <T> Collection<T> newServiceInstances(final Class<T> service) { Collection<T> result = new LinkedList<>(); if (null == SERVICE_MAP.get(service)) { return result; } for (Class<?> each : SERVICE_MAP.get(service)) { result.add((T) each.newInstance()); } return result; } }
NewInstanceServiceLoader的register方法會使用ServiceLoader.load加載指定service的實現(xiàn)類,然后調(diào)用registerServiceClass注冊到SERVICE_MAP;newServiceInstances則找到指定service的實現(xiàn)類,然后挨個創(chuàng)建實例
incubator-shardingsphere-4.0.0-RC1/sharding-opentracing/src/main/java/org/apache/shardingsphere/opentracing/hook/OpenTracingRootInvokeHook.java
public final class OpenTracingRootInvokeHook implements RootInvokeHook { public static final String ACTIVE_SPAN_CONTINUATION = "ACTIVE_SPAN_CONTINUATION"; private static final String OPERATION_NAME = "/" + ShardingTags.COMPONENT_NAME + "/rootInvoke/"; private ActiveSpan activeSpan; @Override public void start() { activeSpan = ShardingTracer.get().buildSpan(OPERATION_NAME).withTag(Tags.COMPONENT.getKey(), ShardingTags.COMPONENT_NAME).startActive(); ShardingExecuteDataMap.getDataMap().put(ACTIVE_SPAN_CONTINUATION, activeSpan.capture()); } @Override public void finish(final int connectionCount) { activeSpan.setTag(ShardingTags.CONNECTION_COUNT.getKey(), connectionCount).deactivate(); } }
OpenTracingRootInvokeHook實現(xiàn)了RootInvokeHook接口,其start方法創(chuàng)建并啟動activeSpan;finish方法則設(shè)置CONNECTION_COUNT,然后標(biāo)記activeSpan為deactivate
incubator-shardingsphere-4.0.0-RC1/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/AbstractConnectionAdapter.java
@Getter public abstract class AbstractConnectionAdapter extends AbstractUnsupportedOperationConnection { private final Multimap<String, Connection> cachedConnections = LinkedHashMultimap.create(); private boolean autoCommit = true; private boolean readOnly = true; private volatile boolean closed; private int transactionIsolation = TRANSACTION_READ_UNCOMMITTED; private final ForceExecuteTemplate<Connection> forceExecuteTemplate = new ForceExecuteTemplate<>(); private final ForceExecuteTemplate<Entry<String, Connection>> forceExecuteTemplateForClose = new ForceExecuteTemplate<>(); private final RootInvokeHook rootInvokeHook = new SPIRootInvokeHook(); private final ShardingTransactionManager shardingTransactionManager; private ShardingTransactionManagerEngine shardingTransactionManagerEngine; private TransactionType transactionType; protected AbstractConnectionAdapter(final ShardingTransactionManagerEngine shardingTransactionManagerEngine, final TransactionType transactionType) { rootInvokeHook.start(); this.transactionType = transactionType; this.shardingTransactionManagerEngine = shardingTransactionManagerEngine; shardingTransactionManager = shardingTransactionManagerEngine.getTransactionManager(transactionType); } //...... public final void close() throws SQLException { closed = true; MasterVisitedManager.clear(); TransactionTypeHolder.clear(); int connectionSize = cachedConnections.size(); try { forceExecuteTemplateForClose.execute(cachedConnections.entries(), new ForceExecuteCallback<Entry<String, Connection>>() { @Override public void execute(final Entry<String, Connection> cachedConnections) throws SQLException { cachedConnections.getValue().close(); } }); } finally { cachedConnections.clear(); rootInvokeHook.finish(connectionSize); } } //...... }
AbstractConnectionAdapter的構(gòu)造器會執(zhí)行rootInvokeHook.start();其close方法會執(zhí)行rootInvokeHook.finish(connectionSize)
RootInvokeHook定義了start、finish接口;SPIRootInvokeHook實現(xiàn)了RootInvokeHook接口,它使用NewInstanceServiceLoader注冊了RootInvokeHook;rootInvokeHooks集合由NewInstanceServiceLoader.newServiceInstances創(chuàng)建;start方法遍歷rootInvokeHooks,執(zhí)行其start方法;finish方法則遍歷rootInvokeHooks,執(zhí)行器finish方法
“sharding-jdbc中RootInvokeHook的用法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。