001/*
002MIT License
003
004Copyright (c) 2020 FBSQL Team
005
006Permission is hereby granted, free of charge, to any person obtaining a copy
007of this software and associated documentation files (the "Software"), to deal
008in the Software without restriction, including without limitation the rights
009to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010copies of the Software, and to permit persons to whom the Software is
011furnished to do so, subject to the following conditions:
012
013The above copyright notice and this permission notice shall be included in all
014copies or substantial portions of the Software.
015
016THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
022SOFTWARE.
023
024Home:   https://fbsql.github.io
025E-Mail: fbsql.team@gmail.com
026*/
027
028package org.fbsql.servlet;
029
030import java.text.MessageFormat;
031import java.text.SimpleDateFormat;
032import java.util.Date;
033import java.util.Locale;
034import java.util.TimeZone;
035
036import org.fbsql.servlet.Logger.Severity;
037import org.quartz.Job;
038import org.quartz.JobDetail;
039import org.quartz.JobExecutionContext;
040import org.quartz.JobExecutionException;
041
042/**
043 * Represent a 'job' to be performed.
044 */
045public class RunnableJob implements Job {
046
047        /**
048         * Key used to store Runnable in JobDataMap
049         */
050        public static final String KEY_RUNNABLE = "KEY_RUNNABLE";
051
052        /**
053         * Called by the Scheduler when a Trigger fires that is associated with the Job.
054         */
055        @Override
056        public void execute(JobExecutionContext jobContext) throws JobExecutionException {
057                JobDetail jobDetail = jobContext.getJobDetail();
058                Logger.out(Severity.INFO, MessageFormat.format("Scheduler: Job started at: {0}. Next scheduled time: {1}", toIso(jobContext.getFireTime()), toIso(jobContext.getNextFireTime())));
059                Runnable runnable = (Runnable) jobDetail.getJobDataMap().get(KEY_RUNNABLE);
060                runnable.run();
061        }
062
063        /**
064         * Convert Date to ISO-8601 String
065         *
066         * @param date
067         * @return
068         */
069        private static String toIso(Date date) {
070                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US);
071                sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
072                return sdf.format(date);
073        }
074}
075
076/*
077Please contact FBSQL Team by E-Mail fbsql.team@gmail.com
078or visit https://fbsql.github.io if you need additional
079information or have any questions.
080*/
081
082/* EOF */