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 index 770734fbd62..1eafa3b70b5 100644 --- 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 @@ -1,31 +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(); -} +/* + * 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 index 30e613773a9..695d07fbae3 100644 --- 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 @@ -21,54 +21,50 @@ import org.apache.dubbo.config.bootstrap.DubboBootstrap; 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.event.annotation.AfterTestMethod; import org.springframework.test.context.junit.jupiter.SpringExtension; @ExtendWith(SpringExtension.class) @ContextConfiguration( classes = { - ServiceAnnotationTestConfiguration.class, + ProviderConfiguration.class, MethodConfigCallbackTest.class, + MethodConfigCallbackTest.MethodCallbackConfiguration.class }) @TestPropertySource(properties = { - "packagesToScan = org.apache.dubbo.config.spring.context.annotation.provider", - "consumer.version = ${demo.service.version}", - "consumer.url = dubbo://127.0.0.1:12345?version=2.5.7", + "dubbo.protocols.dubbo.port=-1", + //"dubbo.registries.my-registry.address=zookeeper://127.0.0.1:2181" }) @EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true) @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) public class MethodConfigCallbackTest { - @AfterTestMethod - public void setUp() { + @BeforeAll + public static void setUp() { DubboBootstrap.reset(); } - @Bean("referenceAnnotationBeanPostProcessor") - public ReferenceAnnotationBeanPostProcessor referenceAnnotationBeanPostProcessor() { - return new ReferenceAnnotationBeanPostProcessor(); - } - @Autowired private ConfigurableApplicationContext context; - @Bean("methodCallback") - public MethodCallback methodCallback() { - return new MethodCallbackImpl(); - } - - @DubboReference(check = false,methods = {@Method(name = "sayHello",oninvoke = "methodCallback.oninvoke",onreturn = "methodCallback.onreturn",onthrow = "methodCallback.onthrow")}) + @DubboReference(check = false, + methods = {@Method(name = "sayHello", + oninvoke = "methodCallback.oninvoke", + onreturn = "methodCallback.onreturn", + onthrow = "methodCallback.onthrow")}) private HelloService helloServiceMethodCallBack; @Test @@ -78,4 +74,14 @@ public void testMethodAnnotationCallBack() { 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 020f5d2460a..243c25e3086 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.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 ProtocolConfig protocolConfig() { @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 index 766010d0b01..e4579672d97 100644 --- 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 @@ -1,52 +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; - -public class MethodCallbackImpl implements MethodCallback { - private String onInvoke; - private String onReturn; - private String onThrow; - - @Override - public void oninvoke(String request) { - this.onInvoke = "dubbo invoke success"; - } - - @Override - public void onreturn(String response, String request) { - this.onReturn = "dubbo return success"; - } - - @Override - public void onthrow(Throwable ex, String request) { - this.onThrow = "dubbo throw exception"; - } - - public String getOnInvoke() { - return this.onInvoke; - } - - public String getOnReturn() { - return this.onReturn; - } - - public String getOnThrow() { - return this.onThrow; - } -} +/* + * 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"); + } + } + +}