读书人

Schedule2.0.1 demo使用教程(二)

发布时间: 2012-09-19 13:43:53 作者: rapoo

Schedule2.0.1 demo应用教程(二)

?

public class TaskSingle implements IScheduleTaskDealSingle<Long> {protected static transient Log log = LogFactory.getLog(TaskSingle.class);protected DataSource dataSource;protected String taskType;protected String ownSign;public void setOwnSign(String ownSign) {this.ownSign = ownSign;}public void setTaskType(String taskType) {this.taskType = taskType;}public DataSource getDataSource() {return dataSource;}public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;}public boolean execute(Long task, String ownSign) throws Exception {Connection conn = null;Long id = (Long) task;try {conn = dataSource.getConnection();String sql = "update SCHEDULE_TEST SET STS ='Y' ,DEAL_COUNT = DEAL_COUNT + 1 WHERE ID = ? and STS ='N' ";PreparedStatement statement = conn.prepareStatement(sql);statement.setLong(1, id);statement.executeUpdate();statement.close();conn.commit();log.debug("处理任务:" + id + " 成功!");return true;} catch (Exception e) {log.error("执行任务:" + task + "失败:" + e.getMessage(), e);if (conn != null) {conn.rollback();}return false;} finally {if (conn != null) {conn.close();}}}public Comparator<Long> getComparator() {return new Comparator<Long>() {public int compare(Long o1, Long o2) {return o1.compareTo(o2);}};}public List<Long> selectTasks(String ownSign, int queueNum,List<String> queryCondition, int fetchNum) throws Exception {List<Long> result = new ArrayList<Long>();if (queryCondition.size() == 0) {return result;}StringBuffer condition = new StringBuffer();for (int i = 0; i < queryCondition.size(); i++) {if (i > 0) {condition.append(",");}condition.append(queryCondition.get(i));}Connection conn = null;try {conn = dataSource.getConnection();String sql = null;sql = "select ID from SCHEDULE_TEST where OWN_SIGN = '" + ownSign+ "' and mod(id," + queueNum + ") in ("+ condition.toString() + ") and sts ='N' LIMIT " + fetchNum;PreparedStatement statement = conn.prepareStatement(sql);ResultSet set = statement.executeQuery();while (set.next()) {result.add(set.getLong("ID"));}set.close();statement.close();return result;} finally {if (conn != null)conn.close();}}public void init() throws Exception {TBScheduleManagerFactory.createTBScheduleManager(taskType,ownSign);}}?

?

<bean id="schedule_source" destroy-method="close"><property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property><property name="defaultAutoCommit"><value>false</value></property><property name="url"><value>jdbc:mysql://localhost:3306/schedule?characterEncoding=utf-8</value></property><property name="username"><value>root</value></property><property name="password"><value>root</value></property></bean>

?

?

<beans default-autowire="byName"><bean id="registry" value="1522" /></bean><bean id="exporter" value="AUTODETECT_MBEAN" /><property name="namingStrategy" ref="namingStrategy" /></bean><bean id="namingStrategy"ref="jmxAttributeSource" /></bean><bean id="jmxAttributeSource"/><bean id="serverConnector"value="connector:name=rmi" /><property name="serviceUrl"value="service:jmx:rmi://localhost/jndi/rmi://localhost:1522/jmxconnector" /></bean><bean id="htmlAdaptor" value="5168"></property></bean></beans>

?

?

读书人网 >软件架构设计

热点推荐