forked from geodmms/xdgnjobs

Dennis Kao
2014-01-15 94ae08701bbd7585a0b7e5a92d1975965a503c03
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.ximple.eofms;
 
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.util.Date;
 
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.awl.Wizard;
import org.quartz.DateBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerUtils;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
 
import com.ximple.eofms.jobs.OracleConvertDgn2ShpJob;
 
/**
 * Hello world!
 */
public class XQuartzJobCarrier {
    static Log logger = LogFactory.getLog(XQuartzJobCarrier.class);
    static Options options = new Options();
 
    private static final String VERSION = "1.3.1";
 
    public static void main(String[] args) {
        XQuartzJobCarrier instance = new XQuartzJobCarrier();
        instance.initializeOption();
        instance.execute(args);
    }
 
    private void initializeOption() {
        Option option;
        option = OptionBuilder.create("help");
        option.setDescription("print this message");
        options.addOption(option);
 
        option = OptionBuilder.create("version");
        option.setDescription("print the version information and exit");
        options.addOption(option);
 
        option = OptionBuilder.create("wizard");
        option.setDescription("start wizard mode");
        options.addOption(option);
 
        option = OptionBuilder.create("verbose");
        option.setDescription("be extra verbose");
        options.addOption(option);
 
        option = OptionBuilder.create("debug");
        option.setDescription("print debugging information");
        options.addOption(option);
 
        option = OptionBuilder.create("jobfile");
        option.setDescription("use given jobfile");
        option.setArgName("file");
        options.addOption(option);
 
    }
 
    private void execute(String[] args) {
        // CommandLineParser parser = new GnuParser();
        CommandLineParser parser = new PosixParser();
        CommandLine commandLine = null;
        try {
            // parse the command commandLine arguments
            commandLine = parser.parse(options, args);
        }
        catch (ParseException exp) {
            // oops, something went wrong
            System.err.println("Parsing failed.  Reason: " + exp.getMessage());
        }
 
        if ((commandLine != null) && (commandLine.getOptions().length > 0)) {
            if (commandLine.hasOption("help")) {
                printHelpMessage();
            } else if (commandLine.hasOption("version")) {
                printVersionMessage();
            } else if (commandLine.hasOption("wizard")) {
                startWizardMode();
            }
            return;
        }
        startScheduler();
    }
 
    private void printHelpMessage() {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("XQuartzJobCarrier", options);
    }
 
    private void printVersionMessage() {
        System.out.println("XQuartzJobCarrier - " + VERSION);
    }
 
    public void startWizardMode() {
        Runnable runnable = new Runnable() {
            public void run() {
                Wizard wizard = new XQuartzJobWizard((JFrame) null);
 
                wizard.pack();
                wizard.setVisibleOnCenterOfScreen();
            }
        };
        try {
            SwingUtilities.invokeLater(runnable);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
    public void startScheduler() {
        Scheduler scheduler = null;
        boolean shutdown = false;
 
        try {
            // Get a Scheduler instance from the Factory
            scheduler = StdSchedulerFactory.getDefaultScheduler();
 
            // Start the scheduler
            scheduler.start();
            logger.info("Scheduler started at " + new Date());
 
        } catch (SchedulerException ex) {
            // deal with any exceptions
            logger.error(ex, ex);
            shutdown = true;
        } catch (Throwable throwable) {
            logger.error(throwable.getMessage(), throwable);
            shutdown = true;
        }
        if (shutdown) {
            try {
                scheduler.shutdown();
            } catch (SchedulerException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
 
    /*
     * return an instance of the Scheduler from the factory
     */
    public Scheduler createScheduler() throws SchedulerException {
        return StdSchedulerFactory.getDefaultScheduler();
    }
 
    // Create and Schedule a ScanDirectoryJob with the Scheduler
    private void scheduleJob(Scheduler scheduler) throws SchedulerException {
 
        // Create a JobDetail for the Job
        /*
        JobDetailImpl jobDetail = new JobDetailImpl("ScanDirectory", Scheduler.DEFAULT_GROUP,
                                            OracleConvertDgn2ShpJob.class);
        */
        JobDetail jobDetail = JobBuilder.newJob(OracleConvertDgn2ShpJob.class)
            .withIdentity("ScanDirectory", Scheduler.DEFAULT_GROUP)
            .usingJobData("SCAN_DIR", "c:\\quartz-book\\input")
            .build();
        // Configure the directory to scan
        // jobDetail.getJobDataMap().put("SCAN_DIR", "c:\\quartz-book\\input");
 
        // Create a trigger that fires every 10 seconds, forever
        // Trigger trigger = TriggerUtils.makeSecondlyTrigger(10);
        // trigger.setName("scanTrigger");
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("scanTrigger")
            .startAt(DateBuilder.futureDate(10, DateBuilder.IntervalUnit.SECOND))
            .build();
        // Start the trigger firing from now
        // trigger.setStartTime(new Date());
 
        // Associate the trigger with the job in the scheduler
        scheduler.scheduleJob(jobDetail, trigger);
    }
 
}