Home  >  Q&A  >  body text

web - 在java开发时常遇到定时的业务,大家说说自己的项目中都是怎么做的呀,不用很详细

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

迷茫迷茫2764 days ago547

reply all(10)I'll reply

  • 阿神

    阿神2017-04-17 13:32:04

    quartz

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:32:04

    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

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:32:04

    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.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 13:32:04

    Requiredquartz

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 13:32:04

    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)

    reply
    0
  • 黄舟

    黄舟2017-04-17 13:32:04

    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

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:32:04

    Be a job server

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:32:04

    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

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 13:32:04

    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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:32:04

    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.

    reply
    0
  • Cancelreply