-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpringSeedBatchJobRunner.java
180 lines (149 loc) · 6.59 KB
/
SpringSeedBatchJobRunner.java
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
package com.profullstack.springseed.core.batch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.JobLocator;
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.core.converter.JobParametersConverter;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.launch.support.ExitCodeMapper;
import org.springframework.batch.core.launch.support.JvmSystemExiter;
import org.springframework.batch.core.launch.support.SimpleJvmExitCodeMapper;
import org.springframework.batch.core.launch.support.SystemExiter;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* Created by christianxiao on 7/23/17.
* Usage: java -cp "batch/*" com.profullstack.springseed.core.batch.SpringSeedBatchJobRunner com.profullstack.springseed.sample.batch.configuration.BatchContextConfig sampleJob param1=value1
*/
public class SpringSeedBatchJobRunner {
protected static final Log logger = LogFactory.getLog(SpringSeedBatchJobRunner.class);
private ExitCodeMapper exitCodeMapper = new SimpleJvmExitCodeMapper();
private static SystemExiter systemExiter = new JvmSystemExiter();
private JobParametersConverter jobParametersConverter = new DefaultJobParametersConverter();
@Autowired
private JobLauncher launcher;
@Autowired
private JobLocator jobLocator;
@Autowired
private JobExplorer jobExplorer;
@Autowired
private JobRepository jobRepository;
/**
* Delegate to the exiter to (possibly) exit the VM gracefully.
*
* @param status
*/
public void exit(int status) {
systemExiter.exit(status);
}
int start(String jobPath, String jobIdentifier, String[] parameters, Set<String> opts) {
SpringBatchApplicationContext context = null;
try {
//Add initializer support here!
context = SpringBatchApplicationContext.instance(jobPath);
//end
/* try {
context = new AnnotationConfigApplicationContext(Class.forName(jobPath));
} catch (ClassNotFoundException cnfe) {
context = new ClassPathXmlApplicationContext(jobPath);
}*/
context.getAutowireCapableBeanFactory().autowireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
Assert.state(launcher != null, "A JobLauncher must be provided. Please add one to the configuration.");
if (opts.contains("-restart") || opts.contains("-next")) {
Assert.state(jobExplorer != null,
"A JobExplorer must be provided for a restart or start next operation. Please add one to the configuration.");
}
String jobName = jobIdentifier;
JobParameters jobParameters = jobParametersConverter.getJobParameters(StringUtils
.splitArrayElementsIntoProperties(parameters, "="));
Assert.isTrue(parameters == null || parameters.length == 0 || !jobParameters.isEmpty(),
"Invalid JobParameters " + Arrays.asList(parameters)
+ ". If parameters are provided they should be in the form name=value (no whitespace).");
Job job = null;
if (jobLocator != null) {
try {
job = jobLocator.getJob(jobName);
} catch (NoSuchJobException e) {
}
}
if (job == null) {
job = (Job) context.getBean(jobName);
}
JobExecution jobExecution = launcher.run(job, jobParameters);
return exitCodeMapper.intValue(jobExecution.getExitStatus().getExitCode());
}
catch (Throwable e) {
String message = "Job Terminated in error: " + e.getMessage();
logger.error(message, e);
return exitCodeMapper.intValue(ExitStatus.FAILED.getExitCode());
}
finally {
if (context != null) {
context.close();
}
}
}
public static void main(String[] args){
SpringSeedBatchJobRunner command = new SpringSeedBatchJobRunner();
List<String> newargs = new ArrayList<String>(Arrays.asList(args));
try {
if (System.in.available() > 0) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = " ";
while (line != null) {
if (!line.startsWith("#") && StringUtils.hasText(line)) {
if (logger.isDebugEnabled()) {
logger.debug("Stdin arg: " + line);
}
newargs.add(line);
}
line = reader.readLine();
}
}
}
catch (IOException e) {
logger.warn("Could not access stdin (maybe a platform limitation)");
if (logger.isDebugEnabled()) {
logger.debug("Exception details", e);
}
}
Set<String> opts = new HashSet<String>();
List<String> params = new ArrayList<String>();
int count = 0;
String jobPath = null;
String jobIdentifier = null;
for (String arg : newargs) {
switch (count) {
case 0:
jobPath = arg;
break;
case 1:
jobIdentifier = arg;
break;
default:
params.add(arg);
break;
}
count++;
}
if (jobPath == null || jobIdentifier == null) {
String message = "At least 2 arguments are required: JobPath/JobClass and jobIdentifier.";
logger.error(message);
command.exit(1);
return;
}
String[] parameters = params.toArray(new String[params.size()]);
int result = command.start(jobPath, jobIdentifier, parameters, opts);
command.exit(result);
}
}