一、数据库部分
1.创建bean对象
CREATE OR REPLACE TYPE "QUARTZJOBBEAN" as object
(
-- Author : Duwc
-- Purpose : for QuartzJobBean
job_name varchar2(200),
job_group varchar2(200),
job_class_name varchar2(250),
trigger_name varchar2(200),
trigger_group varchar2(200),
trigger_state varchar2(16),
trigger_type varchar2(8),
t1 varchar2(200),
t2 varchar2(200),
t3 varchar2(200),
is_durable varchar2(1),
is_volatile varchar2(1),
is_stateful varchar2(1),
requests_recovery varchar2(1),
priority number(13),
start_time number(13),
end_time number(13),
calendar_name varchar2(200),
misfire_instr number(2)
)
2.创建array对象
CREATE OR REPLACE TYPE "QUARTZJOBARRAY" is table of QUARTZJOBBEAN
3.存储过程PACKAGE部分
CREATE OR REPLACE PACKAGE PKG_MODULES_DM_QUARTZ AS /*插入定时任务表*/
PROCEDURE INSERT_QUARTZJOB(v_bean QUARTZJOBBEAN); /*暂停定时任务表*/
PROCEDURE PAUSE_QUARTZJOB(v_array QUARTZJOBARRAY);
END;
4.存储过程BODY部分
CREATE OR REPLACE PACKAGE BODY PKG_MODULES_DM_QUARTZ AS
/*插入定时任务表*/
PROCEDURE INSERT_QUARTZJOB(v_bean QUARTZJOBBEAN) IS
BEGIN
insert into QRTZ_JOB_DETAILS
(JOB_NAME,
JOB_GROUP,
DESCRIPTION,
JOB_CLASS_NAME,
IS_DURABLE,
IS_VOLATILE,
IS_STATEFUL,
REQUESTS_RECOVERY)
values
(v_bean.job_name,
v_bean.job_group,
v_bean.job_name,
v_bean.job_class_name,
v_bean.is_durable,
v_bean.is_volatile,
v_bean.is_stateful,
v_bean.requests_recovery); insert into QRTZ_TRIGGERS
(TRIGGER_NAME,
TRIGGER_GROUP,
JOB_NAME,
JOB_GROUP,
IS_VOLATILE,
PRIORITY,
TRIGGER_STATE,
TRIGGER_TYPE,
START_TIME,
END_TIME,
CALENDAR_NAME,
MISFIRE_INSTR)
values
(v_bean.trigger_name,
v_bean.trigger_group,
v_bean.job_name,
v_bean.job_group,
v_bean.is_volatile,
v_bean.priority,
v_bean.trigger_state,
v_bean.trigger_type,
v_bean.start_time,
v_bean.end_time,
v_bean.calendar_name,
v_bean.misfire_instr); if v_bean.trigger_type = 'CRON' then
insert into QRTZ_CRON_TRIGGERS
(TRIGGER_NAME, TRIGGER_GROUP, CRON_EXPRESSION, TIME_ZONE_ID)
values
(v_bean.trigger_name,
v_bean.trigger_group,
v_bean.t1,
'Asia/Shanghai');
elsif v_bean.trigger_type = 'SIMPLE' then
insert into QRTZ_SIMPLE_TRIGGERS
(TRIGGER_NAME,
TRIGGER_GROUP,
REPEAT_COUNT,
REPEAT_INTERVAL,
TIMES_TRIGGERED)
values
(v_bean.trigger_name,
v_bean.trigger_group,
to_number(v_bean.t2),
to_number(v_bean.t3),
0);
end if;
commit;
END; /*暂停定时任务表*/
PROCEDURE PAUSE_QUARTZJOB(v_array QUARTZJOBARRAY) IS
v_bean QUARTZJOBBEAN;
BEGIN
for i in v_array.first .. v_array.last loop
v_bean := v_array(i);
update QRTZ_TRIGGERS
set TRIGGER_STATE = 'PAUSED'
where trigger_name = v_bean.trigger_name
and trigger_group = v_bean.trigger_group;
commit;
end loop;
END; END;
二、dao部分
1.创建bean对象
package com.ecnt.gnop.modules.dm.quartz.bean; public class QuartzJobBean { private String job_name; private String job_group; private String job_class_name; private String trigger_name; private String trigger_group; private String trigger_state; private String trigger_type; private String t1; private String t2; private String t3; private String is_durable; private String is_volatile; private String is_stateful; private String requests_recovery; private int priority; private int start_time; private int end_time; private String calendar_name; private String misfire_instr; public Object[] toArray() {
Object[] obj = new Object[19];
obj[0] = job_name;
obj[1] = job_group;
obj[2] = job_class_name;
obj[3] = trigger_name;
obj[4] = trigger_group;
obj[5] = trigger_state;
obj[6] = trigger_type;
obj[7] = t1;
obj[8] = t2;
obj[9] = t3;
obj[10] = is_durable;
obj[11] = is_volatile;
obj[12] = is_stateful;
obj[13] = requests_recovery;
obj[14] = priority;
obj[15] = start_time;
obj[16] = end_time;
obj[17] = calendar_name;
obj[18] = misfire_instr;
return obj;
} public String getCalendar_name() {
return calendar_name;
} public void setCalendar_name(String calendar_name) {
this.calendar_name = calendar_name;
} public int getEnd_time() {
return end_time;
} public void setEnd_time(int end_time) {
this.end_time = end_time;
} public String getIs_durable() {
return is_durable;
} public void setIs_durable(String is_durable) {
this.is_durable = is_durable;
} public String getIs_stateful() {
return is_stateful;
} public void setIs_stateful(String is_stateful) {
this.is_stateful = is_stateful;
} public String getIs_volatile() {
return is_volatile;
} public void setIs_volatile(String is_volatile) {
this.is_volatile = is_volatile;
} public String getMisfire_instr() {
return misfire_instr;
} public void setMisfire_instr(String misfire_instr) {
this.misfire_instr = misfire_instr;
} public int getPriority() {
return priority;
} public void setPriority(int priority) {
this.priority = priority;
} public String getRequests_recovery() {
return requests_recovery;
} public void setRequests_recovery(String requests_recovery) {
this.requests_recovery = requests_recovery;
} public int getStart_time() {
return start_time;
} public void setStart_time(int start_time) {
this.start_time = start_time;
} public String getJob_class_name() {
return job_class_name;
} public void setJob_class_name(String job_class_name) {
this.job_class_name = job_class_name;
} public String getJob_group() {
return job_group;
} public void setJob_group(String job_group) {
this.job_group = job_group;
} public String getJob_name() {
return job_name;
} public void setJob_name(String job_name) {
this.job_name = job_name;
} public String getT1() {
return t1;
} public void setT1(String t1) {
this.t1 = t1;
} public String getT2() {
return t2;
} public void setT2(String t2) {
this.t2 = t2;
} public String getT3() {
return t3;
} public void setT3(String t3) {
this.t3 = t3;
} public String getTrigger_group() {
return trigger_group;
} public void setTrigger_group(String trigger_group) {
this.trigger_group = trigger_group;
} public String getTrigger_name() {
return trigger_name;
} public void setTrigger_name(String trigger_name) {
this.trigger_name = trigger_name;
} public String getTrigger_state() {
return trigger_state;
} public void setTrigger_state(String trigger_state) {
this.trigger_state = trigger_state;
} public String getTrigger_type() {
return trigger_type;
} public void setTrigger_type(String trigger_type) {
this.trigger_type = trigger_type;
}
}
2.Dao
package com.ecnt.gnop.modules.dm.quartz.dao; import java.sql.SQLException;
import java.util.List; import com.ecnt.gnop.modules.dm.quartz.bean.QuartzJobBean; public interface QuartzJobDao { public void insertQuartzJob(QuartzJobBean bean) throws SQLException; public void pauseQuartzJob(List<QuartzJobBean> list) throws SQLException;
}
3.DaoImplements
package com.ecnt.gnop.modules.dm.quartz.dao.impl; import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor; import org.apache.commons.dbcp.DelegatingConnection;
import org.apache.log4j.Logger; import com.ecnt.gnop.modules.dm.quartz.bean.QuartzJobBean;
import com.ecnt.gnop.modules.dm.quartz.dao.QuartzJobDao; public class QuartzJobDaoImpl implements QuartzJobDao { private Logger log = Logger.getLogger(this.getClass().getName()); private static Connection getConn() {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.97.201:1521:fznop";
String username = "bi_swxt";
String password = "swxt2013";
Connection conn = null;
try {
Class.forName(driver);
// new oracle.jdbc.driver.OracleDriver();
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} return conn;
} /**
* TOMCAT dbcp Connection --> Oracle Connection
*
* @param con
* @return
* @throws SQLException
*/
public static Connection getNativeConnection(Connection con) throws SQLException {
if (con instanceof DelegatingConnection) {
Connection nativeCon = ((DelegatingConnection) con).getInnermostDelegate();
return (nativeCon != null ? nativeCon : con.getMetaData().getConnection());
}
return con;
} public void insertQuartzJob(QuartzJobBean bean) throws SQLException {
Connection conn = null;
Connection oracleConn = null;
CallableStatement stmt = null;
String sql = "{ CALL PKG_MODULES_DM_QUARTZ.INSERT_QUARTZJOB(?) }";
try {
conn = getConn();
oracleConn = getNativeConnection(conn);
stmt = oracleConn.prepareCall(sql);
StructDescriptor structDescriptor = StructDescriptor.createDescriptor("QUARTZJOBBEAN", oracleConn);
Object[] objects = bean.toArray();
STRUCT struct = new STRUCT(structDescriptor, oracleConn, objects);
stmt.setObject(1, struct);
stmt.execute();
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw e;
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} public void pauseQuartzJob(List<QuartzJobBean> list) throws SQLException {
Connection conn = null;
Connection oracleConn = null;
CallableStatement stmt = null;
String sql = "{ CALL PKG_MODULES_DM_QUARTZ.PAUSE_QUARTZJOB(?) }";
try {
conn = getConn();
oracleConn = getNativeConnection(conn);
stmt = oracleConn.prepareCall(sql);
StructDescriptor structDescriptor = StructDescriptor.createDescriptor("QUARTZJOBBEAN", oracleConn);
ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("QUARTZJOBARRAY", oracleConn);
ArrayList<STRUCT> structList = new ArrayList<STRUCT>();
for (QuartzJobBean bean : list) {
STRUCT struct = new STRUCT(structDescriptor, oracleConn, bean.toArray());
structList.add(struct);
}
ARRAY array = new ARRAY(arrayDescriptor, oracleConn, structList.toArray());
stmt.setArray(1, array);
stmt.execute();
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw e;
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}