com.github.houbb:async-test

The async tool for java.

License

License

GroupId

GroupId

com.github.houbb
ArtifactId

ArtifactId

async-test
Last Version

Last Version

0.0.4
Release Date

Release Date

Type

Type

jar
Description

Description

The async tool for java.

Download async-test

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.houbb/async-test/ -->
<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>async-test</artifactId>
    <version>0.0.4</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.houbb/async-test/
implementation 'com.github.houbb:async-test:0.0.4'
// https://jarcasting.com/artifacts/com.github.houbb/async-test/
implementation ("com.github.houbb:async-test:0.0.4")
'com.github.houbb:async-test:jar:0.0.4'
<dependency org="com.github.houbb" name="async-test" rev="0.0.4">
  <artifact name="async-test" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.houbb', module='async-test', version='0.0.4')
)
libraryDependencies += "com.github.houbb" % "async-test" % "0.0.4"
[com.github.houbb/async-test "0.0.4"]

Dependencies

compile (4)

Group / Artifact Type Version
com.github.houbb : async-api jar 0.0.4
com.github.houbb : async-core jar 0.0.4
com.github.houbb : async-spring jar 0.0.4
com.github.houbb : test-spring jar 0.0.1

Project Modules

There are no modules declared in this project.

项目简介

基于注解的 java 异步处理框架。

Maven Central Build Status Coverage Status

设计目的

并行执行可以大幅度提升程序的运行速度,有效利用 CPU 资源。

但是单独为每次方法都使用线程池手写,显然不够优雅,复用性也很差。

特性

  • 支持接口类的动态代理异步

  • 支持非接口类的 CGLIB 代理异步

快速入门

具体测试代码,参见 async-test 模块。

需要

  • jdk1.7+

  • maven 3.x+

引入 maven

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>async-core</artifactId>
    <version>0.0.3</version>
</dependency>

定义测试对象

  • 定义接口

当前版本没有引入 CGLIB 等字节码包,需要实现接口才能异步并行。

如果不实现接口,则不实现异步并行。

下个版本会添加 CGLIB,则不用实现接口。

import com.github.houbb.async.core.model.async.AsyncResult;

/**
 * 用户服务接口
 * @author binbin.hou
 * date 2019/3/7
 * @since 0.0.1
 */
public interface UserService {

    /**
     * 查询用户信息
     * @param id 主键
     * @return 结果
     */
    AsyncResult<String> queryUser(final String id);

}
  • 定义测试实现类
public class UserServiceImpl implements UserService {

    @Override
    public AsyncResult<String> queryUser(String id) {
        System.out.println("开始根据用户id 查询用户信息 " + id);
        try {
            // 沉睡模拟处理耗时
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        final String result = id + "-result";
        System.out.println("结束根据用户id 查询用户信息 " + result);

        AsyncResult<String> asyncResult = new AsyncResult<>();
        asyncResult.setValue(result);
        return asyncResult;
    }

}

测试

不使用代理

常规使用方式

/**
 * 默认不使用代理
 */
@Test
public void queryUserTest() {
    long start = System.currentTimeMillis();
    UserService userService = new UserServiceImpl();
    AsyncResult<String> result = userService.queryUser("123");
    AsyncResult<String> result2 = userService.queryUser("1234");

    System.out.println("查询结果" + result.getResult());
    System.out.println("查询结果" + result2.getResult());
    long end = System.currentTimeMillis();
    System.out.println("共计耗时: " + (end-start));
}
  • 日志信息
开始根据用户id 查询用户信息 123
结束根据用户id 查询用户信息 123-result
开始根据用户id 查询用户信息 1234
结束根据用户id 查询用户信息 1234-result
查询结果123-result
查询结果1234-result
共计耗时: 6009

使用代理

/**
 * 使用动态代理
 */
@Test
public void queryUserDynamicProxyTest() {
    long start = System.currentTimeMillis();
    UserService userService = new UserServiceImpl();
    UserService userServiceProxy = (UserService) AsyncProxy.getProxy(userService);
    AsyncResult<String> result = userServiceProxy.queryUser("123");
    AsyncResult<String> result2 = userServiceProxy.queryUser("1234");

    System.out.println("查询结果" + result.getResult());
    System.out.println("查询结果" + result2.getResult());
    long end = System.currentTimeMillis();
    System.out.println("共计耗时: " + (end-start));
}
  • 日志信息
开始根据用户id 查询用户信息 123
开始根据用户id 查询用户信息 1234
结束根据用户id 查询用户信息 123-result
结束根据用户id 查询用户信息 1234-result
查询结果123-result
查询结果1234-result
共计耗时: 3009

同样的功能实现,节约了将近一半的时间。

拓展阅读

Async-01-项目模块说明

Async-02-CGLIB代理.md

Async-03-Spring-整合.md

后期 Road-MAP

  • 开启可以指定为 sync 或者 async 的方式执行。

  • 对于返回值的优化,返回值可以是任何类型。

  • 添加 spring-boot-starter 特性

Versions

Version
0.0.4
0.0.3
0.0.2
0.0.1