开源:轻量级异步编排框架

前言

为了更快、更方便的对方法实现异步并排调用,因此实现了一个通过注解就可对方法/类进行异步调用的轻量级异步并排框架。

项目地址:https://gitee.com/madaoEE/my-async

介绍

一个简单实现的异步框架,通过注解对方法、类对象添加异步操作

核心技术:Java + 线程池 + 动态代理/AOP

设计目的

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

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

执行流程

与传统执行对比

具体实现:Java环境采用动态代理实现调用;Spring环境采用AOP实现代理调用

快速入门

需要的环境:

  • JDK 17
  • Maven 3.x+

快速使用

下面以SpringBoot环境做测试

引入依赖

复制代码
		<dependency>
			<groupId>org.pxl</groupId>
			<artifactId>async-springboot-starter</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>

添加测试对象

1、实现接口

复制代码
import org.pxl.core.model.AsyncResult;

/**
 * @author MADAO
 * @create 2024 - 10 - 13 17:35
 */
public interface UserService {

    public AsyncResult<String> queryUser(String id);

    public AsyncResult<String> query(String id);
}

2、接口实现类

复制代码
import org.pxl.api.annotation.Async;
import org.pxl.core.model.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * 使用异步框架
 *
 * @author MADAO
 * @create 2024 - 10 - 13 15:37
 */
@Service
public class UserServiceImpl implements UserService{

//异步方法
    @Async
    public AsyncResult<String> queryUser(String id) {
        System.out.println("开始根据用户id 查询用户信息 " + id);
        try {
            // 沉睡模拟处理耗时
            Thread.sleep(300);
        } 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;
    }

//非异步方法
    @Override
    public AsyncResult<String> query(String id) {
        System.out.println("开始根据用户id 查询用户信息 " + id);
        try {
            // 沉睡模拟处理耗时
            Thread.sleep(300);
        } 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;
    }


}

开始测试

添加测试类:

复制代码
package com.example.asyncspringboottest;

import com.example.asyncspringboottest.service.UserService;
import org.junit.jupiter.api.Test;
import org.pxl.core.model.AsyncResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class AsyncSpringbootTestApplicationTests {

	@Autowired
	private UserService	userService;


	@Test
	public void test() {
		long start = System.currentTimeMillis();
//		异步测试
//		AsyncResult<String> result = userService.queryUser("123");
//		AsyncResult<String> result2 = userService.queryUser("1234");

		AsyncResult<String> result = userService.query("123");
		AsyncResult<String> result2 = userService.query("1234");
		System.out.println("查询结果" + result.getResult());
		System.out.println("查询结果" + result2.getResult());
		long end = System.currentTimeMillis();
		System.out.println("共计耗时: " + (end-start));
	}

}

非异步测试结果:

异步测试结果:

方法实现效率高了一倍

总结

我们的项目基本功能已经完善,但仍有一些扩展空间。通常情况下,可以使用JDK自带的CompletableFuture结合自定义线程池来实现大部分异步编排的场景。然而,当需要对多个方法或类进行频繁的异步调用时,这种方式可能会变得繁琐且难以维护。

为了解决这个问题,我们开发了这个异步框架。该框架通过简单的注解机制,允许开发者轻松地将同步方法转换为异步执行。这样不仅简化了异步编程的复杂度,还提高了代码的可读性和可维护性。

如果本项目对您有帮助,也欢迎您给我一个Star 🌟,这会帮助更多的人了解到本项目。

相关推荐
LucianaiB23 分钟前
百度开源文心4.5系列开源21款模型,实测 ERNIE-4.5-VL-28B-A3B-Paddle 多项评测结果超 Qwen3-235B-A22B
百度·开源·文心大模型·paddle·gitcode
popeye00829 分钟前
免费开源 RPA 软件困境与 tdRPA 的创新破局
开源·rpa
初九之潜龙勿用40 分钟前
文心一言4.5开源模型测评:ERNIE-4.5-0.3B超轻量模型部署指南
开源·dubbo·文心一言
wuxuanok43 分钟前
Web后端开发-分层解耦
java·笔记·后端·学习
NetX行者1 小时前
FastMCP:用于构建MCP服务器的开源Python框架
服务器·python·开源
31535669131 小时前
ClipReader:一个剪贴板英语单词阅读器
前端·后端
ladymorgana1 小时前
【Spring Boot】HikariCP 连接池 YAML 配置详解
spring boot·后端·mysql·连接池·hikaricp
neoooo1 小时前
别慌,Java只有值传递——一次搞懂“为啥我改了它还不变”!
java·后端·spring
用户7785371836961 小时前
一力破万法:从0实现一个http代理池
后端·爬虫
JuiceFS2 小时前
JuiceFS 社区版 V1.3 正式发布:支持 Python SDK、亿级备份加速、SQL 和 Windows 全面优化
开源·资讯