You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/06/24 13:17:58 UTC

[dubbo] branch 3.0 updated: [3.0] Improve method callback (#8109)

This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new b78cfec  [3.0] Improve method callback (#8109)
b78cfec is described below

commit b78cfec47c2a6f057cd4fe637112128b1d0a20a8
Author: Gong Dewei <ky...@qq.com>
AuthorDate: Thu Jun 24 21:17:42 2021 +0800

    [3.0] Improve method callback (#8109)
    
    * Pick method callback test from #7754
    
    * Set default value of MethodConfig fields
    
    * Fix method callback test, change to remote call
    
    * Fix tests
    
    * Tests transaction of callback method
    
    Co-authored-by: chen gang <51...@qq.com>
---
 .../java/org/apache/dubbo/config/MethodConfig.java |  21 ++++
 .../dubbo/config/spring/api/MethodCallback.java    |  31 ++++++
 .../annotation/MethodConfigCallbackTest.java       |  90 +++++++++++++++++
 .../annotation/provider/ProviderConfiguration.java |  27 +++--
 .../config/spring/impl/MethodCallbackImpl.java     | 110 +++++++++++++++++++++
 5 files changed, 272 insertions(+), 7 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/MethodConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/MethodConfig.java
index 02f933b..64f48da 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/MethodConfig.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/MethodConfig.java
@@ -255,6 +255,27 @@ public class MethodConfig extends AbstractMethodConfig {
         }
     }
 
+    /**
+     * Set default field values of MethodConfig.
+     *
+     * @see org.apache.dubbo.config.annotation.Method
+     */
+    @Override
+    protected void checkDefault() {
+        super.checkDefault();
+
+        // set default field values
+        // org.apache.dubbo.config.annotation.Method.isReturn() default true;
+        if (isReturn() == null) {
+            setReturn(true);
+        }
+
+        // org.apache.dubbo.config.annotation.Method.sent() default true;
+        if (getSent() == null) {
+            setSent(true);
+        }
+    }
+
     @Override
     public void addIntoConfigManager() {
         // Don't add MethodConfig to ConfigManager
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/api/MethodCallback.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/api/MethodCallback.java
new file mode 100644
index 0000000..1eafa3b
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/api/MethodCallback.java
@@ -0,0 +1,31 @@
+/*
+ * 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.dubbo.config.spring.api;
+
+public interface MethodCallback {
+    void oninvoke(String request);
+
+    void onreturn(String response, String request);
+
+    void onthrow(Throwable ex, String request);
+
+    String getOnInvoke();
+
+    String getOnReturn();
+
+    String getOnThrow();
+}
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/MethodConfigCallbackTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/MethodConfigCallbackTest.java
new file mode 100644
index 0000000..b2f436f
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/MethodConfigCallbackTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.dubbo.config.spring.beans.factory.annotation;
+
+import org.apache.dubbo.config.annotation.DubboReference;
+import org.apache.dubbo.config.annotation.Method;
+import org.apache.dubbo.config.bootstrap.DubboBootstrap;
+import org.apache.dubbo.config.spring.ZooKeeperServer;
+import org.apache.dubbo.config.spring.api.HelloService;
+import org.apache.dubbo.config.spring.api.MethodCallback;
+import org.apache.dubbo.config.spring.context.annotation.provider.ProviderConfiguration;
+import org.apache.dubbo.config.spring.impl.MethodCallbackImpl;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.EnableAspectJAutoProxy;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.TestPropertySource;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+@ExtendWith(SpringExtension.class)
+@ContextConfiguration(
+        classes = {
+                ProviderConfiguration.class,
+                MethodConfigCallbackTest.class,
+                MethodConfigCallbackTest.MethodCallbackConfiguration.class
+        })
+@TestPropertySource(properties = {
+    "dubbo.protocol.port=-1",
+    "dubbo.registry.address=zookeeper://127.0.0.1:2181"
+})
+@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
+@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
+public class MethodConfigCallbackTest {
+
+    @BeforeAll
+    public static void setUp() {
+        DubboBootstrap.reset();
+        ZooKeeperServer.start();
+    }
+
+    @Autowired
+    private ConfigurableApplicationContext context;
+
+    @DubboReference(check = false,
+        injvm = false, // Currently local call is not supported method callback cause by Injvm protocol is not supported ClusterFilter
+        methods = {@Method(name = "sayHello",
+        oninvoke = "methodCallback.oninvoke",
+        onreturn = "methodCallback.onreturn",
+        onthrow = "methodCallback.onthrow")})
+    private HelloService helloServiceMethodCallBack;
+
+    @Test
+    public void testMethodAnnotationCallBack() {
+        helloServiceMethodCallBack.sayHello("dubbo");
+        MethodCallback notify = (MethodCallback) context.getBean("methodCallback");
+        Assertions.assertEquals("dubbo invoke success", notify.getOnInvoke());
+        Assertions.assertEquals("dubbo return success", notify.getOnReturn());
+    }
+
+    @Configuration
+    static class MethodCallbackConfiguration {
+
+        @Bean("methodCallback")
+        public MethodCallback methodCallback() {
+            return new MethodCallbackImpl();
+        }
+
+    }
+}
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/ProviderConfiguration.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/ProviderConfiguration.java
index d168af8..59e0cb3 100644
--- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/ProviderConfiguration.java
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/ProviderConfiguration.java
@@ -21,14 +21,19 @@ import org.apache.dubbo.config.ProtocolConfig;
 import org.apache.dubbo.config.RegistryConfig;
 import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Primary;
 import org.springframework.context.annotation.PropertySource;
 import org.springframework.transaction.PlatformTransactionManager;
 import org.springframework.transaction.TransactionDefinition;
 import org.springframework.transaction.TransactionException;
-import org.springframework.transaction.TransactionStatus;
 import org.springframework.transaction.annotation.EnableTransactionManagement;
+import org.springframework.transaction.support.AbstractPlatformTransactionManager;
+import org.springframework.transaction.support.DefaultTransactionStatus;
+
+import java.util.UUID;
 
 @DubboComponentScan(basePackages = "org.apache.dubbo.config.spring.context.annotation.provider")
 @PropertySource("classpath:/META-INF/default.properties")
@@ -84,21 +89,29 @@ public class ProviderConfiguration {
     @Primary
     @Bean
     public PlatformTransactionManager platformTransactionManager() {
-        return new PlatformTransactionManager() {
+        return new AbstractPlatformTransactionManager() {
+            private Logger logger = LoggerFactory.getLogger("TestPlatformTransactionManager");
 
             @Override
-            public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
-                return null;
+            protected Object doGetTransaction() throws TransactionException {
+                String transaction = "transaction_" + UUID.randomUUID().toString();
+                logger.info("Create transaction: " + transaction);
+                return transaction;
             }
 
             @Override
-            public void commit(TransactionStatus status) throws TransactionException {
-
+            protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException {
+                logger.info("Begin transaction: " + transaction);
             }
 
             @Override
-            public void rollback(TransactionStatus status) throws TransactionException {
+            protected void doCommit(DefaultTransactionStatus status) throws TransactionException {
+                logger.info("Commit transaction: " + status.getTransaction());
+            }
 
+            @Override
+            protected void doRollback(DefaultTransactionStatus status) throws TransactionException {
+                logger.info("Rollback transaction: " + status.getTransaction());
             }
         };
     }
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/impl/MethodCallbackImpl.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/impl/MethodCallbackImpl.java
new file mode 100644
index 0000000..e457967
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/impl/MethodCallbackImpl.java
@@ -0,0 +1,110 @@
+/*
+ * 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.dubbo.config.spring.impl;
+
+import org.apache.dubbo.config.spring.api.MethodCallback;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
+import org.springframework.core.env.Environment;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.support.TransactionSynchronizationManager;
+
+import javax.annotation.PostConstruct;
+
+public class MethodCallbackImpl implements MethodCallback {
+    private String onInvoke;
+    private String onReturn;
+    private String onThrow;
+
+    @Autowired
+    private Environment environment;
+
+    @Autowired
+    private ApplicationContext context;
+
+    @PostConstruct
+    protected void init() {
+        checkInjection();
+    }
+
+    @Transactional(rollbackFor = Exception.class)
+    @Override
+    public void oninvoke(String request) {
+        try {
+            checkInjection();
+            checkTranscation();
+            this.onInvoke = "dubbo invoke success";
+        } catch (Exception e) {
+            this.onInvoke = e.toString();
+            throw e;
+        }
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void onreturn(String response, String request) {
+        try {
+            checkInjection();
+            checkTranscation();
+            this.onReturn = "dubbo return success";
+        } catch (Exception e) {
+            this.onReturn = e.toString();
+            throw e;
+        }
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void onthrow(Throwable ex, String request) {
+        try {
+            checkInjection();
+            checkTranscation();
+            this.onThrow = "dubbo throw exception";
+        } catch (Exception e) {
+            this.onThrow = e.toString();
+            throw e;
+        }
+    }
+
+    public String getOnInvoke() {
+        return this.onInvoke;
+    }
+
+    public String getOnReturn() {
+        return this.onReturn;
+    }
+
+    public String getOnThrow() {
+        return this.onThrow;
+    }
+
+    private void checkInjection() {
+        if (environment == null) {
+            throw new IllegalStateException("environment is null");
+        }
+        if (context == null) {
+            throw new IllegalStateException("application context is null");
+        }
+    }
+
+    private void checkTranscation() {
+        if (!TransactionSynchronizationManager.isActualTransactionActive()) {
+            throw new IllegalStateException("No active transaction");
+        }
+    }
+
+}