SpringBoot实现定时重启项目

SpringBoot系列教材 (六)- 基础 - 如何让springboot自动重启 目前的Springboot,当发生了任何修改之后,必须关闭后再启动Application类才能够生效,显得略微麻烦。Springboot提供了热部署的方式,当发现任何类发生了改变,马上通过JVM类加载的方式,加载最新的类到虚拟机中。这样就不需要重新启动也能看到修改后的效果了。重新启动Application,然后随便修改一下HelloController, 就会如图所示观察到控制台的自动重启现象。做法很简单,在pom.xml中新增加一个依赖和一个插件就行了。 阅读详情

现在有一个需求,是定时任务执行扫描,判断如果端口有变化,需要重启项目。思路是通过关闭应用程序上下文并从头创建一个新上下文来重新启动应用程序。具体代码如下:

1. 启动类定义重启方法

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author: 
 * @date: 2023-02-03 14:20
 * @description: agent的启动类
 */
@SpringBootApplication
public class AgentApplication {

    private static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        context = SpringApplication.run(AgentApplication.class, args);
    }

    /**
     * 重启服务
     */
    public static void restart() {
        ApplicationArguments args = context.getBean(ApplicationArguments.class);
        Thread thread = new Thread(() -> {
            context.close();
            context = SpringApplication.run(AgentApplication.class, args.getSourceArgs());
        });
        // 设置非守护线程
        thread.setDaemon(false);
        thread.start();
    }
}

2. 定时任务调用代码

import com.wrdxa.agent.AgentApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * @author: 
 * @date: 2023-02-03 16:19
 * @description: 定时任务的类
 */
@Configuration
@EnableScheduling
public class ScheduleTask {

    /**
     * 每隔1分钟执行一次查询,如果监测有变化,则进行重启
     */
    @Scheduled(cron = "*/30 * * * * ?")
    private void configureTasks() {
        System.out.println("开始重启");
        AgentApplication.restart();
    }
}
SpringBoot:通过web控制当前的SpringBoot程序重新启动 当前版本:SpringBoot2.3.12.RELEASE、JDK1.8 1. 声明 当前内容主要为本人学习和使用SpringBoot,实现通过web方式控制SpringBoot的程序重启操作,主要参考SpringBoot官方文档 通过观察发现重启是与RemoteSpringApplication有关 查看源码发现与Restarter有关 所以可以手动实现 2.源码寻找 RemoteSpringApplication源码 private void run(String[] args) { Restar 阅读详情

相关推荐

SpringBoot实现动态定时任务

项目情况:         在当前项目中需要一个定时任务来清除过期的校验码,如果使用数据库存储过程的话不方便维护。因此采用SpringBoot自带的方式来设置定时任务。技术说明:         SpringBoot自带的方式有两种可以实现:        一种是使用@Scheduled注解的方式,只需要在启动类或者它所在的类上添加@EnableScheduling注解允许执行定时任务,并且设置S...

我滴太阳233的博客 2万+

斤斤计较

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.spring...

测试0901-1 803

springboot不重启应用动态添加修改删除定时任务(以cron定时方式为例)

一、实现目标: 动态的添加修改删除cron定时任务,不需要重启应用,包括动态修改某个定时任务的cron定时时间以及任务内容的变化 二、实现思路: 1.设计存储任务信息的文件,此处采用json文件存储,认定task-xxx.json名称的文件为存储任务信息的文件,此处只做简单的日志打印 { "cron" : "0 17 15 * * ?", "taskInfo" : { ...

qq_37549757的博客 3511

spring boot应用内部实现应用自身重启的两种方式

spring boot 重启

weixin_44005802的博客 3297

SpringBoot开发——SpringBoot3.3 实现停止/重启定时任务

SpringBoot开发——SpringBoot3.3 停止/重启定时任务

bjzhang75的博客 2094

SGLang:结构化生成语言推理框架深度解读

SGLang是由斯坦福大学和加州大学伯克利分校联合提出的高性能大语言模型推理框架,针对复杂多轮对话、结构化输出等场景优化。其核心创新包括: RadixAttention机制:通过基数树数据结构实现跨请求的KV Cache自动复用,避免重复计算共享前缀 跳跃前瞻解码:对格式化输出中的固定模板部分直接填充,仅采样变量槽位,提升3-5倍生成速度 约束解码引擎:通过正则表达式和JSON Schema保证输出严格符合预定义格式 声明式DSL:提供类Python的编程接口简化复杂推理流程开发 相比传统框架,SGLang

Ricky Yang的专栏 331

修改定时任务不重启项目,SpringBoot 如何实现?

本文探讨了SpringBoot中定时任务的动态修改实现方案。针对传统@Scheduled注解无法运行时调整的问题,提出了两种核心解决方案:基于SchedulingConfigurer接口的动态Trigger方式,通过每次执行前重新读取外部配置实现动态调度;以及更灵活的ThreadPoolTaskScheduler手动控制方案,通过管理ScheduledFuture实现任务的动态增删改查。文章详细介绍了两种方案的实现原理、代码示例及适用场景,并延伸讨论了数据库持久化、集群环境处理、性能优化等高级话题,最后给出

油墨香^-^的博客 301

深入解析Android SoundTrigger架构:从HIDL到AIDL的演进与实践

本文深入解析Android SoundTrigger架构,从HIDL到AIDL的演进与实践。SoundTrigger是Android系统中低功耗语音唤醒的关键技术,通过分层设计和硬件协同实现高效语音识别。文章详细介绍了核心组件、HIDL与AIDL的对比及优化技巧,帮助开发者掌握语音模型优化和功耗管理。

brown的博客 230

修改定时任务不重启项目,SpringBoot如何实现?

点击上方“Java基基”,选择“设为星标”做积极的人,而不是积极废人!每天14:00更新文章,每天掉亿点点头发...源码精品专栏原创 | Java 2021超神之路,很肝~中文详细...

weixin_44421461的博客 642

大白话,Android Studio安装及其资源分享(离线gradle配置)

安装AndroidAtudio,并通过aliClound镜像与离线gradle配置,来跳过相对“繁文缛节”的过程,直接实现开发

wangzixuanemp的博客 3597

KBS投稿全流程拆解:从模板准备到条件接收的实战指南

本文详细拆解了KBS期刊投稿的全流程,从模板准备、投稿系统操作到返修策略和条件接收。重点介绍了Cover Letter撰写技巧、Latex模板选择、审稿周期管理以及返修文件的系统准备方法,帮助研究者高效完成KBS投稿并提升接收概率。

weixin_26938645的博客 702

springboot 定时任务 实现SchedulingConfigurer接口,修改定时任务不重启项目

业务场景描述: 定时任务的任务名称,cron(定时任务表达式),定时任务开关,存储在数据库表中。在不重启项目的情况下,修改定时任务表达式,可以实时获取新的定时任务执行时间规则;修改定时任务执行状态,可以随时开关定时任务。 使用技术:基于接口 SchedulingConfigurer 1、建表 管理定时任务 DROP TABLE IF EXISTS `scheduled`; CREATE TABLE `scheduled` ( `name` varchar(20) DEFAULT NULL, .

龙池小生 4万+

MATLAB新手必看:SVPWM七段式算法仿真全流程解析(附完整代码)

本文为MATLAB新手和电机控制初学者提供了SVPWM七段式算法的完整仿真与代码实现指南。文章详细解析了从扇区判断、矢量作用时间计算到PWM波形生成的全流程,并附带了可直接运行的MATLAB代码,帮助读者在理解FOC控制核心算法的基础上,快速掌握工程仿真与调试技巧。

weixin_29074295的博客 281

不重启项目,SpringBoot如何实现动态修改定时任务?

业务场景描述:定时任务的任务名称,cron(定时任务表达式),定时任务开关,存储在数据库表中。在不重启项目的情况下,修改定时任务表达式,可以实时获取新的定时任务执行时间规则;修改定时任务执...

独行侠梦的博客 1653

记录一次小项目中的重放攻击与解决方案

文章目录1、重放攻击简介2、重放攻击演示3、解决方案4、重放攻击验证 欢迎各位 前(提)来(出)讨(意)论(见)。 1、重放攻击简介 重放攻击(Replay Attacks)又称重播攻击、回放攻击,是指攻击者发送一个目的主机已接收过的包,来达到欺骗系统的目的,主要用于身份认证过程,破坏认证的正确性。 举个简单的小例子。 我们往系统里面插入一条数据,如果此次操作的数据包被攻击者获取,又恰巧后台没有对重复内容进行验证,那么攻击者就可以利用该数据包重复的发起请求,无限制的往数据库插入数据,即使请求包是加密的也

fiveSix 8725

VIVADO IP核之FIR插值器多相滤波仿真

网络上有许多文章介绍FIR低通滤波器的使用,包括仿真。关于FIR低通滤波器的使用,我之前的文章已经介绍过了,本文将继续深入介绍FIR插值器多相滤波的使用方法,并将FIR插值多相滤波的结果与MATLAB仿真计算的结果比较,验证了FIR插值器多相滤波使用正确。

m0_66360845的博客 3381

在springboot项目中实现一个定时任务执行的功能

这篇文章教大家在springboot项目中实现一个定时任务执行的功能,并且该定时任务要具备服务重启后也能执行的能力,本文会详细分析思路,并提供完整实现代码。

实用的博客 541

Modelsim:LPDDR5仿真(含美光仿真模型官方sv&vcs代码)

虽然,三星在设计跟量产上,早于其他家存储产商,但是镁光后来居上了,是业界首家支持全速 LPDDR5 的供应商,并且开发者在做开发的时候,也是用美光颗粒比较多,所以这里就先以美光的仿真模型为主进行仿真。 本工程为美光官方工程,支持Modelsim、VCS、ncverilog三种仿真器仿真。模型破解过,为完整system-verilog代码。

千歌叹尽执夏的博客 1280

如何实现图片定时切换_从源码研究如何不重启Springboot项目实现redis配置动态切换...

前言多个项目或微服务场景下,各个项目都需要配置redis数据源。但是,每当运维搞事时(修改redis服务地址或端口),各个项目都需要进行重启才能连接上最新的redis配置。服务一多,修改各个项目配置然后重启项目就非常蛋疼。所以我们想要找到一个可行的解决方案,能够不重启项目的情况下,修改配置,动态切换redis服务。如何实现切换redis连接刚遇到这个问题的时候,想必如果对spring-boot-s...

weixin_35549786的博客 99

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

SpringBoot项目销毁DisposableBean接口和 @PreDestroy退出方法

SpringBoot项目销毁DisposableBean接口和 @PreDestroy退出方法 这里用作定时框架偏移量处理 ,由于持久话已经被处理,但是偏移量还在,导致下次启动的时候,还会读取偏移量,持久化中不存在的任务还会被加载到定时队列中(意思就是这个定时任务,明明已经深删除了,但是项目中还有) 调用销毁方法,清除偏移量(程序即将关闭之前做的最后一件事) ① 接口实现 ...

scdncby的博客 6927

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

实现零重启自由编排任务的定时管理器

关于定时任务相信大家在项目中使用了很多,我们一般都是使用Spring自带的@EnableScheduling来实现定时任务虽然Spring自带的定时任务已经可以满足我们的业务需求,但是它还是有不足的地方我们需要改变定时任务的时间,此时我们就需要重启项目我们不需要这个定时任务了,我们就要修改代码,然后重启项目后期我们又要开启这个定时任务,我们又要修改代码,然后重启项目。

qq_66627105的博客 270

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

系统定时重启服务脚本案例

系统定时拉起服务

数据之心-小赵 1931

基于springboot ThreadPoolTaskScheduler类实现定时任务动态添加修改

基于springboot ThreadPoolTaskScheduler类实现定时任务动态添加修改_一只会飞的猪_的博客-优快云博客_threadpooltaskscheduler

z69183787的专栏 1428

springboot quartz动态定时_Spring Boot实现动态增删启停定时任务

点击上方☝SpringForAll社区轻松关注!及时获取有趣有料的技术文章本文来源:https://www.jianshu.com/p/0f68936393fd在spring boot项目中,可以通过@EnableScheduling注解和@Scheduled注解实现定时任务,也可以通过SchedulingConfigurer接口来实现定时任务。但是这两种方式不能动态添加、删除、启动、...

weixin_40008339的博客 229

项目实训记录(八)——springboot定时任务

1.适用情景 2.动态——基于接口(SchedulingConfigurer)实现定时任务 step1:把任务执行周期存储到数据库 step2:添加pom.xml配置信息(这个之前项目配过就不用了) step3:配置数据源(配过的就不用再配了) step4:创建定时器...

pinkray_c的博客 995
上一篇: Springboot+Mybatis,使用redis作为二级缓存
下一篇: HttpServletRequest获取请求参数
蓝色格子
博客等级 码龄9年 129粉丝 174原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值