📄 stdjdbcdelegate.java
字号:
/* * Copyright James House (c) 2001-2004 * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: 1. * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. 2. Redistributions in * binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */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 { 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); 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); list.add(rcvryTrig); } Object[] oArr = list.toArray(); Trigger[] tArr = new Trigger[oArr.length]; System.arraycopy(oArr, 0, tArr, 0, oArr.length); return tArr; } finally { if (null != rs) { try { rs.close(); } catch (SQLException ignore) { } } if (null != ps) { try { ps.close(); } catch (SQLException ignore) { } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -