# mykit-async
**Repository Path**: binghe001/mykit-async
## Basic Information
- **Project Name**: mykit-async
- **Description**: 🔥🔥🔥 mykit架构中独立出来的mykit-async异步编程框架,本异步框架实现了在Spring的基础上重写和扩展了异步执行的流程,主要提供了如下功能: 1、提供注解声明方式异步执行,对原代码无侵入; 2、提供编程式异步方法; 3、提供异步事件编程; 4、解决多层异步嵌套带来的线程阻塞问题;
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 4
- **Forks**: 4
- **Created**: 2022-05-19
- **Last Updated**: 2025-12-23
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# 作者及联系方式
作者:冰河
QQ:2711098650
微信公众号: 冰河技术
# 框架简述
mykit架构中独立出来的mykit-async异步编程框架,本异步框架实现了在Spring的基础上重写和扩展了异步执行的流程,主要提供了如下功能:
1、提供注解声明方式异步执行,对原代码无侵入(解决spring-async对有返回结果的需包装成Future对象问题);
2、提供编程式异步方法;
3、提供异步事件编程;
4、解决多层异步嵌套带来的线程阻塞问题(目前spring-async依然存在此问题);
# 功能描述
mykit-async 是一个基于Spring的异步并行框架;对高并发下的业务提供异步操作的能力,同时解决了Spring异步多层嵌套带来的线程阻塞问题,主要包括以下几个方面的功能,具体如下:
1、提供注解声明方式异步执行,对原代码无侵入(解决spring-async对有返回结果的需包装成Future对象问题);
2、提供编程式异步方法;
3、提供异步事件编程;
4、解决多层异步嵌套带来的线程阻塞问题(目前spring-async依然存在此问题);
# 框架结构描述
## mykit-async-spring
mykit-async 架构下主要以Spring为基础实现的异步编程框架,重写和扩展了Spring异步编程的接口和实现,并提供了Spring异步编程中一些没有的功能;
## mykit-async-spring-test
主要是对mykit-async-spring提供的测试工程,测试入口为:io.mykit.async.spring.test.AsyncTest
# 使用说明
1、引用mykit-async-spring说明
1)在pom.xml中添加如下配置:
```
* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.mykit.async.spring.test.entity; /** * @author liuyazhuang * @date 2018/9/9 22:31 * @description 测试实体类 * @version 1.0.0 */ public class User { private String name; private int age; public User() { } public User(int age, String name) { this.age = age; this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } ``` ### 2、创建测试的Service——TeacherService ``` /** * Copyright 2018-2118 the original author or authors. *
* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.mykit.async.spring.test.service; import io.mykit.async.spring.test.entity.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; /** * @author liuyazhuang * @date 2018/9/9 22:32 * @description 教师Service类 * @version 1.0.0 */ @Service public class TeacherService { private final static Logger logger = LoggerFactory.getLogger(TeacherService.class); public User addTeacher(User user) { logger.info("正在添加教师{}", user.getName()); return user; } } ``` ### 3、创建测试Service——UserService ``` /** * Copyright 2018-2118 the original author or authors. *
* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *
* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.mykit.async.spring.test.service;
import io.mykit.async.spring.annotation.Async;
import io.mykit.async.spring.test.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
/**
* @author liuyazhuang
* @date 2018/9/9 22:32
* @description 用户Service类
* @version 1.0.0
*/
@Service
public class UserService {
private final static Logger logger = LoggerFactory.getLogger(TeacherService.class);
@Async
public User addUser(User user) {
logger.info("正在添加用户{}", user.getName());
return user;
}
}
```
注意:这里的addUser方法,我们加上了@Async注解(这里的注解为:io.mykit.async.spring.annotation.Async)
到此,我们的准备工作完成。
接下来,就是实际测试的类型代码:
## 框架功能演示
### 1、添加@Async注解
```
@Async
public User addUser(User user) {
logger.info("正在添加用户{}", user.getName());
return user;
}
```
### 2、测试异步执行调用
```
@Test
public void testAsyncAnnotation() {
User user1 = userService.addUser(new User(34, "李一"));
User user2 = userService.addUser(new User(35, "李二"));
logger.info("异步任务已执行");
logger.info("执行结果 任务1:{} 任务2:{}", user1.getName(), user2.getName());
}
```
### 3、测试编程式异步
```
@Test
public void testAsyncSaveUser() {
User user = new User();
user.setName("张三");
user.setAge(18);
UserService service = AsyncTemplate.buildProxy(this.userService, 1000);
service.addUser(user);
logger.info("调用结束");
}
```
### 4、测试异步事件编程
```
@Test
public void testAsyncTemplate() {
AsyncTemplate.submit(new AsyncCallable