├── doc
├── issues
│ ├── v0.0.2.md
│ ├── v0.0.4.md
│ ├── v0.0.1.md
│ ├── v0.0.3.md
│ └── index.md
├── blog
│ ├── async-01-项目模块介绍.md
│ ├── async-02-CGLIB代理.md
│ └── async-03-spring-整合.md
├── 版本迭代规范.md
├── CI集成.md
├── 发布流程.md
└── 项目原型.md
├── .coveralls.yml
├── async-api
├── src
│ └── main
│ │ ├── resources
│ │ └── README.md
│ │ └── java
│ │ └── com
│ │ └── github
│ │ └── houbb
│ │ └── async
│ │ └── api
│ │ ├── package-info.java
│ │ ├── annotation
│ │ ├── package-info.java
│ │ └── Async.java
│ │ ├── core
│ │ ├── package-info.java
│ │ ├── executor
│ │ │ ├── package-info.java
│ │ │ └── IAsyncExecutor.java
│ │ ├── async
│ │ │ ├── package-info.java
│ │ │ ├── IAsyncResult.java
│ │ │ └── impl
│ │ │ │ └── AbstractAsyncResult.java
│ │ └── proxy
│ │ │ └── IAsyncProxy.java
│ │ └── constant
│ │ ├── package-info.java
│ │ └── AsyncConstant.java
└── pom.xml
├── async-core
├── src
│ └── main
│ │ ├── resources
│ │ └── README.md
│ │ └── java
│ │ └── com
│ │ └── github
│ │ └── houbb
│ │ └── async
│ │ └── core
│ │ ├── package-info.java
│ │ ├── executor
│ │ ├── package-info.java
│ │ └── AsyncExecutor.java
│ │ ├── constant
│ │ ├── package-info.java
│ │ └── enums
│ │ │ └── ProxyTypeEnum.java
│ │ ├── proxy
│ │ ├── dynamic
│ │ │ ├── package-info.java
│ │ │ └── DynamicProxy.java
│ │ ├── package-info.java
│ │ ├── none
│ │ │ └── NoneProxy.java
│ │ ├── AsyncProxy.java
│ │ └── cglib
│ │ │ └── CglibProxy.java
│ │ ├── model
│ │ ├── package-info.java
│ │ └── async
│ │ │ └── AsyncResult.java
│ │ └── exception
│ │ └── AsyncRuntimeException.java
└── pom.xml
├── async-test
├── src
│ ├── main
│ │ ├── resources
│ │ │ └── README.md
│ │ └── java
│ │ │ └── com
│ │ │ └── github
│ │ │ └── houbb
│ │ │ └── async
│ │ │ └── test
│ │ │ ├── package-info.java
│ │ │ ├── service
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ │ ├── UserServiceImpl.java
│ │ │ │ └── UserServiceDefault.java
│ │ │ └── config
│ │ │ └── SpringConfig.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── github
│ │ └── houbb
│ │ └── async
│ │ └── test
│ │ ├── package-info.java
│ │ ├── service
│ │ ├── package-info.java
│ │ └── UserServiceTest.java
│ │ └── spring
│ │ └── SpringServiceTest.java
└── pom.xml
├── async-springboot-starter
├── src
│ └── main
│ │ ├── java
│ │ └── com
│ │ │ └── github
│ │ │ └── houbb
│ │ │ └── async
│ │ │ └── springboot
│ │ │ └── starter
│ │ │ ├── package-info.java
│ │ │ └── config
│ │ │ └── AsyncAutoConfig.java
│ │ └── resources
│ │ └── META-INF
│ │ └── spring.factories
└── pom.xml
├── .travis.yml
├── cgit.sh
├── cgit.bat
├── .gitignore
├── async-test2
├── src
│ ├── main
│ │ └── java
│ │ │ └── com
│ │ │ └── github
│ │ │ └── houbb
│ │ │ └── async
│ │ │ └── test2
│ │ │ ├── service
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ │ └── UserServiceImpl.java
│ │ │ └── AsyncApplication.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── github
│ │ └── houbb
│ │ └── async
│ │ └── test2
│ │ └── UserServiceTest.java
└── pom.xml
├── async-spring
├── src
│ └── main
│ │ └── java
│ │ └── com
│ │ └── github
│ │ └── houbb
│ │ └── async
│ │ └── spring
│ │ ├── config
│ │ └── AsyncAopConfig.java
│ │ ├── annotation
│ │ └── EnableAsync.java
│ │ └── aop
│ │ ├── AutoLogMethodAdvisor.java
│ │ └── AysncAop.java
└── pom.xml
├── CHANGELOG.md
├── release.bat
├── release_rm.sh
├── release.sh
├── README.md
└── pom.xml
/doc/issues/v0.0.2.md:
--------------------------------------------------------------------------------
1 | 支持 CGLIB
--------------------------------------------------------------------------------
/doc/issues/v0.0.4.md:
--------------------------------------------------------------------------------
1 | - 添加无侵入实现。
--------------------------------------------------------------------------------
/.coveralls.yml:
--------------------------------------------------------------------------------
1 | service_name: travis-ci
--------------------------------------------------------------------------------
/async-api/src/main/resources/README.md:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/async-core/src/main/resources/README.md:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/async-test/src/main/resources/README.md:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/doc/issues/v0.0.1.md:
--------------------------------------------------------------------------------
1 | # 添加 async 基本实现。
2 |
3 | 实现编程式的实现。
4 |
5 | - 支持 fluent
6 |
7 | - 支持接口
--------------------------------------------------------------------------------
/async-api/src/main/java/com/github/houbb/async/api/package-info.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.api;
--------------------------------------------------------------------------------
/async-core/src/main/java/com/github/houbb/async/core/package-info.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.core;
--------------------------------------------------------------------------------
/async-test/src/main/java/com/github/houbb/async/test/package-info.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.test;
--------------------------------------------------------------------------------
/async-test/src/test/java/com/github/houbb/async/test/package-info.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.test;
--------------------------------------------------------------------------------
/doc/issues/v0.0.3.md:
--------------------------------------------------------------------------------
1 | - 日志信息的添加
2 |
3 | log-integration 的发布+整合 heaven。
4 |
5 | - 对于资源的关闭
6 |
7 | JVM 钩子函数
--------------------------------------------------------------------------------
/async-test/src/test/java/com/github/houbb/async/test/service/package-info.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.test.service;
--------------------------------------------------------------------------------
/async-api/src/main/java/com/github/houbb/async/api/annotation/package-info.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.api.annotation;
--------------------------------------------------------------------------------
/async-springboot-starter/src/main/java/com/github/houbb/async/springboot/starter/package-info.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.springboot.starter;
--------------------------------------------------------------------------------
/doc/blog/async-01-项目模块介绍.md:
--------------------------------------------------------------------------------
1 | # async-api
2 |
3 | 定义 api 相关,包括注解,接口,默认实现等。
4 |
5 | # async-core
6 |
7 | 核心实现模块,默认依赖 async-api
8 |
9 | # async-test
10 |
11 | 测试模块。用户无需引入。
--------------------------------------------------------------------------------
/async-core/src/main/java/com/github/houbb/async/core/executor/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * 执行器类
3 | * @since 0.0.2
4 | * @author binbin.hou
5 | */
6 | package com.github.houbb.async.core.executor;
--------------------------------------------------------------------------------
/async-springboot-starter/src/main/resources/META-INF/spring.factories:
--------------------------------------------------------------------------------
1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.github.houbb.async.springboot.starter.config.AsyncAutoConfig
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | jdk:
3 | - oraclejdk8
4 | install: mvn install -DskipTests=true -Dmaven.javadoc.skip=true
5 | script: mvn test
6 | after_success:
7 | - mvn clean cobertura:cobertura coveralls:report
--------------------------------------------------------------------------------
/cgit.sh:
--------------------------------------------------------------------------------
1 | # 提交
2 |
3 | git pull
4 | git add .
5 | git commit -m "[Feature] add for new"
6 | git push
7 | git status
8 |
9 | # 1. 赋值权限: chmod +x ./cgit.sh
10 | # 2. 执行: ./cgit.sh
11 | # Last Update Time: 2018-11-21 21:55:38
12 | # Author: houbb
--------------------------------------------------------------------------------
/cgit.bat:
--------------------------------------------------------------------------------
1 | :: 用于提交当前变更(windows)
2 | :: author: houbb
3 | :: LastUpdateTime: 2018-11-22 09:08:52
4 | :: 用法:双击运行,或者当前路径 cmd 直接输入 .\cgit.bat
5 |
6 | git pull
7 | git add .
8 | git commit -m "[Feature] add for new"
9 | git push
10 | git status
11 |
12 |
--------------------------------------------------------------------------------
/async-core/src/main/java/com/github/houbb/async/core/constant/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | /**
7 | *
常量类
8 | *
9 | * Created: 2019-03-05 22:25
10 | * Project: async
11 | *
12 | * @author houbinbin
13 | */
14 | package com.github.houbb.async.core.constant;
--------------------------------------------------------------------------------
/async-api/src/main/java/com/github/houbb/async/api/core/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | /**
7 | * 核心包
8 | *
9 | * Created: 2019-03-08 10:02
10 | * Project: async
11 | *
12 | * @author houbinbin
13 | * @since 0.0.1
14 | */
15 | package com.github.houbb.async.api.core;
--------------------------------------------------------------------------------
/async-core/src/main/java/com/github/houbb/async/core/proxy/dynamic/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | /**
7 | * 动态代理
8 | *
9 | * Created: 2019-03-05 22:23
10 | * Project: async
11 | *
12 | * @author houbinbin
13 | */
14 | package com.github.houbb.async.core.proxy.dynamic;
--------------------------------------------------------------------------------
/doc/issues/index.md:
--------------------------------------------------------------------------------
1 | 优化点:
2 |
3 | # 对于代码的无侵入性。
4 |
5 | 支持用户不实现 AsyncResult。
6 |
7 | # 支持注解
8 |
9 | Spring 注解 + aop 实现。
10 |
11 | 添加模块,async-spring。
12 |
13 | # 配置
14 |
15 | 自由配置
16 |
17 | - 添加配置支持
18 |
19 | # 生命周期
20 |
21 | 对于 executor 的销毁+创建挂管理。
22 |
23 | 生命周期
24 |
25 | # 执行的回调
26 |
27 | 添加各种回调+钩子
28 |
29 | # 缓存
30 |
31 | 对于代理方法和类的缓存。
32 |
33 | 初期基于 map,后期基于 i-cache。
34 |
35 |
--------------------------------------------------------------------------------
/async-api/src/main/java/com/github/houbb/async/api/constant/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | /**
7 | * 常量定义
8 | *
9 | * Created: 2019-03-08 10:17
10 | * Project: async
11 | *
12 | * @author houbinbin
13 | * @since 0.0.1
14 | */
15 | package com.github.houbb.async.api.constant;
--------------------------------------------------------------------------------
/async-core/src/main/java/com/github/houbb/async/core/model/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | /**
7 | * 存放对象信息
8 | *
9 | * Created: 2019-03-08 10:36
10 | * Project: async
11 | *
12 | * @author houbinbin
13 | * @since 0.0.1
14 | */
15 | package com.github.houbb.async.core.model;
--------------------------------------------------------------------------------
/async-core/src/main/java/com/github/houbb/async/core/proxy/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | /**
7 | * 代理相关的实现
8 | *
9 | * Created: 2019-03-05 22:22
10 | * Project: async
11 | *
12 | * @author houbinbin
13 | * @since 0.0.1
14 | */
15 | package com.github.houbb.async.core.proxy;
--------------------------------------------------------------------------------
/async-api/src/main/java/com/github/houbb/async/api/core/executor/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | /**
7 | * 线程池执行框架相关
8 | *
9 | * Created: 2019-03-08 10:19
10 | * Project: async
11 | *
12 | * @author houbinbin
13 | * @since 0.0.1
14 | */
15 | package com.github.houbb.async.api.core.executor;
--------------------------------------------------------------------------------
/async-api/src/main/java/com/github/houbb/async/api/core/async/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | /**
7 | * 异步相关接口
8 | *
9 | * 1. 异步回调
10 | * 2. 异步结果
11 | * Created: 2019-03-08 10:19
12 | * Project: async
13 | *
14 | * @author houbinbin
15 | */
16 | package com.github.houbb.async.api.core.async;
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # maven ignore
2 | target/
3 | *.jar
4 | *.war
5 | *.zip
6 | *.tar
7 | *.tar.gz
8 |
9 | # eclipse ignore
10 | .settings/
11 | .project
12 | .classpath
13 |
14 | # idea ignore
15 | .idea/
16 | *.ipr
17 | *.iml
18 | *.iws
19 |
20 | # temp ignore
21 | *.log
22 | *.cache
23 | *.diff
24 | *.patch
25 | *.tmp
26 | *.java~
27 | *.properties~
28 | *.xml~
29 |
30 | # system ignore
31 | .DS_Store
32 | Thumbs.db
33 |
34 |
--------------------------------------------------------------------------------
/doc/版本迭代规范.md:
--------------------------------------------------------------------------------
1 | # 版本类型
2 |
3 | x.y.z
4 |
5 | x 表示大版本之间的迭代,可能出现前后的不兼容。
6 |
7 | y 表示新特性,每次新加一个新特性,都会提升 y 的版本号。
8 |
9 | z 表示修复版本号,如果为第一版,则 z=1。后续会依次提升。
10 |
11 | ## 使用的版本选择
12 |
13 | 如果在较为正式的环境使用,建议使用 x.y 相同,z 数值最大的版本。
14 |
15 | 因为这个版本为尽可能修复已知的 bug。
16 |
17 | ## 新特性
18 |
19 | 不同版本的新特性,参见变更日志。
20 |
21 | # 版本的兼容性
22 |
23 | 在同一个 x 大版本中,禁止直接删除类信息。
24 |
25 | 所有的废弃使用 `@Depretectd` 注解、
26 |
27 | ## 必须有对应的测试
28 |
29 | 对应的测试信息。
30 |
31 | 保证代码的正确性。
--------------------------------------------------------------------------------
/async-test2/src/main/java/com/github/houbb/async/test2/service/UserService.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.test2.service;
2 |
3 | import com.github.houbb.async.core.model.async.AsyncResult;
4 |
5 | /**
6 | * 用户服务接口
7 | * @author binbin.hou
8 | * @since 0.0.4
9 | */
10 | public interface UserService {
11 |
12 | /**
13 | * 查询用户信息
14 | * @param id 主键
15 | * @return 结果
16 | */
17 | AsyncResult queryUser(final String id);
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/async-test/src/main/java/com/github/houbb/async/test/service/UserService.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.test.service;
2 |
3 | import com.github.houbb.async.core.model.async.AsyncResult;
4 |
5 | /**
6 | * 用户服务接口
7 | * @author binbin.hou
8 | * date 2019/3/7
9 | * @since 0.0.1
10 | */
11 | public interface UserService {
12 |
13 | /**
14 | * 查询用户信息
15 | * @param id 主键
16 | * @return 结果
17 | */
18 | AsyncResult queryUser(final String id);
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/doc/CI集成.md:
--------------------------------------------------------------------------------
1 | # 文档说明
2 |
3 | 作者:侯宾宾
4 |
5 | 时间:2018-04-24 10:11:43
6 |
7 | 说明:如何进行项目的持续集成+测试覆盖率
8 |
9 | # Travis-CI
10 |
11 | [https://www.travis-ci.org](https://www.travis-ci.org) 直接添加此项目
12 |
13 | # Coveralls
14 |
15 | - 添加项目
16 |
17 | [https://coveralls.io/repos/new](https://coveralls.io/repos/new) 直接添加项目
18 |
19 | - 生成密匙
20 |
21 | ```
22 | travis encrypt COVERALLS_TOKEN=${your_repo_token}
23 | ```
24 |
25 | - 添加到文件
26 |
27 | ```
28 | travis encrypt COVERALLS_TOKEN=${your_repo_token} --add
29 | ```
30 |
31 |
--------------------------------------------------------------------------------
/async-test2/src/main/java/com/github/houbb/async/test2/AsyncApplication.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.test2;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * @author binbin.hou
8 | * @since 0.0.4
9 | */
10 | @SpringBootApplication
11 | public class AsyncApplication {
12 |
13 | public static void main(String[] args) {
14 | SpringApplication.run(AsyncApplication.class, args);
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/async-test/src/main/java/com/github/houbb/async/test/config/SpringConfig.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.test.config;
2 |
3 |
4 | import com.github.houbb.async.spring.annotation.EnableAsync;
5 | import org.springframework.beans.factory.annotation.Configurable;
6 | import org.springframework.context.annotation.ComponentScan;
7 |
8 | /**
9 | * @author binbin.hou
10 | * @since 0.0.3
11 | */
12 | @Configurable
13 | @ComponentScan(basePackages = "com.github.houbb.async.test.service")
14 | @EnableAsync
15 | public class SpringConfig {
16 | }
17 |
--------------------------------------------------------------------------------
/async-spring/src/main/java/com/github/houbb/async/spring/config/AsyncAopConfig.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.spring.config;
2 |
3 | import org.springframework.context.annotation.ComponentScan;
4 | import org.springframework.context.annotation.Configuration;
5 |
6 | /**
7 | * 自动日志 aop 配置
8 | *
9 | * https://blog.csdn.net/u013905744/article/details/91364736
10 | * @author binbin.hou
11 | * @since 0.0.3
12 | */
13 | @Configuration
14 | @ComponentScan(basePackages = "com.github.houbb.async.spring")
15 | public class AsyncAopConfig {
16 | }
17 |
--------------------------------------------------------------------------------
/async-springboot-starter/src/main/java/com/github/houbb/async/springboot/starter/config/AsyncAutoConfig.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.springboot.starter.config;
2 |
3 | import com.github.houbb.async.spring.annotation.EnableAsync;
4 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
5 | import org.springframework.context.annotation.Configuration;
6 |
7 | /**
8 | * 异步配置
9 | * @author binbin.hou
10 | * @since 0.0.4
11 | */
12 | @Configuration
13 | @ConditionalOnClass(EnableAsync.class)
14 | @EnableAsync
15 | public class AsyncAutoConfig {
16 | }
17 |
--------------------------------------------------------------------------------
/async-api/src/main/java/com/github/houbb/async/api/constant/AsyncConstant.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | package com.github.houbb.async.api.constant;
7 |
8 | /**
9 | * 全局的常量定义
10 | *
11 | * Created: 2019/3/8 10:17 AM
12 | * Project: async
13 | *
14 | * @author houbinbin
15 | * @since 0.0.1
16 | */
17 | public final class AsyncConstant {
18 |
19 | private AsyncConstant(){}
20 |
21 | /**
22 | * 默认的超时时间为 30
23 | */
24 | public static final long DEFAULT_TIME_OUT = 30;
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/async-api/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | async
7 | com.github.houbb
8 | 0.0.5-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | async-api
13 | The api module of async project.
14 |
15 |
--------------------------------------------------------------------------------
/async-api/src/main/java/com/github/houbb/async/api/core/proxy/IAsyncProxy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | package com.github.houbb.async.api.core.proxy;
7 |
8 | /**
9 | * 代理接口
10 | *
11 | * Created: 2019/3/8 10:03 AM
12 | * Project: async
13 | *
14 | * @author houbinbin
15 | * @since 0.0.1
16 | */
17 | public interface IAsyncProxy {
18 |
19 | /**
20 | * 获取代理对象
21 | * 1. 如果是实现了接口,默认使用 dynamic proxy 即可。
22 | * 2. 如果没有实现接口,默认使用 CGLIB 实现代理。
23 | * @return 代理对象
24 | */
25 | Object proxy();
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/async-spring/src/main/java/com/github/houbb/async/spring/annotation/EnableAsync.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.spring.annotation;
2 |
3 | import com.github.houbb.async.spring.config.AsyncAopConfig;
4 | import org.springframework.context.annotation.EnableAspectJAutoProxy;
5 | import org.springframework.context.annotation.Import;
6 |
7 | import java.lang.annotation.*;
8 |
9 | /**
10 | * 启用自动日志注解
11 | * @author binbin.hou
12 | * @since 0.0.3
13 | */
14 | @Target(ElementType.TYPE)
15 | @Retention(RetentionPolicy.RUNTIME)
16 | @Documented
17 | @Import(AsyncAopConfig.class)
18 | @EnableAspectJAutoProxy
19 | public @interface EnableAsync {
20 | }
21 |
--------------------------------------------------------------------------------
/async-api/src/main/java/com/github/houbb/async/api/core/async/IAsyncResult.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | package com.github.houbb.async.api.core.async;
7 |
8 | import java.util.concurrent.Future;
9 |
10 | /**
11 | * 异步执行结果
12 | *
13 | * Created: 2019/3/8 10:03 AM
14 | * Project: async
15 | *
16 | * @author houbinbin
17 | * @since 0.0.1
18 | * @param 泛型类型
19 | */
20 | public interface IAsyncResult extends Future {
21 |
22 | /**
23 | * 获取执行的结果
24 | * @return 结果
25 | */
26 | Object getResult();
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/async-api/src/main/java/com/github/houbb/async/api/core/executor/IAsyncExecutor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | package com.github.houbb.async.api.core.executor;
7 |
8 | import com.github.houbb.async.api.core.async.IAsyncResult;
9 |
10 | import java.lang.reflect.Method;
11 | import java.util.concurrent.ExecutorService;
12 |
13 | /**
14 | * 异步框架执行器
15 | *
16 | * Created: 2019/3/8 10:30 AM
17 | * Project: async
18 | *
19 | * @author houbinbin
20 | * @since 0.0.1
21 | */
22 | public interface IAsyncExecutor extends ExecutorService {
23 | }
24 |
--------------------------------------------------------------------------------
/async-core/src/main/java/com/github/houbb/async/core/constant/enums/ProxyTypeEnum.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | package com.github.houbb.async.core.constant.enums;
7 |
8 | /**
9 | * 代理类型枚举
10 | *
11 | * Created: 2019/3/5 10:25 PM
12 | * Project: async
13 | *
14 | * @author houbinbin
15 | * @since 0.0.1
16 | */
17 | public enum ProxyTypeEnum {
18 |
19 | /**
20 | * 不执行任何代理
21 | */
22 | NONE,
23 |
24 | /**
25 | * jdk 动态代理
26 | */
27 | DYNAMIC,
28 |
29 | /**
30 | * cglib 动态代理
31 | */
32 | CGLIB;
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # 变更日志
2 |
3 | | 类型 | 说明 |
4 | |:----|:----|
5 | | A | 新增 |
6 | | U | 更新 |
7 | | D | 删除 |
8 | | T | 测试 |
9 | | O | 优化 |
10 | | F | 修复BUG |
11 |
12 | # release_0.0.1
13 |
14 | | 序号 | 变更类型 | 说明 | 时间 | 备注 |
15 | |:---|:---|:---|:---|:--|
16 | | 1 | A | 接口动态代理的实现 | 2019-03-08 10:11:19 | |
17 |
18 | # release_0.0.2
19 |
20 | | 序号 | 变更类型 | 说明 | 时间 | 备注 |
21 | |:---|:---|:---|:---|:--|
22 | | 1 | A | CGLIB 生成代理的实现 | 2019-3-8 17:04:53 | |
23 |
24 | # release_0.0.3
25 |
26 | | 序号 | 变更类型 | 说明 | 时间 | 备注 |
27 | |:---|:---|:---|:---|:--|
28 | | 1 | A | 添加 spring 整合实现 | 2020-7-30 23:06:40 | |
29 |
30 | # release_0.0.4
31 |
32 | | 序号 | 变更类型 | 说明 | 时间 | 备注 |
33 | |:---|:---|:---|:---|:--|
34 | | 1 | A | 添加 spring-boot 整合实现 | 2020-08-06 23:06:40 | |
--------------------------------------------------------------------------------
/doc/发布流程.md:
--------------------------------------------------------------------------------
1 | # 文档说明
2 |
3 | 本文档用于说明当前项目如何进行发布。
4 |
5 |
6 | # 发布流程
7 |
8 | ## push to mvn center
9 |
10 | ```
11 | mvn clean deploy -P release
12 | ```
13 |
14 | ## commit to github
15 |
16 | ```
17 | git push
18 | ```
19 |
20 | ## merge to master
21 |
22 | ```
23 | git checkout master
24 | git pull
25 | git checkout branch
26 | git rebase master (用rebase合并主干的修改,如果有冲突在此时解决)
27 | git checkout master
28 | git merge branch
29 | git push
30 | ```
31 |
32 | ## create new branch & checkout
33 |
34 | ```
35 | git branch release_XXX
36 | git checkout release_XXX
37 | ```
38 |
39 | ## modify project version
40 |
41 | ```
42 | mvn versions:set -DgroupId=com.github.houbb -DartifactId=paradise* -DoldVersion=1.1.2 -DnewVersion=1.1.3-SNAPSHOT-->
43 | mvn -N versions:update-child-modules
44 | mvn versions:commit
45 | ```
46 |
47 |
--------------------------------------------------------------------------------
/async-core/src/main/java/com/github/houbb/async/core/exception/AsyncRuntimeException.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.core.exception;
2 |
3 | /**
4 | * 异步运行时异常
5 | * @author binbin.hou
6 | * date 2019/3/8
7 | * @since 0.0.2
8 | */
9 | public class AsyncRuntimeException extends RuntimeException{
10 |
11 | public AsyncRuntimeException() {
12 | }
13 |
14 | public AsyncRuntimeException(String message) {
15 | super(message);
16 | }
17 |
18 | public AsyncRuntimeException(String message, Throwable cause) {
19 | super(message, cause);
20 | }
21 |
22 | public AsyncRuntimeException(Throwable cause) {
23 | super(cause);
24 | }
25 |
26 | public AsyncRuntimeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
27 | super(message, cause, enableSuppression, writableStackTrace);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/async-test2/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | async
7 | com.github.houbb
8 | 0.0.5-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | async-test2
13 |
14 |
15 |
16 | com.github.houbb
17 | async-springboot-starter
18 |
19 |
20 |
21 | com.github.houbb
22 | test-spring
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/async-api/src/main/java/com/github/houbb/async/api/annotation/Async.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.api.annotation;
2 |
3 | import java.lang.annotation.*;
4 |
5 | /**
6 | * 当方法添加这个注解的时候,则实际进行异步执行。
7 | * 1. 如果放在方法上,则当前方法会进行异步执行
8 | * 2. 如果放在类上,则当前类下面的所有方法都进行异步处理。
9 | *
10 | * 只有注解的方法被代理执行,其他线程正常走。
11 | * 方法执行的结果放在异步线程中实现。
12 | * 1. 如果方法没有返回值,则直接继续执行主线程。结果在线程中回调。
13 | * 2. 如果方法有返回值,则同时异步执行多个方法。并且所有方法获取结果之后,继续走主线程。
14 | *
15 | * 异步提交方法,拿到 future
16 | * Method 返回值为空,直接提交 executor 执行,然后返回结果为 null。
17 | * Method 返回值不为空,直接返回 class.newInstance。
18 | *
19 | * 对于结果的处理,依然使用对于方法的拦截处理。invoke 执行方法获得方法执行结果。
20 | *
21 | * (1)直接创建返回对象的 class.NewInstance
22 | * (2) future 加载获取
23 | *
24 | * 实现原理:基于 CGLIB 实现代理,基于 aop 实现方法的切面处理。
25 | * 基于线程池实现真正的任务处理。
26 | * @author binbin.hou
27 | * @since 0.0.1
28 | */
29 | @Inherited
30 | @Documented
31 | @Target({ElementType.METHOD, ElementType.TYPE})
32 | @Retention(RetentionPolicy.RUNTIME)
33 | public @interface Async {
34 | }
35 |
--------------------------------------------------------------------------------
/async-springboot-starter/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | async
7 | com.github.houbb
8 | 0.0.5-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | async-springboot-starter
13 |
14 |
15 |
16 | ${project.groupId}
17 | async-spring
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-starter
23 |
24 |
25 |
--------------------------------------------------------------------------------
/async-spring/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | async
7 | com.github.houbb
8 | 0.0.5-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | async-spring
13 |
14 |
15 |
16 |
17 | ${project.groupId}
18 | async-core
19 |
20 |
21 |
22 |
23 | com.github.houbb
24 | aop-spring
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/release.bat:
--------------------------------------------------------------------------------
1 | :: 用于 release 当前项目(windows)
2 | :: author: houbb
3 | :: LastUpdateTime: 2018-1-22 09:08:52
4 | :: 用法:双击运行,或者当前路径 cmd 直接输入 release.bat
5 |
6 | :: 关闭回显
7 | @echo OFF
8 |
9 | ECHO "============================= RELEASE START..."
10 |
11 | :: 版本号信息(需要手动指定)
12 | :::: 旧版本名称
13 | SET version=0.0.4
14 | :::: 新版本名称
15 | SET newVersion=0.0.5
16 | :::: 组织名称
17 | SET groupName=com.github.houbb
18 | :::: 项目名称
19 | SET projectName=async
20 |
21 | :: release 项目版本
22 | :::: snapshot 版本号
23 | SET snapshot_version=%version%"-SNAPSHOT"
24 | :::: 新的版本号
25 | SET release_version=%version%
26 |
27 | call mvn versions:set -DgroupId=%groupName% -DartifactId=%projectName% -DoldVersion=%snapshot_version% -DnewVersion=%release_version%
28 | call mvn -N versions:update-child-modules
29 | call mvn versions:commit
30 | call echo "1. RELEASE %snapshot_version% TO %release_version% DONE."
31 |
32 |
33 | :: 推送到 github
34 | git add .
35 | git commit -m "release branch %version%"
36 | git push
37 | git status
38 |
39 | ECHO "2. PUSH TO GITHUB DONE."
40 |
41 | :: 推送到 maven 中央仓库
42 | call mvn clean deploy -P release
43 | ECHO "3 PUSH TO MVN CENTER DONE."
44 |
--------------------------------------------------------------------------------
/async-test/src/main/java/com/github/houbb/async/test/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.test.service.impl;
2 |
3 | import com.github.houbb.async.core.model.async.AsyncResult;
4 | import com.github.houbb.async.test.service.UserService;
5 | import org.springframework.stereotype.Service;
6 |
7 | import java.util.concurrent.TimeUnit;
8 |
9 | /**
10 | * 用户实现类
11 | * @author binbin.hou
12 | * date 2019/3/7
13 | * @since 0.0.1
14 | */
15 | @Service
16 | public class UserServiceImpl implements UserService {
17 |
18 | @Override
19 | public AsyncResult queryUser(String id) {
20 | System.out.println("开始根据用户id 查询用户信息 " + id);
21 | try {
22 | // 沉睡模拟处理耗时
23 | TimeUnit.SECONDS.sleep(3);
24 | } catch (InterruptedException e) {
25 | e.printStackTrace();
26 | }
27 | final String result = id + "-result";
28 | System.out.println("结束根据用户id 查询用户信息 " + result);
29 |
30 | AsyncResult asyncResult = new AsyncResult<>();
31 | asyncResult.setValue(result);
32 | return asyncResult;
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/async-core/src/main/java/com/github/houbb/async/core/proxy/none/NoneProxy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | package com.github.houbb.async.core.proxy.none;
7 |
8 | import com.github.houbb.async.api.core.proxy.IAsyncProxy;
9 |
10 | import java.lang.reflect.InvocationHandler;
11 | import java.lang.reflect.Method;
12 |
13 | /**
14 | * 没有代理
15 | *
16 | * Created: 2019/3/5 10:23 PM
17 | * Project: async
18 | *
19 | * @author houbinbin
20 | * @since 0.0.1
21 | */
22 | public class NoneProxy implements InvocationHandler, IAsyncProxy {
23 |
24 | /**
25 | * 代理对象
26 | */
27 | private final Object target;
28 |
29 | public NoneProxy(Object target) {
30 | this.target = target;
31 | }
32 |
33 | @Override
34 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
35 | return method.invoke(proxy, args);
36 | }
37 |
38 | /**
39 | * 返回原始对象,没有代理
40 | * @return 原始对象
41 | */
42 | @Override
43 | public Object proxy() {
44 | return this.target;
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/async-test2/src/main/java/com/github/houbb/async/test2/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.test2.service.impl;
2 |
3 | import com.github.houbb.async.api.annotation.Async;
4 | import com.github.houbb.async.core.model.async.AsyncResult;
5 | import com.github.houbb.async.test2.service.UserService;
6 | import org.springframework.stereotype.Service;
7 |
8 | import java.util.concurrent.TimeUnit;
9 |
10 | /**
11 | * 用户实现类
12 | * @author binbin.hou
13 | * @since 0.0.4
14 | */
15 | @Service
16 | public class UserServiceImpl implements UserService {
17 |
18 | @Override
19 | @Async
20 | public AsyncResult queryUser(String id) {
21 | System.out.println("开始根据用户id 查询用户信息 " + id);
22 | try {
23 | // 沉睡模拟处理耗时
24 | TimeUnit.SECONDS.sleep(3);
25 | } catch (InterruptedException e) {
26 | e.printStackTrace();
27 | }
28 | final String result = id + "-result";
29 | System.out.println("结束根据用户id 查询用户信息 " + result);
30 |
31 | AsyncResult asyncResult = new AsyncResult<>();
32 | asyncResult.setValue(result);
33 | return asyncResult;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/async-test/src/main/java/com/github/houbb/async/test/service/impl/UserServiceDefault.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | package com.github.houbb.async.test.service.impl;
7 |
8 | import com.github.houbb.async.api.annotation.Async;
9 | import com.github.houbb.async.core.model.async.AsyncResult;
10 | import com.github.houbb.async.test.service.UserService;
11 | import org.springframework.stereotype.Service;
12 |
13 | import java.util.concurrent.TimeUnit;
14 |
15 | /**
16 | * 用户实现类-无接口
17 | * @author binbin.hou
18 | * date 2019/3/7
19 | * @since 0.0.1
20 | */
21 | @Service
22 | public class UserServiceDefault {
23 |
24 | @Async
25 | public AsyncResult queryUser(String id) {
26 | System.out.println("开始根据用户id 查询用户信息 " + id);
27 | try {
28 | // 沉睡模拟处理耗时
29 | TimeUnit.SECONDS.sleep(3);
30 | } catch (InterruptedException e) {
31 | e.printStackTrace();
32 | }
33 | final String result = id + "-result";
34 | System.out.println("结束根据用户id 查询用户信息 " + result);
35 |
36 | AsyncResult asyncResult = new AsyncResult<>();
37 | asyncResult.setValue(result);
38 | return asyncResult;
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/async-test2/src/test/java/com/github/houbb/async/test2/UserServiceTest.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.test2;
2 |
3 | import com.github.houbb.async.core.model.async.AsyncResult;
4 | import com.github.houbb.async.test2.service.UserService;
5 | import org.junit.Test;
6 | import org.junit.runner.RunWith;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.test.context.ContextConfiguration;
9 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10 |
11 | /**
12 | * @author binbin.hou
13 | * @since 1.0.0
14 | */
15 | @RunWith(SpringJUnit4ClassRunner.class)
16 | @ContextConfiguration(classes = AsyncApplication.class)
17 | public class UserServiceTest {
18 |
19 | @Autowired
20 | private UserService userService;
21 |
22 | @Test
23 | public void userAysncTest() {
24 | long start = System.currentTimeMillis();
25 | AsyncResult result = userService.queryUser("123");
26 | AsyncResult result2 = userService.queryUser("1234");
27 |
28 | System.out.println("查询结果" + result.getResult());
29 | System.out.println("查询结果" + result2.getResult());
30 | long end = System.currentTimeMillis();
31 | System.out.println("共计耗时: " + (end-start));
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/async-core/src/main/java/com/github/houbb/async/core/proxy/AsyncProxy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | package com.github.houbb.async.core.proxy;
7 |
8 | import com.github.houbb.async.core.proxy.cglib.CglibProxy;
9 | import com.github.houbb.async.core.proxy.dynamic.DynamicProxy;
10 | import com.github.houbb.async.core.proxy.none.NoneProxy;
11 | import com.github.houbb.heaven.util.lang.ObjectUtil;
12 |
13 | import java.lang.reflect.Proxy;
14 |
15 | /**
16 | * 代理信息
17 | *
18 | * Created: 2019/3/8 10:38 AM
19 | * Project: async
20 | *
21 | * @author houbinbin
22 | * @since 0.0.1
23 | */
24 | public final class AsyncProxy {
25 |
26 | /**
27 | * 获取对象代理
28 | * @param object 对象代理
29 | * @return 代理信息
30 | */
31 | public static Object getProxy(final Object object) {
32 | if(ObjectUtil.isNull(object)) {
33 | return new NoneProxy(object).proxy();
34 | }
35 |
36 | final Class clazz = object.getClass();
37 |
38 | // 如果targetClass本身是个接口或者targetClass是JDK Proxy生成的,则使用JDK动态代理。
39 | // 参考 spring 的 AOP 判断
40 | if (clazz.isInterface() || Proxy.isProxyClass(clazz)) {
41 | return new DynamicProxy(object).proxy();
42 | }
43 |
44 | return new CglibProxy(object).proxy();
45 | }
46 |
47 |
48 |
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/async-test/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | async
7 | com.github.houbb
8 | 0.0.5-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | async-test
13 |
14 |
15 |
16 |
17 | ${project.groupId}
18 | async-api
19 |
20 |
21 | ${project.groupId}
22 | async-core
23 |
24 |
25 | ${project.groupId}
26 | async-spring
27 |
28 |
29 |
30 |
31 | com.github.houbb
32 | test-spring
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/async-api/src/main/java/com/github/houbb/async/api/core/async/impl/AbstractAsyncResult.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | package com.github.houbb.async.api.core.async.impl;
7 |
8 | import com.github.houbb.async.api.constant.AsyncConstant;
9 | import com.github.houbb.async.api.core.async.IAsyncResult;
10 |
11 | import java.util.concurrent.ExecutionException;
12 | import java.util.concurrent.TimeUnit;
13 | import java.util.concurrent.TimeoutException;
14 |
15 | /**
16 | * 抽象实现类
17 | *
18 | * Created: 2019/3/8 10:14 AM
19 | * Project: async
20 | *
21 | * @author houbinbin
22 | * @since 0.0.1
23 | */
24 | public abstract class AbstractAsyncResult implements IAsyncResult {
25 |
26 | @Override
27 | public boolean cancel(boolean mayInterruptIfRunning) {
28 | return false;
29 | }
30 |
31 | @Override
32 | public boolean isCancelled() {
33 | return false;
34 | }
35 |
36 | @Override
37 | public boolean isDone() {
38 | return false;
39 | }
40 |
41 | @Override
42 | public T get() throws InterruptedException, ExecutionException {
43 | try {
44 | return this.get(AsyncConstant.DEFAULT_TIME_OUT, TimeUnit.SECONDS);
45 | } catch (TimeoutException e) {
46 | throw new RuntimeException(e);
47 | }
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/async-spring/src/main/java/com/github/houbb/async/spring/aop/AutoLogMethodAdvisor.java:
--------------------------------------------------------------------------------
1 | //package com.github.houbb.async.spring.aop;
2 | //
3 | //import com.github.houbb.auto.log.annotation.AutoLog;
4 | //import com.github.houbb.auto.log.core.support.interceptor.AutoLogMethodInterceptor;
5 | //import org.aopalliance.aop.Advice;
6 | //import org.springframework.aop.Pointcut;
7 | //import org.springframework.aop.support.AbstractPointcutAdvisor;
8 | //import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
9 | //import org.springframework.stereotype.Component;
10 | //
11 | ///**
12 | // * @author binbin.hou
13 | // * @since 0.0.3
14 | // */
15 | //@Component
16 | //public class AutoLogMethodAdvisor extends AbstractPointcutAdvisor {
17 | //
18 | // /**
19 | // * 获取切面
20 | // *
21 | // * 获取表达式:
22 | // *
23 | // *
24 | // *
25 | // * AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
26 | // * pointcut.setExpression("切入点表达式"); //基于method级别的注解
27 | // *
28 | // *
29 | // * @return 切面
30 | // * @since 0.0.3
31 | // */
32 | // @Override
33 | // public Pointcut getPointcut() {
34 | // return AnnotationMatchingPointcut.forMethodAnnotation(AutoLog.class);
35 | // }
36 | //
37 | // @Override
38 | // public Advice getAdvice() {
39 | // return new AutoLogMethodInterceptor();
40 | // }
41 | //
42 | //}
43 |
--------------------------------------------------------------------------------
/release_rm.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | echo "============================= RELEASE START..."
3 |
4 | ## 版本号信息(需要手动指定)
5 | oldVersion="0.0.4"
6 | newVersion="0.0.4"
7 | projectName="heaven"
8 |
9 | # 删除分支
10 | oldBranchName="release_"${oldVersion}
11 | git branch -d ${oldBranchName}
12 | git push origin --delete ${oldBranchName}
13 |
14 | echo "1. Branch remove success..."
15 |
16 | # 拉取新的分支
17 | newBranchName="release_"${newVersion}
18 | git branch ${newBranchName}
19 | git checkout ${newBranchName}
20 | git push --set-upstream origin ${newBranchName}
21 |
22 | echo "2. NEW BRANCH DONE."
23 |
24 | # 修改新分支的版本号
25 | ## snapshot 版本号
26 | snapshot_new_version=${newVersion}"-SNAPSHOT"
27 | mvn versions:set -DgroupId=com.github.houbb -DartifactId=${projectName} -DoldVersion=${release_version} -DnewVersion=${snapshot_new_version}
28 | mvn -N versions:update-child-modules
29 | mvn versions:commit
30 |
31 | git add .
32 | git commit -m "modify branch ${release_version} TO ${snapshot_new_version}"
33 | git push
34 | git status
35 | echo "3. MODIFY ${release_version} TO ${snapshot_new_version} DONE."
36 |
37 | echo "============================= BRANCH RE-CREATE END..."
38 |
39 | echo "============================= BRANCH LIST ============================="
40 | git branch -a
41 |
42 | # 使用方式:
43 | # 注意:本脚本用于删除分支,谨慎使用!
44 | # 1. 赋值权限: chmod +x ./release_rm.sh
45 | # 2. 执行: ./release_rm.sh
46 | # Last Update Time: 2018-06-21 11:10:42
47 | # Author: houbb
--------------------------------------------------------------------------------
/async-core/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | async
7 | com.github.houbb
8 | 0.0.5-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | async-core
13 | The core module of async project.
14 |
15 |
16 |
17 |
18 | ${project.groupId}
19 | async-api
20 |
21 |
22 |
23 |
24 | ${project.groupId}
25 | heaven
26 |
27 |
28 | ${project.groupId}
29 | log-integration
30 |
31 |
32 | com.github.houbb
33 | aop-core
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/async-core/src/main/java/com/github/houbb/async/core/proxy/cglib/CglibProxy.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.core.proxy.cglib;
2 |
3 | import com.github.houbb.async.api.core.proxy.IAsyncProxy;
4 | import com.github.houbb.async.core.executor.AsyncExecutor;
5 | import com.github.houbb.async.core.model.async.AsyncResult;
6 | import net.sf.cglib.proxy.Enhancer;
7 | import net.sf.cglib.proxy.MethodInterceptor;
8 | import net.sf.cglib.proxy.MethodProxy;
9 |
10 | import java.lang.reflect.Method;
11 | import java.util.concurrent.ExecutorService;
12 | import java.util.concurrent.Executors;
13 | import java.util.concurrent.Future;
14 |
15 | /**
16 | * CGLIB 代理类
17 | * @author binbin.hou
18 | * date 2019/3/7
19 | * @since 0.0.2
20 | */
21 | public class CglibProxy implements MethodInterceptor, IAsyncProxy {
22 |
23 | /**
24 | * 被代理的对象
25 | */
26 | private final Object target;
27 |
28 | public CglibProxy(Object target) {
29 | this.target = target;
30 | }
31 |
32 | @Override
33 | public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
34 | return AsyncExecutor.submit(target, method, objects);
35 | }
36 |
37 | @Override
38 | public Object proxy() {
39 | Enhancer enhancer = new Enhancer();
40 | //目标对象类
41 | enhancer.setSuperclass(target.getClass());
42 | enhancer.setCallback(this);
43 | //通过字节码技术创建目标对象类的子类实例作为代理
44 | return enhancer.create();
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/doc/blog/async-02-CGLIB代理.md:
--------------------------------------------------------------------------------
1 | # CGLIB 代理
2 |
3 | 和 Spring 的 AOP 实现一样。
4 |
5 | 我们有些方法是没有实现接口的,这时就需要依赖于字节码技术。
6 |
7 | 此处选择 CGLIB 实现。
8 |
9 | ## 支持版本
10 |
11 | since 0.0.2
12 |
13 | # 入门例子
14 |
15 | ## 定义服务类
16 |
17 | ```java
18 | public class UserServiceDefault {
19 |
20 | public AsyncResult queryUser(String id) {
21 | System.out.println("开始根据用户id 查询用户信息 " + id);
22 | try {
23 | // 沉睡模拟处理耗时
24 | TimeUnit.SECONDS.sleep(3);
25 | } catch (InterruptedException e) {
26 | e.printStackTrace();
27 | }
28 | final String result = id + "-result";
29 | System.out.println("结束根据用户id 查询用户信息 " + result);
30 |
31 | AsyncResult asyncResult = new AsyncResult<>();
32 | asyncResult.setValue(result);
33 | return asyncResult;
34 | }
35 |
36 | }
37 | ```
38 |
39 | ## 测试类
40 |
41 | ```java
42 | /**
43 | * CGLIB 代理
44 | */
45 | @Test
46 | public void queryUserCglibProxyTest() {
47 | long start = System.currentTimeMillis();
48 | UserServiceDefault userService = new UserServiceDefault();
49 | UserServiceDefault userServiceProxy = (UserServiceDefault) AsyncProxy.getProxy(userService);
50 | AsyncResult result = userServiceProxy.queryUser("123");
51 | AsyncResult result2 = userServiceProxy.queryUser("1234");
52 |
53 | System.out.println("查询结果" + result.getResult());
54 | System.out.println("查询结果" + result2.getResult());
55 | long end = System.currentTimeMillis();
56 | System.out.println("共计耗时: " + (end-start));
57 | }
58 | ```
59 |
60 | - 日志信息如下
61 |
62 | ```
63 | 开始根据用户id 查询用户信息 123
64 | 开始根据用户id 查询用户信息 1234
65 | 结束根据用户id 查询用户信息 1234-result
66 | 结束根据用户id 查询用户信息 123-result
67 | 查询结果123-result
68 | 查询结果1234-result
69 | 共计耗时: 3181
70 | ```
--------------------------------------------------------------------------------
/async-core/src/main/java/com/github/houbb/async/core/model/async/AsyncResult.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | package com.github.houbb.async.core.model.async;
7 |
8 | import com.github.houbb.async.api.core.async.impl.AbstractAsyncResult;
9 |
10 | import java.util.concurrent.ExecutionException;
11 | import java.util.concurrent.Future;
12 | import java.util.concurrent.TimeUnit;
13 | import java.util.concurrent.TimeoutException;
14 |
15 | /**
16 | * 异步执行结果
17 | * @author binbin.hou
18 | * date 2019/3/7
19 | * @since 0.0.1
20 | */
21 | public class AsyncResult extends AbstractAsyncResult {
22 |
23 | /**
24 | * future 信息
25 | */
26 | private Future future;
27 |
28 | /**
29 | * 结果
30 | */
31 | private Object value;
32 |
33 | /**
34 | * 获取执行的结果
35 | * @return 结果
36 | */
37 | @Override
38 | public Object getResult() {
39 | // 直接返回结果
40 | if(future == null) {
41 | return this.getValue();
42 | }
43 |
44 | try {
45 | T t = future.get();
46 | // 这里拿到的 AsyncResult 对象
47 | if(null != t) {
48 | return ((AsyncResult)t).getValue();
49 | }
50 | return null;
51 | } catch (InterruptedException | ExecutionException e) {
52 | throw new RuntimeException(e);
53 | }
54 | }
55 |
56 | @Override
57 | public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
58 | return future.get(timeout, unit);
59 | }
60 |
61 | public Object getValue() {
62 | return this.value;
63 | }
64 |
65 | public void setValue(Object value) {
66 | this.value = value;
67 | }
68 |
69 | public void setFuture(Future future) {
70 | this.future = future;
71 | }
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/doc/blog/async-03-spring-整合.md:
--------------------------------------------------------------------------------
1 | # spring 整合
2 |
3 | 和 spring 整合可以为我们的使用带来更大的便利性。
4 |
5 | ## maven 引入
6 |
7 | ```xml
8 |
9 | ${project.groupId}
10 | async-spring
11 | 最新版
12 |
13 | ```
14 |
15 | ## 启动配置
16 |
17 | 使用 `@EnableAsync` 注解启动配置。
18 |
19 | ```java
20 | @Configurable
21 | @ComponentScan(basePackages = "com.github.houbb.async.test.service")
22 | @EnableAsync
23 | public class SpringConfig {
24 | }
25 | ```
26 |
27 | ## 方法声明
28 |
29 | 使用 `@Async` 注解声明在方法上。
30 |
31 | ```java
32 | @Service
33 | public class UserServiceDefault {
34 |
35 | @Async
36 | public AsyncResult queryUser(String id) {
37 | System.out.println("开始根据用户id 查询用户信息 " + id);
38 | try {
39 | // 沉睡模拟处理耗时
40 | TimeUnit.SECONDS.sleep(3);
41 | } catch (InterruptedException e) {
42 | e.printStackTrace();
43 | }
44 | final String result = id + "-result";
45 | System.out.println("结束根据用户id 查询用户信息 " + result);
46 |
47 | AsyncResult asyncResult = new AsyncResult<>();
48 | asyncResult.setValue(result);
49 | return asyncResult;
50 | }
51 |
52 | }
53 | ```
54 |
55 | ## 测试效果
56 |
57 | ```java
58 | /**
59 | * 未使用注解
60 | *
61 | * @since 0.0.3
62 | */
63 | @Test
64 | public void userAysncTest() {
65 | long start = System.currentTimeMillis();
66 | AsyncResult result = userServiceDefault.queryUser("123");
67 | AsyncResult result2 = userServiceDefault.queryUser("1234");
68 |
69 | System.out.println("查询结果" + result.getResult());
70 | System.out.println("查询结果" + result2.getResult());
71 | long end = System.currentTimeMillis();
72 | System.out.println("共计耗时: " + (end-start));
73 | }
74 | ```
75 |
76 | - 测试日志
77 |
78 | ```
79 | 开始根据用户id 查询用户信息 123
80 | 开始根据用户id 查询用户信息 1234
81 | 结束根据用户id 查询用户信息 1234-result
82 | 结束根据用户id 查询用户信息 123-result
83 | 查询结果null
84 | 查询结果null
85 | 共计耗时: 3029
86 | ```
--------------------------------------------------------------------------------
/async-core/src/main/java/com/github/houbb/async/core/proxy/dynamic/DynamicProxy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2019. houbinbin Inc.
3 | * async All rights reserved.
4 | */
5 |
6 | package com.github.houbb.async.core.proxy.dynamic;
7 |
8 | import com.github.houbb.async.api.core.proxy.IAsyncProxy;
9 | import com.github.houbb.async.core.executor.AsyncExecutor;
10 | import com.github.houbb.async.core.model.async.AsyncResult;
11 |
12 | import java.lang.reflect.InvocationHandler;
13 | import java.lang.reflect.Method;
14 | import java.lang.reflect.Proxy;
15 | import java.util.concurrent.*;
16 |
17 | /**
18 | * 动态代理
19 | *
20 | * 1. 对于 executor 的抽象,使用 {@link java.util.concurrent.CompletionService}
21 | * 或者 {@link java.util.concurrent.CompletableFuture}
22 | * 2. 确保唯一初始化 executor,在任务执行的最后关闭 executor。
23 | * 3. 异步执行结果的获取,异常信息的获取。
24 | * Created: 2019/3/5 10:23 PM
25 | * Project: async
26 | *
27 | * @author houbinbin
28 | * @since 0.0.1
29 | */
30 | public class DynamicProxy implements InvocationHandler, IAsyncProxy {
31 |
32 | /**
33 | * 被代理的对象
34 | */
35 | private final Object target;
36 |
37 | public DynamicProxy(Object target) {
38 | this.target = target;
39 | }
40 |
41 | /**
42 | * 这种方式虽然实现了异步执行,但是存在一个缺陷:
43 | * 强制用户返回值为 Future 的子类。
44 | *
45 | * 如何实现不影响原来的值,要怎么实现呢?
46 | * @param proxy 原始对象
47 | * @param method 方法
48 | * @param args 入参
49 | * @return 结果
50 | * @throws Throwable 异常
51 | */
52 | @Override
53 | @SuppressWarnings("all")
54 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
55 | return AsyncExecutor.submit(target, method, args);
56 | }
57 |
58 | @Override
59 | public Object proxy() {
60 | // 我们要代理哪个真实对象,就将该对象传进去,最后是通过该真实对象来调用其方法的
61 | InvocationHandler handler = new DynamicProxy(target);
62 |
63 | return Proxy.newProxyInstance(handler.getClass().getClassLoader(),
64 | target.getClass().getInterfaces(), handler);
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/release.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | echo "============================= RELEASE START..."
3 |
4 | ## 版本号信息(需要手动指定)
5 | oldVersion="0.0.2"
6 | newVersion="0.0.3"
7 | projectName="async"
8 |
9 | # release 项目版本
10 | ## snapshot 版本号
11 | snapshot_version=${oldVersion}"-SNAPSHOT"
12 | ## 新的版本号
13 | release_version=${oldVersion}
14 |
15 | mvn versions:set -DgroupId=com.github.houbb -DartifactId=${projectName} -DoldVersion=${snapshot_version} -DnewVersion=${release_version}
16 | mvn -N versions:update-child-modules
17 | mvn versions:commit
18 | echo "1. RELEASE ${snapshot_version} TO ${release_version} DONE."
19 |
20 |
21 | # 推送到 github
22 | git add .
23 | git commit -m "release branch ${oldVersion}"
24 | git push
25 | git status
26 |
27 | echo "2. PUSH TO GITHUB DONE."
28 |
29 |
30 | # 推送到 maven 中央仓库
31 | mvn clean deploy -P release
32 |
33 | echo "3. PUSH TO MAVEN CENTER DONE."
34 |
35 | # 合并到 master 分支
36 | branchName="release_"${oldVersion} # 分支名称
37 | git checkout master
38 | git pull
39 | git checkout ${branchName}
40 | git rebase master
41 | git checkout master
42 | git merge ${branchName}
43 | git push
44 |
45 | echo "4. MERGE TO MASTER DONE."
46 |
47 |
48 | # 拉取新的分支
49 | newBranchName="release_"${newVersion}
50 | git branch ${newBranchName}
51 | git checkout ${newBranchName}
52 | git push --set-upstream origin ${newBranchName}
53 |
54 | echo "5. NEW BRANCH DONE."
55 |
56 | # 修改新分支的版本号
57 | ## snapshot 版本号
58 | snapshot_new_version=${newVersion}"-SNAPSHOT"
59 | mvn versions:set -DgroupId=com.github.houbb -DartifactId=${projectName} -DoldVersion=${release_version} -DnewVersion=${snapshot_new_version}
60 | mvn -N versions:update-child-modules
61 | mvn versions:commit
62 |
63 | git add .
64 | git commit -m "modify branch ${release_version} TO ${snapshot_new_version}"
65 | git push
66 | git status
67 | echo "6. MODIFY ${release_version} TO ${snapshot_new_version} DONE."
68 |
69 | echo "============================= RELEASE END..."
70 |
71 |
72 | # 使用方式:
73 | # 1. 赋值权限: chmod +x ./release.sh
74 | # 2. 执行: ./release.sh
75 | # Last Update Time: 2018-01-20 12:07:34
76 | # Author: houbb
77 |
78 |
79 |
--------------------------------------------------------------------------------
/async-test/src/test/java/com/github/houbb/async/test/spring/SpringServiceTest.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.test.spring;
2 |
3 |
4 | import com.github.houbb.async.core.model.async.AsyncResult;
5 | import com.github.houbb.async.test.config.SpringConfig;
6 | import com.github.houbb.async.test.service.UserService;
7 | import com.github.houbb.async.test.service.impl.UserServiceDefault;
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 | import org.springframework.beans.factory.annotation.Autowired;
11 | import org.springframework.test.context.ContextConfiguration;
12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
13 |
14 | /**
15 | * @author binbin.hou
16 | * @since 0.0.3
17 | */
18 | @ContextConfiguration(classes = SpringConfig.class)
19 | @RunWith(SpringJUnit4ClassRunner.class)
20 | public class SpringServiceTest {
21 |
22 | @Autowired
23 | private UserService userService;
24 |
25 | @Autowired
26 | private UserServiceDefault userServiceDefault;
27 |
28 | /**
29 | * 未使用注解
30 | *
31 | * @since 0.0.3
32 | */
33 | @Test
34 | public void userAysncTest() {
35 | long start = System.currentTimeMillis();
36 | AsyncResult result = userServiceDefault.queryUser("123");
37 | AsyncResult result2 = userServiceDefault.queryUser("1234");
38 |
39 | System.out.println("查询结果" + result.getResult());
40 | System.out.println("查询结果" + result2.getResult());
41 | long end = System.currentTimeMillis();
42 | System.out.println("共计耗时: " + (end-start));
43 | }
44 |
45 | /**
46 | * 未使用注解
47 | *
48 | * @since 0.0.3
49 | */
50 | @Test
51 | public void notUseAsyncTest() {
52 | long start = System.currentTimeMillis();
53 | AsyncResult result = userService.queryUser("123");
54 | AsyncResult result2 = userService.queryUser("1234");
55 |
56 | System.out.println("查询结果" + result.getResult());
57 | System.out.println("查询结果" + result2.getResult());
58 | long end = System.currentTimeMillis();
59 | System.out.println("共计耗时: " + (end-start));
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/doc/项目原型.md:
--------------------------------------------------------------------------------
1 | # 原型创建
2 |
3 | - create
4 |
5 | ```
6 | $ mvn archetype:create-from-project
7 | ```
8 |
9 | - config
10 |
11 | ```
12 | ~/target/generated-sources/archetype/src/main/resources/META-INF/maven/archetype-metadata.xml
13 | ```
14 |
15 | [archetype-descriptor](http://maven.apache.org/archetype/archetype-models/archetype-descriptor/archetype-descriptor.html)
16 |
17 | # 文件内容
18 |
19 | `~/maven-archetype/target/generated-sources/archetype/src/main/resources/META-INF/maven/archetype-metadata.xml`
20 |
21 |
22 | ```xml
23 |
24 |
27 |
28 |
29 |
30 |
31 |
32 | README.md
33 | release.sh
34 | release_rm.sh
35 | .coveralls.yml
36 | .travis.yml
37 | .gitignore
38 | LICENSE.txt
39 |
40 |
41 |
42 |
43 | src/main/java
44 |
45 | **/*.md
46 |
47 |
48 |
49 | src/main/resources
50 |
51 | **/*.md
52 |
53 |
54 |
55 | src/test/java
56 |
57 | **/*.md
58 |
59 |
60 |
61 |
62 | doc
63 |
64 | **/*.md
65 |
66 |
67 |
68 |
69 |
70 | ```
71 |
72 | - install
73 |
74 | ```
75 | $ cd target/generated-sources/archetype/
76 | $ mvn install
77 | ```
78 |
79 |
80 | - use
81 |
82 | ```
83 | $ mkdir /tmp/archetype
84 | $ cd /tmp/archetype
85 | $ mvn archetype:generate -DarchetypeCatalog=local
86 | ```
87 |
88 | ## 注意
89 |
90 | `.gitignore` 文件默认没有添加,需要手动添加。
91 |
92 | `*.iml` 文件是多余的,手动删除
93 |
94 |
95 |
96 |
97 |
98 |
--------------------------------------------------------------------------------
/async-spring/src/main/java/com/github/houbb/async/spring/aop/AysncAop.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.spring.aop;
2 |
3 | import com.github.houbb.async.core.executor.AsyncExecutor;
4 | import com.github.houbb.heaven.response.exception.CommonRuntimeException;
5 | import org.aspectj.lang.ProceedingJoinPoint;
6 | import org.aspectj.lang.Signature;
7 | import org.aspectj.lang.annotation.Around;
8 | import org.aspectj.lang.annotation.Aspect;
9 | import org.aspectj.lang.annotation.Pointcut;
10 | import org.aspectj.lang.reflect.MethodSignature;
11 | import org.springframework.stereotype.Component;
12 |
13 | import java.lang.reflect.Method;
14 |
15 | /**
16 | * 这是一种写法
17 | * @author binbin.hou
18 | * @since 0.0.3
19 | */
20 | @Aspect
21 | @Component
22 | public class AysncAop {
23 |
24 | /**
25 | *
26 | * 切面方法:
27 | *
28 | * (1)扫描所有的共有方法
29 | *
30 | * execution(public * *(..))
31 | *
32 | *
33 | * 问题:切面太大,废弃。
34 | * 使用扫描注解的方式替代。
35 | *
36 | * (2)扫描指定注解的方式
37 | *
38 | * 其实可以在 aop 中直接获取到注解信息,暂时先不调整。
39 | * 暂时先不添加 public 的限定
40 | *
41 | * (3)直接改成注解的优缺点:
42 | * 优点:减少了 aop 的切面访问
43 | * 缺点:弱化了注解的特性,本来是只要是 {@link com.github.houbb.async.api.annotation.Async} 指定的注解即可,
44 | *
45 | * 不过考虑到使用者的熟练度,如果用户知道了自定义注解,自定义 aop 应该也不是问题。
46 | */
47 | @Pointcut("@annotation(com.github.houbb.async.api.annotation.Async)")
48 | public void asyncPointcut() {
49 | }
50 |
51 | /**
52 | * 执行核心方法
53 | *
54 | * 相当于 MethodInterceptor
55 | * @param point 切点
56 | * @return 结果
57 | * @throws Throwable 异常信息
58 | * @since 0.0.3
59 | */
60 | @Around("asyncPointcut()")
61 | public Object around(ProceedingJoinPoint point) throws Throwable {
62 | Method method = getCurrentMethod(point);
63 | Object target = point.getTarget();
64 | Object[] params = point.getArgs();
65 |
66 | return AsyncExecutor.submit(target, method, params);
67 | }
68 |
69 | /**
70 | * 获取当前方法信息
71 | *
72 | * @param point 切点
73 | * @return 方法
74 | */
75 | private Method getCurrentMethod(ProceedingJoinPoint point) {
76 | try {
77 | Signature sig = point.getSignature();
78 | MethodSignature msig = (MethodSignature) sig;
79 | Object target = point.getTarget();
80 | return target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
81 | } catch (NoSuchMethodException e) {
82 | throw new CommonRuntimeException(e);
83 | }
84 | }
85 |
86 | }
87 |
--------------------------------------------------------------------------------
/async-test/src/test/java/com/github/houbb/async/test/service/UserServiceTest.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.test.service;
2 |
3 | import com.github.houbb.async.core.model.async.AsyncResult;
4 | import com.github.houbb.async.core.proxy.AsyncProxy;
5 | import com.github.houbb.async.test.service.impl.UserServiceDefault;
6 | import com.github.houbb.async.test.service.impl.UserServiceImpl;
7 | import org.junit.Test;
8 |
9 | /**
10 | * 用户测试
11 | * @author binbin.hou
12 | * date 2019/3/7
13 | * @since 0.0.1
14 | */
15 | public class UserServiceTest {
16 |
17 | /**
18 | * 默认不使用代理
19 | */
20 | @Test
21 | public void queryUserTest() {
22 | long start = System.currentTimeMillis();
23 | UserService userService = new UserServiceImpl();
24 | AsyncResult result = userService.queryUser("123");
25 | AsyncResult result2 = userService.queryUser("1234");
26 |
27 | System.out.println("查询结果" + result.getResult());
28 | System.out.println("查询结果" + result2.getResult());
29 | long end = System.currentTimeMillis();
30 | System.out.println("共计耗时: " + (end-start));
31 | }
32 |
33 | /**
34 | * 使用动态代理
35 | */
36 | @Test
37 | public void queryUserDynamicProxyTest() {
38 | long start = System.currentTimeMillis();
39 | UserService userService = new UserServiceImpl();
40 | UserService userServiceProxy = (UserService) AsyncProxy.getProxy(userService);
41 | AsyncResult result = userServiceProxy.queryUser("123");
42 | AsyncResult result2 = userServiceProxy.queryUser("1234");
43 |
44 | System.out.println("查询结果" + result.getResult());
45 | System.out.println("查询结果" + result2.getResult());
46 | long end = System.currentTimeMillis();
47 | System.out.println("共计耗时: " + (end-start));
48 | }
49 |
50 | /**
51 | * CGLIB 代理
52 | */
53 | @Test
54 | public void queryUserCglibProxyTest() {
55 | long start = System.currentTimeMillis();
56 | UserServiceDefault userService = new UserServiceDefault();
57 | UserServiceDefault userServiceProxy = (UserServiceDefault) AsyncProxy.getProxy(userService);
58 | AsyncResult result = userServiceProxy.queryUser("123");
59 | AsyncResult result2 = userServiceProxy.queryUser("1234");
60 |
61 | System.out.println("查询结果" + result.getResult());
62 | System.out.println("查询结果" + result2.getResult());
63 | long end = System.currentTimeMillis();
64 | System.out.println("共计耗时: " + (end-start));
65 | }
66 |
67 |
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/async-core/src/main/java/com/github/houbb/async/core/executor/AsyncExecutor.java:
--------------------------------------------------------------------------------
1 | package com.github.houbb.async.core.executor;
2 |
3 | import com.github.houbb.async.api.core.async.IAsyncResult;
4 | import com.github.houbb.async.api.core.executor.IAsyncExecutor;
5 | import com.github.houbb.async.core.exception.AsyncRuntimeException;
6 | import com.github.houbb.async.core.model.async.AsyncResult;
7 | import com.github.houbb.log.integration.core.Log;
8 | import com.github.houbb.log.integration.core.LogFactory;
9 |
10 | import java.lang.reflect.InvocationTargetException;
11 | import java.lang.reflect.Method;
12 | import java.util.concurrent.*;
13 |
14 | /**
15 | * 异步执行器
16 | * @author binbin.hou
17 | * date 2019/3/8
18 | * @since 0.0.2
19 | */
20 | public class AsyncExecutor extends ThreadPoolExecutor implements IAsyncExecutor {
21 |
22 | private static final Log log = LogFactory.getLog(AsyncExecutor.class);
23 |
24 | //region 私有属性
25 | /**
26 | * 是否初始化
27 | */
28 | private static volatile boolean isInit = false;
29 |
30 | /**
31 | * 是否被销毁
32 | */
33 | private static volatile boolean isDestroy = false;
34 |
35 | /**
36 | * 线程执行器
37 | */
38 | private static ExecutorService executorService = null;
39 | //endregion
40 |
41 | //region 构造器
42 | public AsyncExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) {
43 | super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
44 | }
45 |
46 | public AsyncExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory) {
47 | super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
48 | }
49 |
50 | public AsyncExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, RejectedExecutionHandler handler) {
51 | super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
52 | }
53 |
54 | public AsyncExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
55 | super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
56 | }
57 | //endregion
58 |
59 | @SuppressWarnings("all")
60 | public static IAsyncResult submit(final Object target, final Method method, final Object[] objects) {
61 | // 初始化的判断
62 | if(!isInit) {
63 | init();
64 | }
65 |
66 | Future future = executorService.submit(new Runnable() {
67 | @Override
68 | public void run() {
69 | try {
70 | method.invoke(target, objects);
71 | } catch (IllegalAccessException | InvocationTargetException e) {
72 | log.error("异步执行遇到异常: ", e);
73 | }
74 | }
75 | });
76 | AsyncResult asyncResult = new AsyncResult<>();
77 | asyncResult.setFuture(future);
78 | return asyncResult;
79 | }
80 |
81 | /**
82 | * 初始化
83 | * 1. 暂时不添加配置相关的信息
84 | * 2. 最后调整状态
85 | */
86 | private static synchronized void init() {
87 | try {
88 | if(isInit) {
89 | return;
90 | }
91 |
92 | // 各种属性配置
93 | // 淘汰策略
94 | // 最佳线程数量
95 | executorService = Executors.newFixedThreadPool(10);
96 | updateExecutorStatus(true);
97 | } catch (Exception e) {
98 | throw new AsyncRuntimeException(e);
99 | }
100 | }
101 |
102 |
103 |
104 | /**
105 | * 销毁容器
106 | * 1. 销毁的时候进行等待,确保任务的正常执行完成。
107 | * 2. 任务执行的统计信息,后期添加。
108 | */
109 | private static synchronized void destroy() {
110 | if(isDestroy) {
111 | return;
112 | }
113 |
114 | executorService = null;
115 | updateExecutorStatus(false);
116 | }
117 |
118 | /**
119 | * 更新执行器的状态
120 | * @param initStatus 初始化状态
121 | */
122 | private static void updateExecutorStatus(final boolean initStatus) {
123 | isInit = initStatus;
124 | isDestroy = !isInit;
125 | }
126 |
127 | }
128 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 项目简介
2 |
3 | 基于注解的 java 异步处理框架。
4 |
5 | [](http://mvnrepository.com/artifact/com.github.houbb/async)
6 | [](https://www.travis-ci.org/houbb/async?branch=master)
7 | [](https://coveralls.io/github/houbb/async?branch=master)
8 |
9 | ## 设计目的
10 |
11 | 并行执行可以大幅度提升程序的运行速度,有效利用 CPU 资源。
12 |
13 | 但是单独为每次方法都使用线程池手写,显然不够优雅,复用性也很差。
14 |
15 | ## 特性
16 |
17 | - 支持接口类的动态代理异步
18 |
19 | - 支持非接口类的 CGLIB 代理异步
20 |
21 | # 快速入门
22 |
23 | 具体测试代码,参见 async-test 模块。
24 |
25 | ## 需要
26 |
27 | - jdk1.7+
28 |
29 | - maven 3.x+
30 |
31 | ## 引入 maven
32 |
33 | ```xml
34 |
35 | com.github.houbb
36 | async-core
37 | 0.0.3
38 |
39 | ```
40 |
41 | ## 定义测试对象
42 |
43 | - 定义接口
44 |
45 | 当前版本没有引入 CGLIB 等字节码包,需要实现接口才能异步并行。
46 |
47 | 如果不实现接口,则不实现异步并行。
48 |
49 | 下个版本会添加 CGLIB,则不用实现接口。
50 |
51 | ```java
52 | import com.github.houbb.async.core.model.async.AsyncResult;
53 |
54 | /**
55 | * 用户服务接口
56 | * @author binbin.hou
57 | * date 2019/3/7
58 | * @since 0.0.1
59 | */
60 | public interface UserService {
61 |
62 | /**
63 | * 查询用户信息
64 | * @param id 主键
65 | * @return 结果
66 | */
67 | AsyncResult queryUser(final String id);
68 |
69 | }
70 | ```
71 |
72 | - 定义测试实现类
73 |
74 | ```java
75 | public class UserServiceImpl implements UserService {
76 |
77 | @Override
78 | public AsyncResult queryUser(String id) {
79 | System.out.println("开始根据用户id 查询用户信息 " + id);
80 | try {
81 | // 沉睡模拟处理耗时
82 | TimeUnit.SECONDS.sleep(3);
83 | } catch (InterruptedException e) {
84 | e.printStackTrace();
85 | }
86 | final String result = id + "-result";
87 | System.out.println("结束根据用户id 查询用户信息 " + result);
88 |
89 | AsyncResult asyncResult = new AsyncResult<>();
90 | asyncResult.setValue(result);
91 | return asyncResult;
92 | }
93 |
94 | }
95 | ```
96 |
97 | ## 测试
98 |
99 | ### 不使用代理
100 |
101 | 常规使用方式
102 |
103 | ```java
104 | /**
105 | * 默认不使用代理
106 | */
107 | @Test
108 | public void queryUserTest() {
109 | long start = System.currentTimeMillis();
110 | UserService userService = new UserServiceImpl();
111 | AsyncResult result = userService.queryUser("123");
112 | AsyncResult result2 = userService.queryUser("1234");
113 |
114 | System.out.println("查询结果" + result.getResult());
115 | System.out.println("查询结果" + result2.getResult());
116 | long end = System.currentTimeMillis();
117 | System.out.println("共计耗时: " + (end-start));
118 | }
119 | ```
120 |
121 | - 日志信息
122 |
123 | ```
124 | 开始根据用户id 查询用户信息 123
125 | 结束根据用户id 查询用户信息 123-result
126 | 开始根据用户id 查询用户信息 1234
127 | 结束根据用户id 查询用户信息 1234-result
128 | 查询结果123-result
129 | 查询结果1234-result
130 | 共计耗时: 6009
131 | ```
132 |
133 | ### 使用代理
134 |
135 | ```java
136 | /**
137 | * 使用动态代理
138 | */
139 | @Test
140 | public void queryUserDynamicProxyTest() {
141 | long start = System.currentTimeMillis();
142 | UserService userService = new UserServiceImpl();
143 | UserService userServiceProxy = (UserService) AsyncProxy.getProxy(userService);
144 | AsyncResult result = userServiceProxy.queryUser("123");
145 | AsyncResult result2 = userServiceProxy.queryUser("1234");
146 |
147 | System.out.println("查询结果" + result.getResult());
148 | System.out.println("查询结果" + result2.getResult());
149 | long end = System.currentTimeMillis();
150 | System.out.println("共计耗时: " + (end-start));
151 | }
152 | ```
153 |
154 | - 日志信息
155 |
156 | ```
157 | 开始根据用户id 查询用户信息 123
158 | 开始根据用户id 查询用户信息 1234
159 | 结束根据用户id 查询用户信息 123-result
160 | 结束根据用户id 查询用户信息 1234-result
161 | 查询结果123-result
162 | 查询结果1234-result
163 | 共计耗时: 3009
164 | ```
165 |
166 | 同样的功能实现,节约了将近一半的时间。
167 |
168 | # 拓展阅读
169 |
170 | [Async-01-项目模块说明](doc/blog/async-01-项目模块介绍.md)
171 |
172 | [Async-02-CGLIB代理.md](doc/blog/async-02-CGLIB代理.md)
173 |
174 | [Async-03-Spring-整合.md](doc/blog/async-03-spring-整合.md)
175 |
176 | # 后期 Road-MAP
177 |
178 | - [ ] 开启可以指定为 sync 或者 async 的方式执行。
179 |
180 | - [ ] 对于返回值的优化,返回值可以是任何类型。
181 |
182 | - [ ] 添加 spring-boot-starter 特性
183 |
184 | # 中间件等工具开源矩阵
185 |
186 | [heaven: 收集开发中常用的工具类](https://github.com/houbb/heaven)
187 |
188 | [rpc: 基于 netty4 实现的远程调用工具](https://github.com/houbb/rpc)
189 |
190 | [mq: 简易版 mq 实现](https://github.com/houbb/mq)
191 |
192 | [ioc: 模拟简易版 spring ioc](https://github.com/houbb/ioc)
193 |
194 | [mybatis: 简易版 mybatis](https://github.com/houbb/mybatis)
195 |
196 | [cache: 渐进式 redis 缓存](https://github.com/houbb/cache)
197 |
198 | [jdbc-pool: 数据库连接池实现](https://github.com/houbb/jdbc-pool)
199 |
200 | [sandglass: 任务调度时间工具框架](https://github.com/houbb/sandglass)
201 |
202 | [sisyphus: 支持注解的重试框架](https://github.com/houbb/sisyphus)
203 |
204 | [resubmit: 防止重复提交框架,支持注解](https://github.com/houbb/resubmit)
205 |
206 | [auto-log: 日志自动输出](https://github.com/houbb/auto-log)
207 |
208 | [async: 多线程异步并行框架](https://github.com/houbb/async)
209 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.github.houbb
8 | async
9 | pom
10 | 0.0.5-SNAPSHOT
11 |
12 | async-api
13 | async-core
14 | async-test
15 | async-spring
16 | async-springboot-starter
17 | async-test2
18 |
19 |
20 |
21 |
22 |
23 | 3.2
24 | 3.2
25 | 2.18.1
26 | false
27 | false
28 |
29 | 2.2.1
30 | 2.10.4
31 | 1.5
32 |
33 | 4.3.0
34 | 2.7
35 |
36 |
37 | UTF-8
38 | 1.7
39 |
40 |
41 | 0.1.112
42 | 1.1.8
43 | 0.0.2
44 | 0.0.1
45 |
46 |
47 | 1.5.22.RELEASE
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 | ${project.groupId}
56 | async-api
57 | ${project.version}
58 |
59 |
60 | ${project.groupId}
61 | async-core
62 | ${project.version}
63 |
64 |
65 | ${project.groupId}
66 | async-spring
67 | ${project.version}
68 |
69 |
70 | ${project.groupId}
71 | async-springboot-starter
72 | ${project.version}
73 |
74 |
75 |
76 |
77 | ${project.groupId}
78 | heaven
79 | ${heaven.version}
80 |
81 |
82 | ${project.groupId}
83 | log-integration
84 | ${log-integration.version}
85 |
86 |
87 |
88 | com.github.houbb
89 | aop-core
90 | ${aop.version}
91 |
92 |
93 | com.github.houbb
94 | aop-spring
95 | ${aop.version}
96 |
97 |
98 |
99 | com.github.houbb
100 | test-spring
101 | ${test.version}
102 |
103 |
104 |
105 | org.springframework.boot
106 | spring-boot-starter
107 | ${spring-boot.version}
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 | org.apache.maven.plugins
118 | maven-compiler-plugin
119 | ${plugin.compiler.version}
120 |
121 | ${project.compiler.level}
122 | ${project.compiler.level}
123 | ${project.build.sourceEncoding}
124 | -proc:none
125 |
126 |
127 |
128 |
129 |
130 | org.apache.maven.plugins
131 | maven-javadoc-plugin
132 | ${plugin.maven-javadoc-plugin.version}
133 |
134 |
135 |
136 |
137 |
138 |
139 | async
140 | The async tool for java.
141 |
142 |
143 | org.sonatype.oss
144 | oss-parent
145 | 7
146 |
147 |
148 |
149 | The Apache Software License, Version 2.0
150 | http://www.apache.org/licenses/LICENSE-2.0.txt
151 | repo
152 |
153 |
154 |
155 | https://github.com/houbb/async
156 | https://github.com/houbb/async.git
157 | https://houbb.github.io/
158 |
159 |
160 |
161 | houbb
162 | houbinbin.echo@gmail.com
163 | https://houbb.github.io/
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 | release
173 |
174 |
175 |
176 |
177 | org.apache.maven.plugins
178 | maven-source-plugin
179 | ${plugin.maven-source-plugin.version}
180 |
181 |
182 | package
183 |
184 | jar-no-fork
185 |
186 |
187 |
188 |
189 |
190 |
191 | org.apache.maven.plugins
192 | maven-javadoc-plugin
193 | ${plugin.maven-javadoc-plugin.version}
194 |
195 |
196 | attach-javadocs
197 |
198 | jar
199 |
200 |
201 |
202 |
203 | UTF-8
204 | true
205 | UTF-8
206 | UTF-8
207 | true
208 | -Xdoclint:none
209 |
210 |
211 | date
212 | a
213 | 日期
214 |
215 |
216 |
217 |
218 |
219 |
220 | org.apache.maven.plugins
221 | maven-gpg-plugin
222 | ${plugin.maven-gpg-plugin.version}
223 |
224 |
225 | verify
226 |
227 | sign
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 | org.eluder.coveralls
237 | coveralls-maven-plugin
238 | ${plugin.coveralls.version}
239 |
240 |
241 |
242 | org.codehaus.mojo
243 | cobertura-maven-plugin
244 | ${plugin.cobertura.version}
245 |
246 | xml
247 | 256m
248 |
249 | true
250 |
251 |
252 | **/*Test.class
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 | oss
264 | https://oss.sonatype.org/content/repositories/snapshots/
265 |
266 |
267 | oss
268 | https://oss.sonatype.org/service/local/staging/deploy/maven2/
269 |
270 |
271 |
272 |
273 |
274 |
--------------------------------------------------------------------------------