use of com.twinsoft.convertigo.beans.scheduler.AbstractJob in project convertigo by convertigo.
the class CreateScheduledElements method getServiceResult.
protected void getServiceResult(HttpServletRequest request, Document document) throws Exception {
SchedulerManager schedulerManager = Engine.theApp.schedulerManager;
SchedulerXML schedulerXML = schedulerManager.getSchedulerXML();
boolean edit = (request.getParameter("edit") != null);
boolean del = (request.getParameter("del") != null);
Type type = null;
try {
type = Type.valueOf(request.getParameter("type"));
} catch (Exception e) {
throw new ServiceException("invalide \"type\" parameter");
}
Element rootElement = document.getDocumentElement();
AbstractBase ab = null;
Exception eProb = null;
try {
if (edit || del) {
String exname = ServiceUtils.getRequiredParameter(request, "exname");
if (ScheduledJob.class.isAssignableFrom(type.c)) {
ab = schedulerXML.getScheduledJob(exname);
} else if (AbstractSchedule.class.isAssignableFrom(type.c)) {
ab = schedulerXML.getSchedule(exname);
} else if (AbstractJob.class.isAssignableFrom(type.c)) {
ab = schedulerXML.getJob(exname);
}
if (del) {
schedulerXML.delAbstractBase(ab);
ab = null;
}
} else {
ab = type.c.getConstructor().newInstance();
}
if (ab != null) {
String exName = ab.getName();
String name = ServiceUtils.getRequiredParameter(request, "name");
ab.setName(name);
ab.setDescription(ServiceUtils.getRequiredParameter(request, "description"));
ab.setEnable("true".equals(ServiceUtils.getRequiredParameter(request, "enabled")));
if (ScheduledJob.class.isAssignableFrom(type.c)) {
ScheduledJob sj = (ScheduledJob) ab;
sj.setJob(schedulerXML.getJob(ServiceUtils.getRequiredParameter(request, "jobName")));
sj.setSchedule(schedulerXML.getSchedule(ServiceUtils.getRequiredParameter(request, "scheduleName")));
} else if (AbstractSchedule.class.isAssignableFrom(type.c)) {
AbstractSchedule as = (AbstractSchedule) ab;
if (ScheduleCron.class.isAssignableFrom(type.c)) {
ScheduleCron sc = (ScheduleCron) as;
sc.setCron(ServiceUtils.getRequiredParameter(request, "cron"));
}
} else if (AbstractJob.class.isAssignableFrom(type.c)) {
AbstractJob aj = (AbstractJob) ab;
if (JobGroupJob.class.isAssignableFrom(type.c)) {
JobGroupJob jgj = (JobGroupJob) aj;
jgj.setParallelJob(Integer.parseInt(ServiceUtils.getRequiredParameter(request, "parallelJob")));
jgj.delAllJobs();
for (String jobname : request.getParameterValues("jobsname")) {
AbstractJob jobToAdd = schedulerXML.getJob(jobname);
if (jobToAdd != null) {
jgj.addJob(jobToAdd);
}
}
} else if (AbstractConvertigoJob.class.isAssignableFrom(type.c)) {
AbstractConvertigoJob acj = (AbstractConvertigoJob) aj;
acj.setContextName(ServiceUtils.getRequiredParameter(request, "context"));
acj.setProjectName(ServiceUtils.getRequiredParameter(request, "project"));
acj.setWriteOutput("true".equals(ServiceUtils.getRequiredParameter(request, "writeOutput")));
if (TransactionConvertigoJob.class.isAssignableFrom(type.c)) {
TransactionConvertigoJob tcj = (TransactionConvertigoJob) acj;
tcj.setConnectorName(ServiceUtils.getParameter(request, "connector", ""));
tcj.setTransactionName(ServiceUtils.getParameter(request, "transaction", ""));
} else if (SequenceConvertigoJob.class.isAssignableFrom(type.c)) {
SequenceConvertigoJob scj = (SequenceConvertigoJob) acj;
scj.setSequenceName(ServiceUtils.getParameter(request, "sequence", ""));
}
Map<String, String[]> parameters = new HashMap<String, String[]>();
Matcher prefix = prefixPattern.matcher("");
for (String pname : Collections.list(GenericUtils.<Enumeration<String>>cast(request.getParameterNames()))) {
prefix.reset(pname);
if (prefix.find()) {
String para_name = prefix.group(1);
String[] values = request.getParameterValues(pname);
// for (String value : values) {
// parameters.put(para_name, value);
parameters.put(para_name, values);
// }
}
if (pname.equals("parameters")) {
String value = request.getParameter(pname);
if (!value.equals("0")) {
parameters.put("__testcase", new String[] { value });
}
}
}
acj.setParameters(parameters);
}
}
List<String> problems = schedulerXML.checkProblems(ab);
if (exName.equals(name) && edit) {
problems.remove(SchedulerXML.prob_alreadyExist);
}
if (problems.size() > 0) {
for (String problem : problems) {
rootElement.appendChild(document.createElement("problem")).appendChild(document.createTextNode(problem));
}
throw (eProb = new Exception("problem!"));
} else {
if (!edit) {
schedulerXML.addAbstractBase(ab);
}
}
}
schedulerManager.save();
schedulerManager.refreshJobs();
} catch (Exception e) {
schedulerManager.load();
if (e != eProb) {
throw e;
}
}
}
use of com.twinsoft.convertigo.beans.scheduler.AbstractJob in project convertigo by convertigo.
the class List method getServiceResult.
protected void getServiceResult(HttpServletRequest request, Document document) throws Exception {
Category category = null;
try {
category = Category.valueOf(request.getParameter("category"));
} catch (Exception e) {
// just ignore parse exception
}
Element rootElement = document.getDocumentElement();
SchedulerManager schedulerManager = Engine.theApp.schedulerManager;
SchedulerXML schedulerXML = schedulerManager.getSchedulerXML();
if (category == null || category == Category.scheduledJobs) {
for (ScheduledJob sj : schedulerXML.getScheduledJobs()) {
Element element = prepareElement(document, Category.scheduledJobs, sj);
AbstractJob job = sj.getJob();
AbstractSchedule schedule = sj.getSchedule();
element.setAttribute("jobName", job == null ? "..." : job.getName());
element.setAttribute("scheduleName", schedule == null ? "..." : schedule.getName());
StringBuffer info = new StringBuffer();
if (schedulerManager.getRunningScheduledJobs().contains(sj)) {
info.append("[ currently running ] ");
}
if (sj.isAllEnabled()) {
info.append("[ ready to run ] ");
} else {
info.append("[ not ready to run ] ");
if (sj.isEnable()) {
info.append("caused by ");
if (job == null) {
info.append("[ job is not defined ]");
} else if (!job.isEnable()) {
info.append("[ job is disabled ]");
}
if (schedule == null) {
info.append("[ schedule is not defined ]");
} else if (!schedule.isEnable()) {
info.append("[ schedule is disabled ]");
}
}
}
element.setAttribute("info", info.toString());
rootElement.appendChild(element);
}
}
if (category == null || category == Category.jobs) {
for (AbstractJob aj : schedulerXML.getJobs()) {
Element element = prepareElement(document, Category.jobs, aj);
if (aj instanceof AbstractConvertigoJob) {
AbstractConvertigoJob acj = (AbstractConvertigoJob) aj;
element.setAttribute("writeOutput", Boolean.toString(acj.isWriteOutput()));
element.setAttribute("project", acj.getProjectName());
element.setAttribute("context", acj.getContextName());
element.setAttribute("info", "URL : " + acj.getConvertigoURL());
for (Map.Entry<String, String[]> entry : acj.getParameters().entrySet()) {
Element parameter = document.createElement("parameter");
parameter.setAttribute("name", entry.getKey());
for (String value : entry.getValue()) {
if (!value.isEmpty()) {
Element parameter_value = document.createElement("value");
parameter_value.setTextContent(value);
parameter.appendChild(parameter_value);
}
}
element.appendChild(parameter);
}
if (acj instanceof SequenceConvertigoJob) {
SequenceConvertigoJob scj = (SequenceConvertigoJob) acj;
element.setAttribute("sequence", scj.getSequenceName());
} else if (acj instanceof TransactionConvertigoJob) {
TransactionConvertigoJob tcj = (TransactionConvertigoJob) acj;
element.setAttribute("connector", ((TransactionConvertigoJob) acj).getConnectorName());
element.setAttribute("transaction", tcj.getTransactionName());
}
} else if (aj instanceof JobGroupJob) {
JobGroupJob jgj = (JobGroupJob) aj;
element.setAttribute("parallelJob", Integer.toString(jgj.getParallelJob()));
for (AbstractJob sub : jgj.getJobGroup()) {
Element job = document.createElement("job_group_member");
job.appendChild(document.createTextNode(sub.getName()));
element.appendChild(job);
}
element.setAttribute("info", (jgj.isSerial() ? "serial " : "parallel ") + new ArrayList<AbstractJob>(jgj.getJobGroup()).toString());
}
rootElement.appendChild(element);
}
}
if (category == null || category == Category.schedules) {
for (AbstractSchedule as : schedulerXML.getSchedules()) {
Element element = prepareElement(document, Category.schedules, as);
if (as instanceof ScheduleCron) {
ScheduleCron sc = (ScheduleCron) as;
element.setAttribute("cron", sc.getCron());
element.setAttribute("info", "CRON [" + sc.getCron() + "]");
}
if (as instanceof ScheduleRunNow) {
element.setAttribute("info", "RunNow");
}
rootElement.appendChild(element);
}
}
}
use of com.twinsoft.convertigo.beans.scheduler.AbstractJob in project convertigo by convertigo.
the class SchedulerJob method execute.
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDetail jd = context.getJobDetail();
ScheduledJob scheduledJob = (ScheduledJob) jd.getJobDataMap().get("scheduledJob");
SchedulerManager schedulerManager = (SchedulerManager) jd.getJobDataMap().get("schedulerManager");
AbstractJob job = scheduledJob.getJob();
if (job != null) {
if (schedulerManager.getRunningScheduledJobs().contains(scheduledJob)) {
Engine.logScheduler.warn("No start " + jd.getName() + " because another still running.");
} else {
jd.getJobDataMap().put("running", true);
executeJob(job, jd.getName());
}
}
}
use of com.twinsoft.convertigo.beans.scheduler.AbstractJob in project convertigo by convertigo.
the class SchedulerJob method executeJob.
public void executeJob(AbstractJob job, String jdName) {
if (job.isEnable()) {
long start = System.currentTimeMillis();
if (job instanceof AbstractConvertigoJob) {
AbstractConvertigoJob convertigoJob = (AbstractConvertigoJob) job;
HttpServletRequest request = null;
try {
Engine.logScheduler.info("Prepare job " + jdName + " for " + convertigoJob.getProjectName());
Map<String, String[]> parameters = convertigoJob.getConvertigoParameters();
InternalRequester requester = new InternalRequester(GenericUtils.<Map<String, Object>>cast(parameters));
HttpSessionListener.checkSession(request = requester.getHttpServletRequest());
Object response = requester.processRequest();
String message = "Completed job " + jdName + " with success";
if (convertigoJob.isWriteOutput()) {
if (response instanceof Document) {
response = XMLUtils.prettyPrintDOM((Document) response);
}
message += "\n" + response;
}
Engine.logScheduler.info(message);
} catch (Exception e) {
Engine.logScheduler.error("Failed job " + jdName, e);
} finally {
if (request != null) {
request.getSession(true).invalidate();
}
}
} else if (job instanceof JobGroupJob) {
JobGroupJob jobGroupJob = (JobGroupJob) job;
SortedSet<AbstractJob> jobs = jobGroupJob.getJobGroup();
Engine.logScheduler.info("Prepare job " + jdName + " for " + jobs.size() + " jobs. Serial ? " + jobGroupJob.isSerial());
int parallelJob = jobGroupJob.getParallelJob();
if (parallelJob <= 1) {
for (AbstractJob abstractJob : jobs) {
executeJob(abstractJob, jdName + "[" + abstractJob.getName() + "]");
}
} else {
int[] jobCount = { 0 };
Set<Thread> threads = new HashSet<>();
List<AbstractJob> list = new ArrayList<>(jobs);
while (!list.isEmpty()) {
synchronized (jobCount) {
if (jobCount[0] == parallelJob) {
try {
jobCount.wait();
} catch (InterruptedException e) {
}
}
jobCount[0]++;
AbstractJob abstractJob = list.remove(0);
final String subname = jdName + "[" + abstractJob.getName() + "]";
Thread thread = new Thread(() -> {
try {
executeJob(abstractJob, subname);
} finally {
synchronized (jobCount) {
jobCount[0]--;
jobCount.notify();
}
}
});
threads.add(thread);
thread.setDaemon(true);
thread.start();
}
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
Engine.logScheduler.error("Unexpected exception", e);
}
}
}
}
Engine.logScheduler.info("Completed job " + jdName + " in " + (System.currentTimeMillis() - start) + "ms");
} else {
Engine.logScheduler.info("Trying to start " + jdName + " failed because the job is disabled !");
}
}
Aggregations