[英]Execute java code at specific time
I'm developing a soccer manager code that handles a game between friends where people need to compile their team and when real matches begin who has the players that play better wons the game. 我正在开发一个足球经理代码,该代码处理人们之间需要人们组建球队的朋友之间的比赛,并且在真正的比赛开始时谁的球员表现更好就赢得了比赛。
Obviously, once the game has started, the players can't change their team anymore. 显然,一旦游戏开始,玩家就无法再更换团队。 On the database every match has an attribute "match is opened" that determines if the players still can change their team.
在数据库中,每场比赛都有一个“比赛已打开”属性,该属性确定玩家是否仍然可以更改其球队。 Once the values is set false players can't edit their team anymore.
一旦设置了值,则错误的玩家将无法再编辑其团队。
So basically I have a datetime and when that date occurs the value on the database has to be set to false. 所以基本上我有一个日期时间,当该日期发生时,数据库上的值必须设置为false。
I thought many alternatives I'm going to explain you: 我想我会向您解释许多替代方法:
The last one is the solution I'd prefer to develop, I know there are libs that allow to work with scheduling but I really don't know if stuff like that is possible and how would you develop this stuff. 最后一个是我更愿意开发的解决方案,我知道有一些库可以用于调度,但是我真的不知道这样的事情是否可行以及您将如何开发这些东西。
Any hint will be appreciated. 任何提示将不胜感激。
Maybe the Quartz Framework might be interesting to you. 也许Quartz Framework对您来说可能很有趣。 It has a Job interface, which can be scheduled like Cron tasks.
它具有一个Job界面,可以像Cron任务一样进行调度。
Here is an example we use: 这是我们使用的示例:
@Configuration
@EnableScheduling
public class SchedulerConfiguration {
@Scheduled(cron = "15 * * * * ") //every 15 minutes
public void scheduledTask() {
doSomethingInYourDatabase();
}
}
i would recommend whether to use spring scheduler (if you are using spring) or a simple java executor service to run aa method with lets say 5 minutes delay after the previous execution. 我会建议是使用spring调度程序(如果您使用spring)还是简单的Java executor服务来运行一个方法,比如说在上一次执行后延迟了5分钟。 have a look here http://tutorials.jenkov.com/java-util-concurrent/executorservice.html
在这里看看http://tutorials.jenkov.com/java-util-concurrent/executorservice.html
In fact you will just need to run a simple update query like 实际上,您只需要运行一个简单的更新查询,例如
update match set match_open = false where start_time = (Sysdate-X);
May be you are using spring. 可能是您正在使用Spring。 you can add a Service class (@Service) and use @Scheduled annotated method which will help you execute a task at defined interval.
您可以添加Service类(@Service)并使用@Scheduled注释方法,该方法将帮助您按定义的时间间隔执行任务。 Check details spring schedulling
检查详情弹簧排程
@Scheduled(cron="*/5 * * * * MON-FRI")
public void doSomething() {
//something that should execute on weekdays only
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.