
ff.prac.jobs.Worker Maven / Gradle / Ivy
/*
* Copyright (C) 2015-2018 XinZhenfeng
*
* This file is part of prac
*
* prac is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package ff.prac.jobs;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.DataSource;
import ff.prac.jobs.annotation.DoNotCount;
import ff.prac.logging.SrcLog;
import ff.prac.util.DateTime;
public final class Worker extends Thread {
private DataSource ds = null;
private int batch = -1;
private String jobuid = null;
Worker(DataSource ds, int batch, String juid) {
this.ds = ds;
this.batch = batch;
this.jobuid = juid;
}
public void run() {
DBA dba = new DBA(this.ds);
Connection con = null;
DateTime start = DateTime.now();
String clsname = null;
BgRuntime rt = new BgRuntime(dba);
rt.setBatch(this.batch);
rt.setJobUid(this.jobuid);
try {
con = dba.newConnection();
con.setAutoCommit(true);
PreparedStatement selpst = con.prepareStatement(
"select * from "+JobSchema.getEXE()+" where J_BAT=? and J_UID=?");
selpst.setInt(1, this.batch);
selpst.setString(2, this.jobuid);
ResultSet jobrs = selpst.executeQuery();
if(!jobrs.next()) {
return;
}
clsname = jobrs.getString("J_CLS");
rt.setParam(jobrs.getString("J_PAR"));
} catch (Throwable e) {
dba.close();
SrcLog.error(e);
return;
}
DoNotCount doNotCount = null;
String err = null;
try {
Class> cls = Class.forName(clsname);
BgJob job = (BgJob)(cls.newInstance());
doNotCount = cls.getAnnotation(DoNotCount.class);
boolean tran = (job instanceof TransactionalBgJob);
dba.setTransactional(tran);
job.execute(rt);
dba.commit();
cls = null;
job = null;
} catch (Throwable e) {
dba.rollback();
err = e.toString();
SrcLog.error(e);
} finally {
if(doNotCount==null) {
JobService.increaseBgJobCount();
}
}
DateTime end = DateTime.now();
int sta = err==null ? JobState.DONE.intValue() : JobState.ERROR.intValue();
try {
PreparedStatement endpst = con.prepareStatement(
"update "+JobSchema.getEXE()
+ " set J_STA=?, J_TIMEND=?, J_TIM=?, J_ERR=?"
+ " where J_BAT=? and J_UID=?");
int i = 1;
endpst.setInt(i++, sta);
endpst.setString(i++, end.toString());
endpst.setLong(i++, end.getTimeMillis() - start.getTimeMillis());
endpst.setString(i++, err);
endpst.setInt(i++, this.batch);
endpst.setString(i++, this.jobuid);
endpst.executeUpdate();
endpst.close();
endpst = null;
} catch (Throwable e) {
SrcLog.error(e);
} finally {
dba.close();
}
dba = null;
MainWorker.rlsWorker(this.batch, this.jobuid);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy