或者在需要开启异步功能的类上加上如下注解
@EnableAsync
注意:此注解类为:
io.mykit.async.spring.annotation.EnableAsync
在Spring的配置文件中加入如下配置:
<context:property-placeholder location=“classpath*:properties/async-default.properties, classpath*: properties/async.properties” ignore-unresolvable=“true”/>
来引入异步配置文件,classpath*:properties/async-default.properties文件为框架默认提供的异步配置文件
classpath*: properties/async.properties文件为自定义的异步配置文件,注意配置顺序必须为上述示例中的配置顺序,
这样自定义的配置文件属性会覆盖框架默认的配置文件属性。
框架默认的异步配置文件的内容如下:
#核心线程数(默认CPU核数*4)
async.corePoolSize=8
#最大线程数
async.maxPoolSize=24
#最大队列size
async.maxAcceptCount=100
#线程空闲时间
async.keepAliveTime=10000
#拒绝服务处理方式 (不建议修改)
async.rejectedExecutionHandler=CALLERRUN
#是否允许线程自动超时销毁(不建议修改)
async.allowCoreThreadTimeout=true
代码演示
前期准备
1、创建测试实体类
/**
-
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.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;
}
@Async
public String getName(){
logger.info(“正在添加用户姓名{}”, “张三”);
return “张三”;
}
}
注意:这里的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, “李二”));
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
[外链图片转存中…(img-YLOQAooy-1715842702101)]
[外链图片转存中…(img-mK1DfpOi-1715842702102)]
[外链图片转存中…(img-uLOd0kgM-1715842702102)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!