Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Spring在x天后运行一次任务_Java_Spring - Fatal编程技术网

Java 使用Spring在x天后运行一次任务

Java 使用Spring在x天后运行一次任务,java,spring,Java,Spring,我正在开发一个应用程序,其中有优惠和还盘。当我创建报价时,我想安排一个任务在5天后运行,以检查它是否有任何还价。如果没有任何还价,则报价状态将设置为过期 我已经阅读了@Scheduled注释,但它只会每x天运行一次任务,但我只想在创建一个报价后运行一次任务,并且只为每个新报价运行一次。我怎样才能做到这一点?谢谢我没有给你一个直接的答案,你可以使用Spring任务调度器 Spring3.0为调度任务引入了TaskScheduler。它是Spring核心的一部分,不需要声明额外的依赖项 像下面的Sp

我正在开发一个应用程序,其中有
优惠
还盘
。当我创建报价时,我想安排一个任务在5天后运行,以检查它是否有任何还价。如果没有任何还价,则报价状态将设置为过期


我已经阅读了
@Scheduled
注释,但它只会每x天运行一次任务,但我只想在创建一个报价后运行一次任务,并且只为每个新报价运行一次。我怎样才能做到这一点?谢谢

我没有给你一个直接的答案,你可以使用Spring任务调度器

Spring3.0为调度任务引入了TaskScheduler。它是Spring核心的一部分,不需要声明额外的依赖项

像下面的Spring配置一样创建cron作业

<task:scheduled-tasks>
   <task:scheduled ref="offerScheduler" method="processOffer" cron="0 1 * * * *" />
</task:scheduled-tasks>
更多阅读:


嗨,我迟到了,不是spring专家,但我有一个解决方案,使用注释创建计划组件更容易

class OfferClass {
    public Date creationDate;
    // .... user related info
}

@EnableScheduling
@Component
public class OfferManager {
    private static final long interval_milliSeconds = 60*60*1000; // scheduled to run once every hour
    public List<OfferClass> offersList = new ArrayList<>();

    @Scheduled(fixedRate = interval_milliSeconds)
    public void timeout() {
        Date now = new Date(); // current timestamp
        for (OfferClass offer:this.offersList) {
            // check if 5 days are spent
            if (now.getTime() - offer.date.getTime() > 5 days) {
               // Do your action and remove the offer from the offersList
               this.offersList.remove(offer)
            }
        }
    }

}

// Usage
void main() {
    // Add an offer to the array
    offerClass newOffer = new offerClass();
    newOffer.creationDate = new Date() // set start date
    // Add offer to offer manager and wait for magic
    OfferManager.offersList.add(newOffer)
}
类OfferClass{
公开日期;
//..用户相关信息
}
@使能调度
@组成部分
公共类提供者管理器{
私有静态最终长间隔\u毫秒=60*60*1000;//计划每小时运行一次
public List offersList=new ArrayList();
@已计划(fixedRate=间隔\u毫秒)
公共无效超时(){
现在日期=新日期();//当前时间戳
for(OfferClass报价:此.OfferList){
//检查是否花费了5天
如果(now.getTime()-offer.date.getTime()>5天){
//执行您的操作并从报价人列表中删除报价
此.offerList.remove(要约)
}
}
}
}
//用法
void main(){
//向阵列中添加报价
offerClass newOffer=新offerClass();
newOffer.creationDate=新日期()//设置开始日期
//向报价经理添加报价,然后等待magic
OfferManager.OfferList.add(新报价)
}

我知道这不是最好的答案,但我是一个iOS开发人员,不是spring一个快速的改进将是深入研究“@Scheduled(fixedRate=…)”注释可能使用其他选项,如cron或fixeDelay,可以做得更好祝你好运

嗨,使用cron作业你可以定期运行任务,但你不能在x天后从不规则的时间点开始运行任务。你说的绝对正确,但我想说的是定期运行cron任务,并以这样的方式编写逻辑,它将以你想要的任何方式处理提供的内容。(我在上面的答案中编写了高级psuedo代码)是的,这是解决方案的近似答案,但这不是我想要的,因为如果使用cron作业,例如,任务可以在5天23小时后执行。我只想在创建
报价
后5天内执行一项任务。这可能是OP在这个主题上得到的最佳答案-对于每个创建的对象,在X个时间之后运行需要一些东西,比如为每个对象生成一个单独的线程,而这个答案中的逻辑可以扩展到任意数量的对象和时间范围。
class OfferClass {
    public Date creationDate;
    // .... user related info
}

@EnableScheduling
@Component
public class OfferManager {
    private static final long interval_milliSeconds = 60*60*1000; // scheduled to run once every hour
    public List<OfferClass> offersList = new ArrayList<>();

    @Scheduled(fixedRate = interval_milliSeconds)
    public void timeout() {
        Date now = new Date(); // current timestamp
        for (OfferClass offer:this.offersList) {
            // check if 5 days are spent
            if (now.getTime() - offer.date.getTime() > 5 days) {
               // Do your action and remove the offer from the offersList
               this.offersList.remove(offer)
            }
        }
    }

}

// Usage
void main() {
    // Add an offer to the array
    offerClass newOffer = new offerClass();
    newOffer.creationDate = new Date() // set start date
    // Add offer to offer manager and wait for magic
    OfferManager.offersList.add(newOffer)
}