web - 在java开发时常遇到定时的业务,大家说说自己的项目中都是怎么做的呀,不用很详细
迷茫
迷茫 2017-04-17 13:30:04
0
10
505

比如要做一个5点下线的团购,但是如果不用定时器做的话,5点后没人访问,就一直是开启状态,直到第一个人去触发下架动作,这期间要写数不清的烂代码,恳请前辈点拨

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(10)
阿神

quartz

Ty80

quartz is triggered regularly when it reaches the point,
Or use Lazy mode, wait for a front-end user to trigger the group purchase, and check whether it has expired in the business logic. The disadvantage is that the business coupling is high

巴扎黑

Two methods:
1. Use crontab to specify 5 o'clock to start the removal task.
2. Run a service to continuously poll the time, query the products to be removed from the shelves at the time, and perform the removal task.

小葫芦

Requiredquartz

大家讲道理

I don’t understand the business logic described, quartz is standard
If you are in a spring3.x environment, the simplest way:
Add the @Component annotation to the scheduled task class, and the @Scheduled(cron = "0/5 * * * * ?") annotation to the method that executes the task.
Let Spring scan this class: add <task:annotation-driven /> configuration to the spring configuration file,
The @Scheduled annotation is (org.springframework.scheduling.annotation.Scheduled)

黄舟

Using quartz is the most reliable.
But as the original poster said

No one visits after 5 o'clock, it will remain on until the first person triggers the removal action

In fact, it is quite reliable to do it this way. Every time you browse the group purchase page, you need to check whether the group purchase has expired.
Because you can't completely rely on scheduled tasks. Scheduled tasks only do one thing at a certain time, but doing things still takes time. So you still need to do verification when rendering the group purchase page

Peter_Zhu

Be a job server

迷茫

It is better to use the quartz framework in Java. If it is just a simple task, you can also directly use the scheduled task that comes with jdk

左手右手慢动作

org.springframework.scheduling.timer.ScheduledTimerTask

Use spring to configure a task plan and configure the class to be executed. In this way, you can only manage the logical content of the task plan.
Or if you need more advanced functions, you can use the Job/schedule related framework

But this function seems to be more common in Windows by using the Windows task schedule to call bat. Under bat, you can run a jar through the java command. The same system under Linux also provides task planning, just call the shell

洪涛

For more complex tasks, such as timing and fixing, use Quartz. If Spring is used in the project, Spring and Quartz can be perfectly combined.
Generally, for simple tasks that are performed at regular intervals, you can directly use the one that comes with jdk.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!