浏览 2120 次
锁定老帖子 主题:java一般任务较好的写法举例
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2010-01-14
1.定义任务 /** * 表示一个任务 * */ public interface Task { /** * 运行实际的任务内容 * * @throws Exception */ public void runTask() throws Exception; /** * 返回Task的信息 * @return */ public TaskInfo getTaskInfo(); /** * 准备参数信息 * @param args * @throws Exception */ void initTaskInfo(String[] args); } 2.任务信息 /** * 表示一个任务的信息 * */ public class TaskInfo { private String taskName; // 参数列表 private Map<String, String> arguments = new HashMap<String, String>(); public TaskInfo() { } public TaskInfo(String taskName) { this.taskName = taskName; } public String getTaskName() { return taskName; } public void setTaskName(String taskName) { this.taskName = taskName; } public void addArgument(String argName, String argValue) { arguments.put(argName, argValue); } public String getArgument(String argName) { return arguments.get(argName); } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(taskName); if (arguments.size() > 0) { for (Entry<String, String> arg : arguments.entrySet()) { sb.append('[').append(arg.getKey()).append('=').append(arg.getValue()).append(']'); } } return sb.toString(); } } 3.任务实现support类。 /** * 任务support * */ public abstract class TaskSupport implements Task { protected TaskInfo taskInfo; @Override public void initTaskInfo(String[] args) { if (args.length == 0) { return; } taskInfo = new TaskInfo(args[0]); // 初如化参数列表 for (int i = 1; i < args.length; i++) { String[] argPair = args[i].split("="); if (initArg(argPair[0], argPair[1])) { taskInfo.addArgument(argPair[0], argPair[1]); } else { Log.warn("Argument ignored: " + args[i]); } } } protected boolean initArg(String name, String value) { return false; } @Override public TaskInfo getTaskInfo() { return taskInfo; } @Override final public void runTask() throws Exception { initTask(); try { doRunTask(); } finally { afterRunTask(); } } /** * 真正处理task的 */ abstract protected void doRunTask() throws Exception; /** * 用于收尾工作,如资源的释放等等 */ protected void afterRunTask() throws Exception { } /** * 初始化task中的一些信息 */ protected void initTask() throws Exception { } }
4.任务运行入口 /** * 所有task的启动类 * * */ public class TaskRunner { private static final Log log = LogFactory.getLog(TaskRunner.class); public static void main(String[] args) { Task task = prepareTask(args); if (task == null) { return; } try { log.info("Start running task: " + task.getTaskInfo()); task.runTask(); log.info("Finish running task: " + task.getTaskInfo()); } catch (Exception e) { log.error("Error running Task: " + task.getTaskInfo(), e); } } public static Task prepareTask(String[] args) { String[] beanFiles = new String[] { "*.xml", "*.xml","*.xml"}; ApplicationContext context = new ClassPathXmlApplicationContext(beanFiles); Map<String, Task> tasks = context.getBeansOfType(Task.class); if (args.length < 1 || !tasks.containsKey(args[0])) { log.error("No such task: " + args[0]); log.info(getTaskListStr(tasks)); return null; } Task task = tasks.get(args[0]); task.initTaskInfo(args); return task; } private static String getTaskListStr(Map<String, Task> tasks) { StringBuilder sb = new StringBuilder("All available tasks: "); for (String taskName : tasks.keySet()) { sb.append(taskName).append(','); } return sb.toString(); } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |