The usage of RootInvokeHook in sharding-jdbc
This article introduces the knowledge of "the use of RootInvokeHook in sharding-jdbc". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Order
This paper mainly studies the RootInvokeHook of 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 defines start and finish interfaces.
SPIRootInvokeHook
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 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 implements the RootInvokeHook interface, which registers the RootInvokeHook;rootInvokeHooks collection with NewInstanceServiceLoader and is created by NewInstanceServiceLoader.newServiceInstances; the start method traverses the rootInvokeHooks and executes its start method; the finish method traverses the rootInvokeHooks and executes the finish method
NewInstanceServiceLoader
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 > 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 type of service * @ return service instances * / @ SneakyThrows @ SuppressWarnings ("unchecked") public static Collection newServiceInstances (final Class service) {Collection 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;}}
The register method of NewInstanceServiceLoader uses ServiceLoader.load to load the implementation class of the specified service, then calls registerServiceClass to register to SERVICE_MAP;newServiceInstances to find the implementation class of the specified service, and then creates instances one by one
OpenTracingRootInvokeHook
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 implements the RootInvokeHook interface. Its start method creates and starts the activeSpan;finish method, sets CONNECTION_COUNT, and then marks activeSpan as deactivate.
AbstractConnectionAdapter
Incubator-shardingsphere-4.0.0-RC1/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/AbstractConnectionAdapter.java
@ Getterpublic abstract class AbstractConnectionAdapter extends AbstractUnsupportedOperationConnection {private final Multimap cachedConnections = LinkedHashMultimap.create (); private boolean autoCommit = true; private boolean readOnly = true; private volatile boolean closed; private int transactionIsolation = TRANSACTION_READ_UNCOMMITTED; private final ForceExecuteTemplate forceExecuteTemplate = new ForceExecuteTemplate (); private final ForceExecuteTemplate 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 () {@ Override public void execute (final Entry cachedConnections) throws SQLException {cachedConnections.getValue () .close ()) }});} finally {cachedConnections.clear (); rootInvokeHook.finish (connectionSize);}} /.}
The constructor of AbstractConnectionAdapter executes rootInvokeHook.start (); its close method executes rootInvokeHook.finish (connectionSize)
Summary
RootInvokeHook defines the start and finish interfaces; SPIRootInvokeHook implements the RootInvokeHook interface, which registers the RootInvokeHook;rootInvokeHooks collection using NewInstanceServiceLoader and is created by NewInstanceServiceLoader.newServiceInstances; the start method traverses the rootInvokeHooks and executes its start method; the finish method traverses the rootInvokeHooks and executes the finish method
This is the end of the introduction to "the use of RootInvokeHook in sharding-jdbc". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!