david-PC\david
2018-06-12 cc7f57619fd09f68582b748a3580402717b84c50
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
package frame.schedule;
 
 
import java.util.Date;
 
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
import frame.util.Util;
 
 
 
 
public class ScheduleJob implements Job{
    
    protected static Logger logger;
    
    private Object lock = new Object();
    private int tryTimes;
    private int tryCycles;
    private Date beginTime;
    private Date endTime;
    private boolean success;
    
    
    static {
        logger = Logger.getLogger(ScheduleJob.class);
    }
    
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDetail jobDetail = context.getJobDetail();
        JobDataMap dataMap = jobDetail.getJobDataMap();
        
        IJob job = (IJob)dataMap.get(IJob.Key_Job);
        
        synchronized (lock) {
            JobStatus status = job.getStatus();
 
            if (JobStatus.Idle == status) {
                String taskId = Util.newShortGUID();
                
                beforeExecute(job, taskId);
                try {
                    executeJob(job, taskId);
                }
                finally {
                    afterExecute(job, taskId);
                }
            }
        }
    }
 
    private void beforeExecute(IJob job, String taskId) {
        success = false;
        beginTime = new Date();
        tryTimes = 0;
        tryCycles = 0;    
                
        job.begin(taskId, beginTime);
    }
    private void afterExecute(IJob job, String taskId) {
        if (success) {
            job.succeed(taskId, tryTimes, tryCycles, endTime);
        }
        else {
            job.fail(taskId, tryTimes, tryCycles, endTime);
        }
    }
    
    private void executeJob(IJob job, String taskId) {
        int cnt = 0;
        String name = job.getName();
        
        for (int j = 0; j < IJob.Max_TryCycles; j++) {
            tryCycles++;
            
            for (int i = 0; i < IJob.Max_TryTimes; i++) {
                if (Schedule.Terminated) {
                    break;
                }
                
                tryTimes++;
                
                try {
                    cnt = cnt + 1;
                    logger.info(name + "尝试第" + cnt + "次运行...");
                    
                    job.execute();
                    
                    success = true;
                    break;
                }
                catch (Exception e) {
                }
            }
            
            if (success) {
                break;
            }
            
            try {
                int waitfor = 60 * IJob.Minutes_IntervalCycle;
                
                for (int w = 0; w < waitfor; w++) {
                    Thread.sleep(1000);
                    
                    if (Schedule.Terminated) {
                        break;
                    }
                }
            } catch (InterruptedException e) {
            }                
        }        
    }
}