update 每秒事件,动态配置工具类

master
管理员 10 months ago
parent 5154a08a0e
commit 1b89eda79c

@ -0,0 +1,53 @@
package com.ruoyi.common.event;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.context.ApplicationEvent;
/**
* <pre>
* -
* Author : J.L.Zhou
* E-Mail : 2233875735@qq.com
* Tel : 151 1104 7708
* Date : 20221227 3:10:39
* Version : 1.0
* Copyright 2022 jlzhou.top Inc. All rights reserved.
* Warning: this content is only for internal circulation of the company.
* It is forbidden to divulge it or use it for other commercial purposes.
* </pre>
*/
public class PerSecondEvent extends ApplicationEvent {
/**
*
*/
private static final long serialVersionUID = 1752655123185832181L;
private static AtomicInteger globalCount = new AtomicInteger(0);
private static final int MAX = 24*365*60*60;
private int count;
public PerSecondEvent() {
super(0);
if(globalCount.incrementAndGet()>MAX) {
globalCount.set(0);
}
count=globalCount.get();
super.source= count;
}
public int getCount() {
return count;
}
@Override
public String toString() {
return " 每秒都会触发的事件: " + count;
}
}

@ -0,0 +1,16 @@
package com.ruoyi.common.runner;
import com.ruoyi.common.event.PerSecondEvent;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
@Configuration
public class PerSecondRunner {
@Scheduled(fixedRate = 1000)
public void PerSecondRunner() {
SpringUtils.publishEvent(new PerSecondEvent());
}
}

@ -0,0 +1,26 @@
package com.ruoyi.common.utils;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
public class DynConfigUtils {
/**
* bean
* @param config bean
* @return bean
* @param <T>
*/
public static <T> T toBean(T config) {
return (T)BeanUtil.toBean(config, config.getClass());
}
/**
* beanconfig
* @param bean bean
* @param config bean
*/
public static void setBean(Object bean, Object config) {
BeanUtil.copyProperties(bean,config, CopyOptions.create(config.getClass(),true));
}
}

@ -5,6 +5,7 @@ import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions; import cn.hutool.core.bean.copier.CopyOptions;
import com.ruoyi.common.annotation.Dev; import com.ruoyi.common.annotation.Dev;
import com.ruoyi.common.utils.DynConfigUtils;
import com.ruoyi.demo.dynconfig.TestDynConfig; import com.ruoyi.demo.dynconfig.TestDynConfig;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -21,14 +22,16 @@ public class DynConfigApi {
@GetMapping @GetMapping
public Object get(){ public Object get(){
// return config; //java中可以直接使用但是不能JSON序列号因为他是经过aop代理后的bean // return config; //java中可以直接使用但是不能JSON序列号因为他是经过aop代理后的bean
return BeanUtil.toBean(config, TestDynConfig.class); // return BeanUtil.toBean(config, TestDynConfig.class);
return DynConfigUtils.toBean(config);
} }
@PostMapping @PostMapping
public void set(@RequestBody TestDynConfig config){ public void set(@RequestBody TestDynConfig config){
// this.config=config;//不能直接赋值 // this.config=config;//不能直接赋值
BeanUtil.copyProperties(config,this.config, CopyOptions.create(TestDynConfig.class,true));//使用属性copy的方法或setter(getter)完成 // BeanUtil.copyProperties(config,this.config, CopyOptions.create(TestDynConfig.class,true));//使用属性copy的方法或setter(getter)完成
DynConfigUtils.setBean(config,this.config);
this.config.save();//主动异步保存PS:属性的方法不会触发保存需要主动异步保存执行set*,put*,add*,remove*方法后会自动异步保存 this.config.save();//主动异步保存PS:属性的方法不会触发保存需要主动异步保存执行set*,put*,add*,remove*方法后会自动异步保存
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="refresh" content="0; URL=/admin/">
</head>
<body>
</body>
</html>
Loading…
Cancel
Save