cn.dceast.platform.task.quartz.data.InputFromFile Maven / Gradle / Ivy
The newest version!
package cn.dceast.platform.task.quartz.data;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.dceast.platform.task.config.Constants;
import cn.dceast.platform.task.quartz.JobEntity;
import cn.dceast.platform.task.util.StringUtil;
import com.alibaba.fastjson.JSON;
public class InputFromFile implements InputJobInfoExecutor{
private static String fileName="task.json";
private static Logger logger=LoggerFactory.getLogger(InputFromFile.class);
@Override
public List getJobs() {
InputStream inputStream = null;
InputStreamReader isr= null;
BufferedReader bufferedReader =null;
logger.info("Begin to read job config file...");
try {
inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); // properties.load(Prop.class.getResourceAsStream(fileName));
if (inputStream == null){
throw new IllegalArgumentException("Task.json file not found in classpath: " + fileName);
}
isr=new InputStreamReader(inputStream,Constants.SYS_ENCODING);
bufferedReader = new BufferedReader(isr);
String line=null;
StringBuffer sBuffer=new StringBuffer();
while((line=bufferedReader.readLine())!=null){
sBuffer.append(line);
}
String str = sBuffer.toString();
logger.info("Finish to read job config file!");
if(!StringUtil.isEmpty(str)){
List list=JSON.parseArray(str, JobEntity.class);
for(JobEntity job:list){
String jobId=UUID.randomUUID().toString().replaceAll("-", "");
job.setJobId(jobId);
}
return list;
}
} catch (Exception e) {
throw new RuntimeException("Error loading Task.json file.", e);
}
finally {
if (bufferedReader != null) try {bufferedReader.close();} catch (IOException e) {e.printStackTrace();}
if (isr != null) try {isr.close();} catch (IOException e) {e.printStackTrace();}
if (inputStream != null) try {inputStream.close();} catch (IOException e) {e.printStackTrace();}
}
return null;
}
}