diff --git a/threadutiltongyong/.gitignore b/threadutiltongyong/.gitignore new file mode 100644 index 0000000..682d4ed --- /dev/null +++ b/threadutiltongyong/.gitignore @@ -0,0 +1,27 @@ +# Created by .ignore support plugin (hsz.mobi) +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +/.idea/ +/target/ diff --git a/threadutiltongyong/pom.xml b/threadutiltongyong/pom.xml new file mode 100644 index 0000000..34daee9 --- /dev/null +++ b/threadutiltongyong/pom.xml @@ -0,0 +1,181 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.9.RELEASE + + + + com.ayi + woniu-web + 0.0.1-SNAPSHOT + woniu-web + create by woniu + + jar + + + 1.8 + + + + + + + com.github.rholder + guava-retrying + 2.0.0 + + + + com.google.guava + guava + 29.0-jre + + + + org.lionsoul + ip2region + 2.7.0 + + + + org.springframework.boot + spring-boot-starter-aop + + + cn.hutool + hutool-all + 5.3.3 + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-test + + + + org.projectlombok + lombok + + + org.springframework.boot + spring-boot-starter-cache + + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.3.2 + + + + com.alibaba + druid-spring-boot-starter + 1.2.18 + + + + + + + + mysql + mysql-connector-java + 8.0.29 + + + + com.alipay.sofa + runtime-sofa-boot-starter + 3.18.0 + + + + org.springframework.boot + spring-boot-starter-validation + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + com.github.ben-manes.caffeine + caffeine + 2.9.3 + + + + com.alibaba + fastjson + 1.2.76 + + + com.alibaba + easyexcel + 3.0.5 + + + org.testng + testng + RELEASE + compile + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/threadutiltongyong/src/main/java/com/woniu/WoNiuApplication.java b/threadutiltongyong/src/main/java/com/woniu/WoNiuApplication.java new file mode 100644 index 0000000..1ebf2e3 --- /dev/null +++ b/threadutiltongyong/src/main/java/com/woniu/WoNiuApplication.java @@ -0,0 +1,16 @@ +package com.woniu; + +import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + + +@SpringBootApplication(exclude = MybatisAutoConfiguration.class) +public class WoNiuApplication { + + public static void main(String[] args) { + SpringApplication.run(WoNiuApplication.class, args); + } + + +} diff --git a/threadutiltongyong/src/main/java/com/woniu/threaddemo/BizApiException.java b/threadutiltongyong/src/main/java/com/woniu/threaddemo/BizApiException.java new file mode 100644 index 0000000..e1801c4 --- /dev/null +++ b/threadutiltongyong/src/main/java/com/woniu/threaddemo/BizApiException.java @@ -0,0 +1,13 @@ +package com.woniu.threaddemo; + +import lombok.Data; + +@Data +public class BizApiException extends RuntimeException { + public BizApiException() { + } + + public BizApiException(String message) { + super(message); + } +} diff --git a/threadutiltongyong/src/main/java/com/woniu/threaddemo/CompletableFutureDemo.java b/threadutiltongyong/src/main/java/com/woniu/threaddemo/CompletableFutureDemo.java new file mode 100644 index 0000000..bcee8e9 --- /dev/null +++ b/threadutiltongyong/src/main/java/com/woniu/threaddemo/CompletableFutureDemo.java @@ -0,0 +1,55 @@ +package com.woniu.threaddemo; + +import java.util.Collection; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.stream.Collectors; + + +public class CompletableFutureDemo { + + /** + * 线程池直接这样写了先,真实业务中不是这样搞的哈! + */ + private final static ExecutorService executorService = Executors.newFixedThreadPool(4); + + /** + * 创建并行任务并执行 + * + * @param list 数据源 + * @param api API调用逻辑 + * @param exceptionHandle 异常处理逻辑 + * @param 数据源类型 + * @param 程序返回类型 + * @return 处理结果列表 + */ + public List parallelFutureJoin(Collection list, Function api, BiFunction exceptionHandle) { + //规整所有任务 + List> collectFuture = list.stream() + .map(s -> this.createFuture(() -> api.apply(s), e -> exceptionHandle.apply(e, s))).collect(Collectors.toList()); + //汇总所有任务,并执行join,全部执行完成后,统一返回 + return collectFuture.stream() + .map(CompletableFuture::join) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + /** + * 创建单个CompletableFuture任务 + * + * @param logic 任务逻辑 + * @param exceptionHandle 异常处理 + * @param 类型 + * @return 任务 + */ + public CompletableFuture createFuture(Supplier logic, Function exceptionHandle) { + return CompletableFuture.supplyAsync(logic, executorService).exceptionally(exceptionHandle); + } + +} diff --git a/threadutiltongyong/src/main/java/com/woniu/threaddemo/MainTest.java b/threadutiltongyong/src/main/java/com/woniu/threaddemo/MainTest.java new file mode 100644 index 0000000..5648197 --- /dev/null +++ b/threadutiltongyong/src/main/java/com/woniu/threaddemo/MainTest.java @@ -0,0 +1,27 @@ +package com.woniu.threaddemo; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import java.util.Arrays; +import java.util.List; + +@Slf4j +@Component +public class MainTest { + public static void main(String[] args) { + //CompletableFuture 多线程工具类通用实现 + CompletableFutureDemo demo = new CompletableFutureDemo(); + List numList = demo.parallelFutureJoin(Arrays.asList(1, 2, 3), num -> { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + log.error("exception", e); + } + return num; + }, (e, num) -> { + log.error("我异常了,我刚才在处理数字:" + num + ",异常原因:" + e); + return -1; + }); + log.info("numList:{}", numList); + } +} diff --git a/threadutiltongyong/src/main/java/com/woniu/vo/User.java b/threadutiltongyong/src/main/java/com/woniu/vo/User.java new file mode 100644 index 0000000..f79c77c --- /dev/null +++ b/threadutiltongyong/src/main/java/com/woniu/vo/User.java @@ -0,0 +1,10 @@ +package com.woniu.vo; + +import lombok.Data; + +@Data +public class User { + private Long id; + private String name; + private Integer sex; +} diff --git a/threadutiltongyong/src/main/resources/application.properties b/threadutiltongyong/src/main/resources/application.properties new file mode 100644 index 0000000..8ce8c75 --- /dev/null +++ b/threadutiltongyong/src/main/resources/application.properties @@ -0,0 +1,52 @@ +# +spring.application.name=www +server.port=8081 +##1800s +#server.servlet.session.timeout=1800 +#spring.jackson.time-zone=GMT+8 +#spring.jackson.date-format=yyyy-MM-dd HH:mm:ss +# +##Դ +#spring.shardingsphere.datasource.names=m1,m2 +# +spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=utf8 +spring.datasource.username=root +spring.datasource.password=123456 +spring.datasource.driver\class\name=com.mysql.cj.jdbc.Driver +spring.datasource.type=com.alibaba.druid.pool.DruidDataSource +# +# +#spring.shardingsphere.datasource.m2.url=jdbc:mysql://127.0.0.1:3306/test2?serverTimezone=GMT%2b8&useUnicode=true&characterEncoding=utf8 +#spring.shardingsphere.datasource.m2.username=root +#spring.shardingsphere.datasource.m2.password=123456 +#spring.shardingsphere.datasource.m2.driver\class\name=com.mysql.cj.jdbc.Driver +#spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource +# +## ָt_userݷֲݽڵ +#spring.shardingsphere.sharding.tables.t_user.actual\data\nodes=m$->{1..2}.t_user_$->{1..2} +# +## ָt_userɲΪSNOWFLAKE +#spring.shardingsphere.sharding.tables.t_user.key\generator.column=id +#spring.shardingsphere.sharding.tables.t_user.key\generator.type=SNOWFLAKE +# +## ָt_userķֿԣֿ԰ƬͷƬ㷨 +#spring.shardingsphere.sharding.tables.t_user.database-strategy.inline.sharding\column=sex +#spring.shardingsphere.sharding.tables.t_user.database-strategy.inline.algorithm\expression=m$->{sex % 2 + 1} +# +# +## ָt_userķֱԣֱ԰ƬͷƬ㷨 +#spring.shardingsphere.sharding.tables.t_user.table\strategy.inline.sharding\column=id +#spring.shardingsphere.sharding.tables.t_user.table\strategy.inline.algorithm\expression=t_user_$->{id % 2 + 1} +# +# +## ̨־ +#logging.level.root=info +# +## sql־ +#spring.shardingsphere.props.sql.show=true +# +##mapperļɨ· +#mybatis.mapper-locations=classpath:/mapper/*.xml +# +# +# diff --git a/threadutiltongyong/src/main/resources/application.yml b/threadutiltongyong/src/main/resources/application.yml new file mode 100644 index 0000000..bbf568c --- /dev/null +++ b/threadutiltongyong/src/main/resources/application.yml @@ -0,0 +1,14 @@ +#spring: +# datasource: +# type: com.alibaba.druid.pool.DruidDataSource +# driverClassName: com.mysql.cj.jdbc.Driver +# # 主库数据源 +# master: +# url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 +# username: root +# password: 123456 +# slave: +# url: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 +# username: root +# password: 123456 + diff --git a/threadutiltongyong/src/main/resources/ip2region/ip2region.xdb b/threadutiltongyong/src/main/resources/ip2region/ip2region.xdb new file mode 100644 index 0000000..9f6502b Binary files /dev/null and b/threadutiltongyong/src/main/resources/ip2region/ip2region.xdb differ diff --git a/threadutiltongyong/src/main/resources/logback-spring.xml b/threadutiltongyong/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..9b029bd --- /dev/null +++ b/threadutiltongyong/src/main/resources/logback-spring.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + ${CONSOLE_LOG_PATTERN} + + UTF-8 + + + + + + + + + + + diff --git a/threadutiltongyong/src/main/resources/mapper/master/UserDaoMapper.xml b/threadutiltongyong/src/main/resources/mapper/master/UserDaoMapper.xml new file mode 100644 index 0000000..846e3d0 --- /dev/null +++ b/threadutiltongyong/src/main/resources/mapper/master/UserDaoMapper.xml @@ -0,0 +1,24 @@ + + + + + + + + insert into t_user_1(id,name, sex) values (#{id},#{name}, #{sex}) + + + + + + + diff --git a/threadutiltongyong/src/main/resources/mapper/slave/UserDaoMapper.xml b/threadutiltongyong/src/main/resources/mapper/slave/UserDaoMapper.xml new file mode 100644 index 0000000..6500acb --- /dev/null +++ b/threadutiltongyong/src/main/resources/mapper/slave/UserDaoMapper.xml @@ -0,0 +1,9 @@ + + + + + + insert into t_user_1(id,name, sex) values (#{id},#{name}, #{sex}) + + + diff --git a/threadutiltongyong/src/main/resources/mybatis-config.xml b/threadutiltongyong/src/main/resources/mybatis-config.xml new file mode 100644 index 0000000..b099e5d --- /dev/null +++ b/threadutiltongyong/src/main/resources/mybatis-config.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/threadutiltongyong/woniu-web.iml b/threadutiltongyong/woniu-web.iml new file mode 100644 index 0000000..0a5cabd --- /dev/null +++ b/threadutiltongyong/woniu-web.iml @@ -0,0 +1,154 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file