简体   繁体   English

在特定时间执行Java代码

[英]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: 我想我会向您解释许多替代方法:

  • Obviously the most dirty and naive is to log to the server, open the dbms and query the db to set the value to false. 显然,最肮脏和天真的方法是登录到服务器,打开dbms并查询db以将值设置为false。 Very dirty solution. 非常脏的解决方案。
  • Make a batch script and put it in the scheduled tasks. 制作批处理脚本并将其放入计划的任务中。 This solution too is very dirty as every week I get to log on the server to edit the script with the new date. 这个解决方案也很脏,因为我每周都要登录服务器以使用新日期来编辑脚本。
  • On the webapp make some control like: "When one of the users tries to edit his team, if the time has passed set the "open" attribute to false for everyone." 在Web应用程序上进行一些控制,例如:“当其中一个用户试图编辑他的团队时,如果时间已经过去,则将每个人的“打开”属性设置为false。 This solutions seems to be dirty because until someone tries to change the formations and the system recognizes the problem, formations are de facto open. 该解决方案似乎是肮脏的,因为在有人尝试更改构型并且系统识别出问题之前,构架实际上是开放的。
  • Given a date, when the x hour arrives the code automatically launches a method that closes the teams. 给定一个日期,当x小时到达时,代码会自动启动关闭团队的方法。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM