Search in sources :

Example 56 with Scheduled

use of org.springframework.scheduling.annotation.Scheduled in project NabAlive by jcheype.

the class ApplicationScheduler method scheduled.

@Scheduled(cron = "0 * * * * *")
public void scheduled() {
    logger.debug("scheduled");
    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    int day = cal.get(Calendar.DAY_OF_WEEK);
    String scheduleKey = String.format("%02d:%02d-%d", hour, minute, day);
    logger.debug("scheduleKey: {}", scheduleKey);
    Query<Nabaztag> queryWakeUp = nabaztagDAO.createQuery().filter("wakeup", scheduleKey);
    sendToAll(queryWakeUp, new SleepPacket(SleepPacket.Action.WakeUp));
    Query<Nabaztag> querySleep = nabaztagDAO.createQuery().filter("sleep", scheduleKey);
    sendToAll(querySleep, new SleepPacket(SleepPacket.Action.Sleep));
}
Also used : Nabaztag(com.nabalive.data.core.model.Nabaztag) SleepPacket(com.nabalive.server.jabber.packet.SleepPacket) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 57 with Scheduled

use of org.springframework.scheduling.annotation.Scheduled in project new-cloud by xie-summer.

the class ScheduleTriggerServiceImpl method refreshTrigger.

/**
 * 定时刷新扫描配置表
 */
@Scheduled(fixedRate = 1000 * 30)
@Override
public void refreshTrigger() {
    try {
        List<ScheduleTrigger> jobList = scheduleTriggerMapper.selectAll();
        if (null != jobList && jobList.size() > 0) {
            for (ScheduleTrigger scheduleJob : jobList) {
                String status = scheduleJob.getStatus();
                TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
                CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
                // 说明本条任务还没有添加到quartz中
                if (null == trigger) {
                    if (STATUS.equals(status)) {
                        continue;
                    }
                    JobDetail jobDetail = null;
                    try {
                        // 创建JobDetail(数据库中job_name存的任务全路径,这里就可以动态的把任务注入到JobDetail中)
                        jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(scheduleJob.getJobName())).withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).build();
                        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCron());
                        trigger = TriggerBuilder.newTrigger().withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).withSchedule(scheduleBuilder).build();
                        scheduler.scheduleJob(jobDetail, trigger);
                    } catch (ClassNotFoundException e) {
                    }
                } else {
                    // 如果是禁用,从quartz中删除这条任务
                    if (STATUS.equals(status)) {
                        JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
                        scheduler.deleteJob(jobKey);
                        continue;
                    }
                    String searchCron = scheduleJob.getCron();
                    String currentCron = trigger.getCronExpression();
                    if (!searchCron.equals(currentCron)) {
                        // 表达式调度构建器
                        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(searchCron);
                        // 按新的cronExpression表达式重新构建trigger
                        trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
                        // 按新的trigger重新设置job执行
                        scheduler.rescheduleJob(triggerKey, trigger);
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error("定时任务每日刷新触发器任务异常,在ScheduleTriggerService的方法refreshTrigger中,异常信息:", e);
    }
}
Also used : ScheduleTrigger(com.monitor.model.task.ScheduleTrigger) TriggerKey(org.quartz.TriggerKey) CronTrigger(org.quartz.CronTrigger) JobDetail(org.quartz.JobDetail) CronScheduleBuilder(org.quartz.CronScheduleBuilder) JobKey(org.quartz.JobKey) Job(org.quartz.Job) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 58 with Scheduled

use of org.springframework.scheduling.annotation.Scheduled in project OpenClinica by OpenClinica.

the class JobTriggerService method hourlyJobTrigger.

// @Scheduled(cron = "0 0/2 * * * ?") // trigger every 2 minutes
// @Scheduled(cron = "0 0/1 * * * ?") // trigger every minute
// trigger every hour
@Scheduled(cron = "0 0 0/1 * * ?")
public void hourlyJobTrigger() throws NumberFormatException, ParseException {
    try {
        logger.info("Beginning scheduled rule run.  The time is now " + currentDateFormat.format(new Date()));
        triggerJob();
        logger.info("Completed scheduled rule run.");
    } catch (Exception e) {
        logger.error(e.getMessage());
        logger.error(ExceptionUtils.getStackTrace(e));
        throw e;
    }
}
Also used : Date(java.util.Date) ParseException(java.text.ParseException) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 59 with Scheduled

use of org.springframework.scheduling.annotation.Scheduled in project CzechIdMng by bcvsolutions.

the class DefaultAttachmentManager method purgeTemp.

/**
 * Purge old temporary files and attachments once per day.
 * Temporary files older than configured ttl will be purged.
 * Temporary uploaded attachments older than configured ttl will be purged.
 *
 * @return purged files count
 */
// once per day
@Scheduled(fixedDelay = 86400000)
public void purgeTemp() {
    int purgedFiles = 0;
    long ttl = attachmentConfiguration.getTempTtl();
    if (ttl == 0) {
        LOG.warn("Removing old temporary files is disabled. Configure property [{}] - time to live in milliseconds (greater than zero).", AttachmentConfiguration.PROPERTY_TEMP_TTL);
        return;
    }
    // 
    // purge older temporary files than purge time
    long purgeTime = System.currentTimeMillis() - ttl;
    File temp = new File(getTempPath());
    if (temp.isDirectory()) {
        File[] files = temp.listFiles();
        if (files != null) {
            for (File f : files) {
                try {
                    if (f.getName().endsWith("." + DEFAULT_TEMP_FILE_EXTENSION) && f.lastModified() < purgeTime) {
                        f.delete();
                        purgedFiles++;
                    }
                } catch (Exception ex) {
                    LOG.error("Removing old temporary [.{}] file [{}] failed", DEFAULT_TEMP_FILE_EXTENSION, f.getName(), ex);
                }
            }
        }
    }
    // 
    // purge temporary attachments
    IdmAttachmentFilter filter = new IdmAttachmentFilter();
    filter.setOwnerType(TEMPORARY_ATTACHMENT_OWNER_TYPE);
    filter.setCreatedBefore(ZonedDateTime.now().minus(ttl, ChronoField.MILLI_OF_DAY.getBaseUnit()));
    for (IdmAttachmentDto attachment : find(filter, null)) {
        delete(attachment);
        purgedFiles++;
    }
    // 
    LOG.debug("Temporary files were purged [{}]", purgedFiles);
}
Also used : IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) File(java.io.File) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) IdmAttachmentFilter(eu.bcvsolutions.idm.core.ecm.api.dto.filter.IdmAttachmentFilter) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 60 with Scheduled

use of org.springframework.scheduling.annotation.Scheduled in project anton-pavlovich-bot by wyvie.

the class MessageReader method readMessages.

@Scheduled(fixedDelay = 200)
public void readMessages() {
    GetUpdates getUpdates = new GetUpdates().limit(telegramProperties.getUpdateLimit()).offset(lastOffset).timeout(0);
    GetUpdatesResponse response = telegramBot.execute(getUpdates);
    List<Update> updates = response.updates();
    updates.forEach(update -> {
        lastOffset = update.updateId() + 1;
        Message message = update.message();
        if (message != null && message.from() != null && bannedUsers.contains(message.from().id())) {
            logger.debug("Message ignored from user " + message.from().id());
            persistUser(message.from());
            return;
        }
        if (message != null && message.text() != null) {
            logger.debug("Got message '" + message.text() + "' from chat_id " + message.chat().id());
            persistUser(message.from());
            if (validateCommmand(message))
                commandProcessor.processCommand(message);
            else {
                String messageText;
                messageText = message.text().trim();
                if (messageText.startsWith("+") || messageText.startsWith("-") || emojiHelper.isThumbsUp(messageText) || emojiHelper.isThumbsDown(messageText))
                    commandProcessor.processKarma(message);
            }
            lastOffset = update.updateId() + 1;
        }
        if (message != null && message.sticker() != null) {
            String emoji = message.sticker().emoji();
            if (emojiHelper.isThumbsDown(emoji) || emojiHelper.isThumbsUp(emoji)) {
                commandProcessor.processKarma(message);
            }
        }
    });
}
Also used : GetUpdates(com.pengrad.telegrambot.request.GetUpdates) Message(com.pengrad.telegrambot.model.Message) GetUpdatesResponse(com.pengrad.telegrambot.response.GetUpdatesResponse) Update(com.pengrad.telegrambot.model.Update) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Aggregations

Scheduled (org.springframework.scheduling.annotation.Scheduled)140 Date (java.util.Date)38 IOException (java.io.IOException)17 HashMap (java.util.HashMap)15 ArrayList (java.util.ArrayList)12 File (java.io.File)9 List (java.util.List)9 User (io.github.jhipster.sample.domain.User)8 SimpleDateFormat (java.text.SimpleDateFormat)8 LocalDate (java.time.LocalDate)8 Map (java.util.Map)8 Word (ai.elimu.model.content.Word)6 Instant (java.time.Instant)6 SysConfig (com.itrus.portal.db.SysConfig)5 InetAddress (java.net.InetAddress)5 UnknownHostException (java.net.UnknownHostException)5 Contributor (ai.elimu.model.Contributor)4 StoryBookParagraph (ai.elimu.model.content.StoryBookParagraph)4 Language (ai.elimu.model.v2.enums.Language)4 Nabaztag (com.nabalive.data.core.model.Nabaztag)4