You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/01/04 06:34:28 UTC

[GitHub] WillemJiang commented on a change in pull request #100: [SCB-100] async support

WillemJiang commented on a change in pull request #100: [SCB-100] async support
URL: https://github.com/apache/incubator-servicecomb-saga/pull/100#discussion_r159586912
 
 

 ##########
 File path: omega/omega-spring-tx/src/main/java/org/apache/servicecomb/saga/omega/transaction/spring/ExecutorFieldCallback.java
 ##########
 @@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.servicecomb.saga.omega.transaction.spring;
+
+import java.lang.invoke.MethodHandles;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Collection;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Executor;
+import java.util.stream.Collectors;
+
+import org.apache.servicecomb.saga.omega.context.OmegaContext;
+import org.apache.servicecomb.saga.omega.transaction.spring.annotations.OmegaContextAware;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.util.ReflectionUtils;
+import org.springframework.util.ReflectionUtils.FieldCallback;
+
+class ExecutorFieldCallback implements FieldCallback {
+  private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  private final OmegaContext omegaContext;
+  private final Object bean;
+
+  ExecutorFieldCallback(Object bean, OmegaContext omegaContext) {
+    this.omegaContext = omegaContext;
+    this.bean = bean;
+  }
+
+  @Override
+  public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
+    if (!field.isAnnotationPresent(OmegaContextAware.class)) {
+      return;
+    }
+
+    ReflectionUtils.makeAccessible(field);
+
+    Class<?> generic = field.getType();
+
+    if (!Executor.class.isAssignableFrom(generic)) {
+      throw new IllegalArgumentException(
+          "Only Executor, ExecutorService, and ScheduledExecutorService are supported for @"
+              + OmegaContextAware.class.getSimpleName());
+    }
+
+    field.set(bean, ExecutorProxy.newInstance(field.get(bean), field.getType(), omegaContext));
+  }
+
+  private static class RunnableProxy implements InvocationHandler {
+
+    private final String globalTxId;
+    private final String localTxId;
+    private final String parentTxId;
+    private final Object runnable;
 
 Review comment:
   If it is Runnable, we can just treat it as Runnable.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services