code optimize commit.

This commit is contained in:
Administrator 2018-10-26 17:18:53 +08:00
parent 0d8645bcf5
commit 68c589f77c
11 changed files with 74 additions and 26 deletions

View File

@ -86,6 +86,16 @@ public class TccConfig {
*/
private int consumerThreads = Runtime.getRuntime().availableProcessors() << 1;
/**
* this is hmily async execute cancel or confirm thread size.
*/
private int asyncThreads = Runtime.getRuntime().availableProcessors() << 1;
/**
* when start this set true actor set false.
*/
private Boolean started = true;
/**
* db config.
*/

View File

@ -17,45 +17,46 @@
package com.hmily.tcc.core.service.handler;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.hmily.tcc.common.bean.context.TccTransactionContext;
import com.hmily.tcc.common.bean.entity.TccTransaction;
import com.hmily.tcc.common.config.TccConfig;
import com.hmily.tcc.common.enums.TccActionEnum;
import com.hmily.tcc.core.concurrent.threadlocal.TransactionContextLocal;
import com.hmily.tcc.core.concurrent.threadpool.HmilyThreadFactory;
import com.hmily.tcc.core.service.HmilyTransactionHandler;
import com.hmily.tcc.core.service.executor.HmilyTransactionExecutor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* this is transaction starter.
* this is hmily transaction starter.
*
* @author xiaoyu
*/
@Component
public class StarterHmilyTransactionHandler implements HmilyTransactionHandler {
private static final int MAX_THREAD = Runtime.getRuntime().availableProcessors() << 1;
public class StarterHmilyTransactionHandler implements HmilyTransactionHandler, ApplicationListener<ContextRefreshedEvent> {
private final HmilyTransactionExecutor hmilyTransactionExecutor;
private final Executor executor = new ThreadPoolExecutor(MAX_THREAD, MAX_THREAD, 0, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
HmilyThreadFactory.create("hmily-execute", false),
new ThreadPoolExecutor.AbortPolicy());
private Executor executor;
private final TccConfig tccConfig;
@Autowired
public StarterHmilyTransactionHandler(final HmilyTransactionExecutor hmilyTransactionExecutor) {
public StarterHmilyTransactionHandler(final HmilyTransactionExecutor hmilyTransactionExecutor, TccConfig tccConfig) {
this.hmilyTransactionExecutor = hmilyTransactionExecutor;
this.tccConfig = tccConfig;
}
@Override
public Object handler(final ProceedingJoinPoint point, final TccTransactionContext context)
throws Throwable {
@ -78,9 +79,21 @@ public class StarterHmilyTransactionHandler implements HmilyTransactionHandler {
final TccTransaction currentTransaction = hmilyTransactionExecutor.getCurrentTransaction();
executor.execute(() -> hmilyTransactionExecutor.confirm(currentTransaction));
} finally {
TransactionContextLocal.getInstance().remove();
TransactionContextLocal.getInstance().remove();
hmilyTransactionExecutor.remove();
}
return returnValue;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (tccConfig.getStarted()) {
executor = new ThreadPoolExecutor(tccConfig.getAsyncThreads(),
tccConfig.getAsyncThreads(), 0, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
HmilyThreadFactory.create("hmily-execute", false),
new ThreadPoolExecutor.AbortPolicy());
}
}
}

View File

@ -132,6 +132,10 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

View File

@ -19,6 +19,7 @@ package com.hmily.tcc.demo.dubbo.account;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@ -38,7 +39,9 @@ public class DubboTccAccountApplication {
* @param args args.
*/
public static void main(final String[] args) {
SpringApplication.run(DubboTccAccountApplication.class, args);
SpringApplication springApplication = new SpringApplication(DubboTccAccountApplication.class);
springApplication.setWebApplicationType(WebApplicationType.NONE);
springApplication.run(args);
}

View File

@ -14,11 +14,12 @@ spring:
hmily:
tcc :
serializer : kryo
recoverDelayTime : 128
retryMax : 3
scheduledDelay : 128
recoverDelayTime : 100
retryMax : 5
scheduledDelay : 100
scheduledThreadMax : 10
repositorySupport : db
started : false
tccDbConfig :
driverClassName : com.mysql.jdbc.Driver
url : jdbc:mysql://192.168.1.98:3306/tcc?useUnicode=true&amp;characterEncoding=utf8

View File

@ -126,6 +126,10 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -19,6 +19,7 @@ package com.hmily.tcc.demo.dubbo.inventory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@ -39,7 +40,9 @@ public class DubboTccInventoryApplication {
* @param args args
*/
public static void main(final String[] args) {
SpringApplication.run(DubboTccInventoryApplication.class, args);
SpringApplication springApplication = new SpringApplication(DubboTccInventoryApplication.class);
springApplication.setWebApplicationType(WebApplicationType.NONE);
springApplication.run(args);
}

View File

@ -22,6 +22,7 @@
<property name="scheduledDelay" value="120"/>
<property name="scheduledThreadMax" value="4"/>
<property name="repositorySupport" value="db"/>
<property name="started" value="false"/>
<property name="tccDbConfig">
<bean class="com.hmily.tcc.common.config.TccDbConfig">
<property name="url"

View File

@ -19,6 +19,8 @@ hmily:
scheduledDelay : 128
scheduledThreadMax : 10
repositorySupport : db
started: true
async-threads: 32
tccDbConfig :
driverClassName : com.mysql.jdbc.Driver
url : jdbc:mysql://192.168.1.98:3306/tcc?useUnicode=true&amp;characterEncoding=utf8

View File

@ -20,6 +20,7 @@
package com.hmily.tcc.spring.boot.starter.parent.config;
import com.hmily.tcc.common.config.TccConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ -28,7 +29,7 @@ import org.springframework.stereotype.Component;
*
* @author xiaoyu(Myth)
*/
@Component
@Component("tccConfig")
@ConfigurationProperties(prefix = "hmily.tcc")
public class TccConfigProperties extends TccConfig {
}

View File

@ -23,6 +23,7 @@ import com.hmily.tcc.core.bootstrap.HmilyTransactionBootstrap;
import com.hmily.tcc.core.service.HmilyInitService;
import com.hmily.tcc.spring.boot.starter.parent.config.TccConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@ -31,6 +32,7 @@ import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* HmilyAutoConfiguration is spring boot starter handler.
*
* @author xiaoyu(Myth)
*/
@Configuration
@ -48,7 +50,8 @@ public class HmilyAutoConfiguration {
}
@Bean
public HmilyTransactionBootstrap tccTransactionBootstrap(HmilyInitService hmilyInitService) {
@Qualifier("hmilyTransactionBootstrap")
public HmilyTransactionBootstrap hmilyTransactionBootstrap(HmilyInitService hmilyInitService) {
final HmilyTransactionBootstrap hmilyTransactionBootstrap = new HmilyTransactionBootstrap(hmilyInitService);
hmilyTransactionBootstrap.setBufferSize(tccConfigProperties.getBufferSize());
hmilyTransactionBootstrap.setRetryMax(tccConfigProperties.getRetryMax());
@ -63,6 +66,9 @@ public class HmilyAutoConfiguration {
hmilyTransactionBootstrap.setTccRedisConfig(tccConfigProperties.getTccRedisConfig());
hmilyTransactionBootstrap.setTccZookeeperConfig(tccConfigProperties.getTccZookeeperConfig());
hmilyTransactionBootstrap.setTccMongoConfig(tccConfigProperties.getTccMongoConfig());
hmilyTransactionBootstrap.setConsumerThreads(tccConfigProperties.getConsumerThreads());
hmilyTransactionBootstrap.setLoadFactor(tccConfigProperties.getLoadFactor());
hmilyTransactionBootstrap.setAsyncThreads(tccConfigProperties.getAsyncThreads());
return hmilyTransactionBootstrap;
}
}