Schedule2.0.1 demo应用教程(二)

 

 

创建任务处理类

 

在创建schedule配置文件前,我们先完成任务处理类的编写,该类是业务系统进行数据处理的实现类。要求实现Schedule的接口IScheduleTaskDealMulti或者IScheduleTaskDealSingle

接口主要包括三个方法。一个是根据调度器分配到的队列查询数据的接口,一个是进行数据处理的接口,一个是NOTSLEEP模式下需要实现的比较接口(用于判断重复的任务)。该类里还有一个init方法在spring加载此类时启动该任务的执行操作。

 

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);
	}
}
 

 

 

配置数据库连接

 

db4MySql.xml文件中配置连接schedule数据源

 

<bean id="schedule_source" class="org.apache.commons.dbcp.BasicDataSource" 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>

 

 

配置schedule mbean文件

 

Schedule可通过JMX控制调度服务的创建和停止,mbean-config.xml文件其相关配置JMX配置,HtmlAdaptor暴露端口号为5168

 

 

<beans default-autowire="byName">
	<bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
		<property name="port" value="1522" />
	</bean>

	<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"
		lazy-init="false">
		<property name="autodetectModeName" value="AUTODETECT_MBEAN" />
		<property name="namingStrategy" ref="namingStrategy" />
	</bean>

	<bean id="namingStrategy"
		class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
		<property name="attributeSource" ref="jmxAttributeSource" />
	</bean>

	<bean id="jmxAttributeSource"
		class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />

	<bean id="serverConnector"
		class="org.springframework.jmx.support.ConnectorServerFactoryBean">
		<property name="objectName" value="connector:name=rmi" />
		<property name="serviceUrl"
			value="service:jmx:rmi://localhost/jndi/rmi://localhost:1522/jmxconnector" />
	</bean>

	<bean id="htmlAdaptor" class="com.sun.jdmk.comm.HtmlAdaptorServer"
		init-method="start">
		<property name="port" value="5168"></property>
	</bean>
</beans>

 

 

Schedule2.0.1 demo应用教程(三)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值