介绍在SpringBoot项目中,使用@Async不生效的原因介绍和分析;
代码参考gitee仓库:spring-boot-2022-05: 主要是介绍Spring框架注解、常用的功能的使用案例,记录平时遇到的技术知识点,进行实践操作; - Gitee.com
1.启动类中没有添加注解@EnableAsync;
2.同一个类中调用含有@Async的方法;因为@Transactional和@Async是采用Spring AOP原理实现的,需要通过代理对象调用其方法。
3.方法必须是public修饰,且返回值是void或Future。
4.使用ThreadPoolTaskExecutor对象创建自定义线程池?
5.关于@Async("param")中参数param含义?
正确使用步骤事项:
第一步:启动类中应该添加注解@EnableAsync;
第二步:执行方法和异步方法不在同一个类中。
第三步:异步方法必须是public 修饰,且返回值是void或Future;
1.启动类需要添加注解@EnableAsync
package com.dbzhang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
/**
* @program: spring-boot-2022-05
* @description:
* @author: zdb
* @motto: 认真写好每一行代码
* @create: 2022-05-05 13:15
*/
@SpringBootApplication
@EnableAsync
public class ApplicationRun {
public static void main(String[] args) {
SpringApplication.run(ApplicationRun.class, args);
}
}
2.PlayerService业务类:通过代理对象调用异步方法发送短信asyncMethodClass.sendMessage(playerId, msg);
package com.dbzhang.asyc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* @program: spring-boot-2022-05
* @description:
* @author: zdb
* @motto: 认真写好每一行代码
* @create: 2022-10-18 13:11
*/
@Component
@Slf4j
public class PlayerService {
@Resource
private AsyncMethodClass asyncMethodClass;
public void register(Long playerId) {
log.info("begin----注册用户编号,playerId:{}", playerId);
//异步发送短信
String msg = "异步发送短信";
asyncMethodClass.sendMessage(playerId, msg);
log.info("end----注册用户编号,playerId:{}", playerId);
}
}
3.AsyncMethodClass中异步方法,使用@Async("param")标记异步方法,其中param参数是引用线程池的bean名称,但是Spring容器中必须存在这个bean。
package com.dbzhang.asyc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* @program: spring-boot-2022-05
* @description:
* @author: zdb
* @motto: 认真写好每一行代码
* @create: 2022-10-18 13:12
*/
@Component
@Slf4j
public class AsyncMethodClass {
/**
* 测试调用异常方法:发送短信
*
*/
@Async("threadPoolTaskExecutor")
public void sendMessage(Long playerId, String msg) {
log.info("玩家注册编号playerId:{},发送短信信息msg:{}", playerId, msg);
}
}
4.自定义线程池ThreadPoolTaskExector,并交给Spring容器管理。
package com.dbzhang.asyc;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
* @program: spring-boot-2022-05
* @description: 自定义线程池配置
* @author: zdb
* @motto: 认真写好每一行代码
* @create: 2022-10-19 12:50
*/
@Configuration
public class AsyncThreadParam {
@Value("${threadPool.corePoolSize}")
private int corePoolSize;
@Value("${threadPool.maxPoolSize}")
private int maxPoolSize;
@Value("${threadPool.keepAliveSeconds}")
private int keepAliveSeconds;
@Value("${threadPool.queueCapacity}")
private int queueCapacity;
@Value("${threadPool.namePrefix}")
private String namePrefix;
/**
* 初始化线程池参数
*
* @return
*/
@Bean(value = "threadPoolTaskExecutor")
public ThreadPoolTaskExecutor initThreadPoolTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);
threadPoolTaskExecutor.setThreadNamePrefix(namePrefix);
// threadPoolTaskExecutor.setRejectedExecutionHandler();
return threadPoolTaskExecutor;
}
}
5.配置文件中执行线程参数:
#自定义线程池ThreadPoolTaskExecutor参数
threadPool:
corePoolSize: 2
maxPoolSize: 5
keepAliveSeconds: 60
queueCapacity: 10
namePrefix: async-zdb-
6.测试类:
package com.dbzhang.async;
import com.dbzhang.asyc.PlayerService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @program: spring-boot-2022-05
* @description:
* @author: zdb
* @motto: 认真写好每一行代码
* @create: 2022-10-18 13:18
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class AsyncTest {
@Autowired
private PlayerService playerService;
@Test
public void testAsync() {
playerService.register(123456789L);
}
}
7.执行结果:

exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
?博客主页:https://xiaoy.blog.csdn.net?本文由呆呆敲代码的小Y原创,首发于CSDN??学习专栏推荐:Unity系统学习专栏?游戏制作专栏推荐:游戏制作?Unity实战100例专栏推荐:Unity实战100例教程?欢迎点赞?收藏⭐留言?如有错误敬请指正!?未来很长,值得我们全力奔赴更美好的生活✨------------------❤️分割线❤️-------------------------
在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.
目录H2数据库入门以及实际开发时的使用1.H2数据库的初识1.1H2数据库介绍1.2为什么要使用嵌入式数据库?1.3嵌入式数据库对比1.3.1性能对比1.4技术选型思考2.H2数据库实战2.1H2数据库下载搭建以及部署2.1.1H2数据库的下载2.1.2数据库启动2.1.2.1windows系统可以在bin目录下执行h2.bat2.1.2.2同理可以通过cmd直接使用命令进行启动:2.1.2.3启动后控制台页面:2.1.3spring整合H2数据库2.1.3.1引入依赖文件2.1.4数据库通过file模式实际保存数据的位置2.2H2数据库操作2.2.1Mysql兼容模式2.2.2Mysql模式
我们开始使用Ruby开发新游戏项目。我们决定使用其中一种异步Ruby服务器,但我们无法决定选择哪一种。选项是:歌利亚抽筋+消瘦/彩虹rack-fiber_pool+rack+thin/rainbowseventmachine_httpserver它们似乎都在处理HTTP请求。Cramp还支持开箱即用的Websocket和服务器端事件。您知道这些服务器的优缺点吗? 最佳答案 我使用eventmachine_httpserver公开了一个RESTfulAPIinanEventMachine-basedIRCbot绝对不会推荐它用于任何严
我一直在研究ruby的并行/异步处理能力,并阅读了许多文章和博客文章。我查看了EventMachine、Fibers、Revactor、Reia等。不幸的是,我无法为这个非常简单的用例找到简单、有效(且非IO阻塞)的解决方案:File.open('somelogfile.txt')do|file|whileline=file.gets#(R)ReadfromIOline=process_line(line)#(P)Processthelinewrite_to_db(line)#(W)WritetheoutputtosomeIO(DBorfile)endend你看到了吗,我的小脚本正
我使用RubyEventMachines已经有一段时间了,我想我已经了解它的基础知识了。但是,我不确定如何高效地读取大文件(120MB)。我的目标是逐行读取文件并将每一行写入Cassandra数据库(对于MySQL、PostgreSQL、MongoDB等也应该如此,因为Cassandra客户端明确支持EM)。这个简单的片段会阻塞react器,对吗?require'rubygems'require'cassandra'require'thrift_client/event_machine'EM.rundoFiber.newdorm=Cassandra.new('RankMetrics',
如果您希望在Spring中启用定时任务功能,则需要在主类上添加 @EnableScheduling 注解。这样Spring才会扫描 @Scheduled 注解并执行定时任务。在大多数情况下,只需要在主类上添加 @EnableScheduling 注解即可,不需要在Service层或其他类中再次添加。以下是一个示例,演示如何在SpringBoot中启用定时任务功能:@SpringBootApplication@EnableSchedulingpublicclassApplication{publicstaticvoidmain(String[]args){SpringApplication.ru
软件特点部署后能通过浏览器查看线上日志。支持Linux、Windows服务器。采用随机读取的方式,支持大文件的读取。支持实时打印新增的日志(类终端)。支持日志搜索。使用手册基本页面配置路径配置日志所在的目录,配置后按回车键生效,下拉框选择日志名称。选择日志后点击生效,即可加载日志。windows路径E:\java\project\log-view\logslinux路径/usr/local/XX历史模式历史模式下,不会读取新增的日志。针对历史文件可以分页读取,配置分页大小、跳转。历史模式下,支持根据关键词搜索。目前搜索引擎使用的是jdk自带类库,搜索速度相对较低,优点是比较简单。2G日志全文搜
1.依赖导入org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-validation2.validation常用注解@Null被注释的元素必须为null@NotNull被注释的元素不能为null,可以为空字符串@AssertTrue被注释的元素必须为true@AssertFalse被注释的元素必须为false@Min(value)被注释的元素必须是一个数字,其值必须大于等于指定的最小值@Max(value)被注释的元素必须是一个数字,其值必须小于等于指定的最大值@D