?? stdjdbcdelegate.java
字號:
/*
* Copyright 2004-2005 OpenSymphony
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
/*
* Previously Copyright (c) 2001-2004 James House
*/
package org.quartz.impl.jdbcjobstore;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Set;
import java.util.Properties;
import java.util.TimeZone;
import org.apache.commons.logging.Log;
import org.quartz.Calendar;
import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.spi.ClassLoadHelper;
import org.quartz.utils.Key;
import org.quartz.utils.TriggerStatus;
/**
* <p>
* This is meant to be an abstract base class for most, if not all, <code>{@link org.quartz.impl.jdbcjobstore.DriverDelegate}</code>
* implementations. Subclasses should override only those methods that need
* special handling for the DBMS driver in question.
* </p>
*
* @author <a href="mailto:jeff@binaryfeed.org">Jeffrey Wescott</a>
* @author James House
*/
public class StdJDBCDelegate implements DriverDelegate, StdJDBCConstants {
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Data members.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected Log logger = null;
protected String tablePrefix = DEFAULT_TABLE_PREFIX;
protected String instanceId;
protected boolean useProperties;
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Constructors.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/**
* <p>
* Create new StdJDBCDelegate instance.
* </p>
*
* @param logger
* the logger to use during execution
* @param tablePrefix
* the prefix of all table names
*/
public StdJDBCDelegate(Log logger, String tablePrefix, String instanceId) {
this.logger = logger;
this.tablePrefix = tablePrefix;
this.instanceId = instanceId;
}
/**
* <p>
* Create new StdJDBCDelegate instance.
* </p>
*
* @param logger
* the logger to use during execution
* @param tablePrefix
* the prefix of all table names
*/
public StdJDBCDelegate(Log logger, String tablePrefix, String instanceId,
Boolean useProperties) {
this.logger = logger;
this.tablePrefix = tablePrefix;
this.instanceId = instanceId;
this.useProperties = useProperties.booleanValue();
}
/*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Interface.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
protected boolean canUseProperties() {
return useProperties;
}
//---------------------------------------------------------------------------
// startup / recovery
//---------------------------------------------------------------------------
/**
* <p>
* Insert the job detail record.
* </p>
*
* @param conn
* the DB Connection
* @param newState
* the new state for the triggers
* @param oldState1
* the first old state to update
* @param oldState2
* the second old state to update
* @return number of rows updated
*/
public int updateTriggerStatesFromOtherStates(Connection conn,
String newState, String oldState1, String oldState2)
throws SQLException {
PreparedStatement ps = null;
try {
ps = conn
.prepareStatement(rtp(UPDATE_TRIGGER_STATES_FROM_OTHER_STATES));
ps.setString(1, newState);
ps.setString(2, oldState1);
ps.setString(3, oldState2);
return ps.executeUpdate();
} finally {
if (null != ps) {
try {
ps.close();
} catch (SQLException ignore) {
}
}
}
}
/**
* <p>
* Get the names of all of the triggers that have misfired.
* </p>
*
* @param conn
* the DB Connection
* @return an array of <code>{@link
* org.quartz.utils.Key}</code> objects
*/
public Key[] selectMisfiredTriggers(Connection conn, long ts)
throws SQLException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(rtp(SELECT_MISFIRED_TRIGGERS));
ps.setBigDecimal(1, new BigDecimal(String.valueOf(ts)));
rs = ps.executeQuery();
ArrayList list = new ArrayList();
while (rs.next()) {
String triggerName = rs.getString(COL_TRIGGER_NAME);
String groupName = rs.getString(COL_TRIGGER_GROUP);
list.add(new Key(triggerName, groupName));
}
Object[] oArr = list.toArray();
Key[] kArr = new Key[oArr.length];
System.arraycopy(oArr, 0, kArr, 0, oArr.length);
return kArr;
} finally {
if (null != rs) {
try {
rs.close();
} catch (SQLException ignore) {
}
}
if (null != ps) {
try {
ps.close();
} catch (SQLException ignore) {
}
}
}
}
/**
* <p>
* Select all of the triggers in a given state.
* </p>
*
* @param conn
* the DB Connection
* @param state
* the state the triggers must be in
* @return an array of trigger <code>Key</code> s
*/
public Key[] selectTriggersInState(Connection conn, String state)
throws SQLException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(rtp(SELECT_TRIGGERS_IN_STATE));
ps.setString(1, state);
rs = ps.executeQuery();
ArrayList list = new ArrayList();
while (rs.next()) {
list.add(new Key(rs.getString(1), rs.getString(2)));
}
Key[] sArr = (Key[]) list.toArray(new Key[list.size()]);
return sArr;
} finally {
if (null != rs) {
try {
rs.close();
} catch (SQLException ignore) {
}
}
if (null != ps) {
try {
ps.close();
} catch (SQLException ignore) {
}
}
}
}
public Key[] selectMisfiredTriggersInState(Connection conn, String state,
long ts) throws SQLException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(rtp(SELECT_MISFIRED_TRIGGERS_IN_STATE));
ps.setBigDecimal(1, new BigDecimal(String.valueOf(ts)));
ps.setString(2, state);
rs = ps.executeQuery();
ArrayList list = new ArrayList();
while (rs.next()) {
String triggerName = rs.getString(COL_TRIGGER_NAME);
String groupName = rs.getString(COL_TRIGGER_GROUP);
list.add(new Key(triggerName, groupName));
}
Object[] oArr = list.toArray();
Key[] kArr = new Key[oArr.length];
System.arraycopy(oArr, 0, kArr, 0, oArr.length);
return kArr;
} finally {
if (null != rs) {
try {
rs.close();
} catch (SQLException ignore) {
}
}
if (null != ps) {
try {
ps.close();
} catch (SQLException ignore) {
}
}
}
}
/**
* <p>
* Get the names of all of the triggers in the given group and state that
* have misfired.
* </p>
*
* @param conn
* the DB Connection
* @return an array of <code>{@link
* org.quartz.utils.Key}</code> objects
*/
public Key[] selectMisfiredTriggersInGroupInState(Connection conn,
String groupName, String state, long ts) throws SQLException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn
.prepareStatement(rtp(SELECT_MISFIRED_TRIGGERS_IN_GROUP_IN_STATE));
ps.setBigDecimal(1, new BigDecimal(String.valueOf(ts)));
ps.setString(2, groupName);
ps.setString(3, state);
rs = ps.executeQuery();
ArrayList list = new ArrayList();
while (rs.next()) {
String triggerName = rs.getString(COL_TRIGGER_NAME);
list.add(new Key(triggerName, groupName));
}
Object[] oArr = list.toArray();
Key[] kArr = new Key[oArr.length];
System.arraycopy(oArr, 0, kArr, 0, oArr.length);
return kArr;
} finally {
if (null != rs) {
try {
rs.close();
} catch (SQLException ignore) {
}
}
if (null != ps) {
try {
ps.close();
} catch (SQLException ignore) {
}
}
}
}
/**
* <p>
* Select all of the triggers for jobs that are requesting recovery. The
* returned trigger objects will have unique "recoverXXX" trigger names and
* will be in the <code>{@link
* org.quartz.Scheduler}.DEFAULT_RECOVERY_GROUP</code>
* trigger group.
* </p>
*
* <p>
* In order to preserve the ordering of the triggers, the fire time will be
* set from the <code>COL_FIRED_TIME</code> column in the <code>TABLE_FIRED_TRIGGERS</code>
* table. The caller is responsible for calling <code>computeFirstFireTime</code>
* on each returned trigger. It is also up to the caller to insert the
* returned triggers to ensure that they are fired.
* </p>
*
* @param conn
* the DB Connection
* @return an array of <code>{@link org.quartz.Trigger}</code> objects
*/
public Trigger[] selectTriggersForRecoveringJobs(Connection conn)
throws SQLException, IOException, ClassNotFoundException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn
.prepareStatement(rtp(SELECT_INSTANCES_RECOVERABLE_FIRED_TRIGGERS));
ps.setString(1, instanceId);
ps.setBoolean(2, true);
rs = ps.executeQuery();
long dumId = System.currentTimeMillis();
ArrayList list = new ArrayList();
while (rs.next()) {
String jobName = rs.getString(COL_JOB_NAME);
String jobGroup = rs.getString(COL_JOB_GROUP);
String trigName = rs.getString(COL_TRIGGER_NAME);
String trigGroup = rs.getString(COL_TRIGGER_GROUP);
long firedTime = rs.getLong(COL_FIRED_TIME);
SimpleTrigger rcvryTrig = new SimpleTrigger("recover_"
+ instanceId + "_" + String.valueOf(dumId++),
Scheduler.DEFAULT_RECOVERY_GROUP, new Date(firedTime));
rcvryTrig.setJobName(jobName);
rcvryTrig.setJobGroup(jobGroup);
rcvryTrig
.setMisfireInstruction(SimpleTrigger.MISFIRE_INSTRUCTION_FIRE_NOW);
JobDataMap jd = selectTriggerJobDataMap(conn, trigName, trigGroup);
jd.put("QRTZ_FAILED_JOB_ORIG_TRIGGER_NAME", trigName);
jd.put("QRTZ_FAILED_JOB_ORIG_TRIGGER_GROUP", trigGroup);
jd.put("QRTZ_FAILED_JOB_ORIG_TRIGGER_FIRETIME_IN_MILLISECONDS_AS_STRING", String.valueOf(firedTime));
rcvryTrig.setJobDataMap(jd);
list.add(rcvryTrig);
}
Object[] oArr = list.toArray();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -