Skip to content

Commit

Permalink
Tests transaction of callback method #8098
Browse files Browse the repository at this point in the history
  • Loading branch information
kylixs committed Jun 22, 2021
1 parent c8cefee commit f7d10a6
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 108 deletions.
@@ -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();
}
Expand Up @@ -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
Expand All @@ -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();
}

}
}
Expand Up @@ -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")
Expand Down Expand Up @@ -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());
}
};
}
Expand Down
@@ -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");
}
}

}

0 comments on commit f7d10a6

Please sign in to comment.