darkstar处理超时,分解Task的方案! (2)

package cn.vicky.sgs.app; import java.io.Serializable; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import cn.vicky.sgs.mo.Counter; import cn.vicky.sgs.task.BaseTask; import cn.vicky.sgs.task.impl.TestTask; import com.sun.sgs.app.AppContext; import com.sun.sgs.app.AppListener; import com.sun.sgs.app.ClientSession; import com.sun.sgs.app.ClientSessionListener; import com.sun.sgs.app.NameNotBoundException; import com.sun.sgs.app.util.ScalableList; public class MainApp implements AppListener, Serializable { private static final long serialVersionUID = 1L; private static final Logger logger = LoggerFactory.getLogger(MainApp.class); public void initialize(Properties properties) { logger.info("initialize begin ... "); long time1 = System.currentTimeMillis(); ScalableList<BaseTask> testTasks = new ScalableList<BaseTask>(); // 一个任务需要执行的子任务 ... testTasks.add(new TestTask()); testTasks.add(new TestTask()); testTasks.add(new TestTask()); testTasks.add(new TestTask()); testTasks.add(new TestTask()); class NewTask extends BaseTask{ private static final long serialVersionUID = 1L; public NewTask() { } public NewTask(ScalableList<BaseTask> subTasks) { super(subTasks); } public void run() throws Exception { long time1 = System.currentTimeMillis(); doSubTask(); long id = 0; Counter counter = null; try { counter = (Counter) AppContext.getDataManager().getBinding("id"); } catch (NameNotBoundException e) { AppContext.getDataManager().setBinding("id", new Counter()); counter = (Counter) AppContext.getDataManager().getBinding("id"); } id = counter.next(); logger.info("Done ~ " + id); logger.info(" NewTask 1 "); long time2 = System.currentTimeMillis(); logger.info("last for " + (time2 - time1) + " ms!"); } } class NewTask2 extends BaseTask{ private static final long serialVersionUID = 1L; public NewTask2() { } public NewTask2(ScalableList<BaseTask> subTasks) { super(subTasks); } public void run() throws Exception { long time1 = System.currentTimeMillis(); doSubTask(); long id = 0; Counter counter = null; try { counter = (Counter) AppContext.getDataManager().getBinding("id"); } catch (NameNotBoundException e) { AppContext.getDataManager().setBinding("id", new Counter()); counter = (Counter) AppContext.getDataManager().getBinding("id"); } id = counter.next(); logger.info("Done ~ " + id); logger.info(" NewTask 2 "); long time2 = System.currentTimeMillis(); logger.info("last for " + (time2 - time1) + " ms!"); } } class NewTask3 extends BaseTask{ private static final long serialVersionUID = 1L; public NewTask3() { } public NewTask3(ScalableList<BaseTask> subTasks) { super(subTasks); } public void run() throws Exception { long time1 = System.currentTimeMillis(); doSubTask(); long id = 0; Counter counter = null; try { counter = (Counter) AppContext.getDataManager().getBinding("id"); } catch (NameNotBoundException e) { AppContext.getDataManager().setBinding("id", new Counter()); counter = (Counter) AppContext.getDataManager().getBinding("id"); } id = counter.next(); logger.info("Done ~ " + id); logger.info(" NewTask 3 "); long time2 = System.currentTimeMillis(); logger.info("last for " + (time2 - time1) + " ms!"); } } class NewTask4 extends BaseTask{ private static final long serialVersionUID = 1L; public NewTask4() { } public NewTask4(ScalableList<BaseTask> subTasks) { super(subTasks); } public void run() throws Exception { long time1 = System.currentTimeMillis(); doSubTask(); long id = 0; Counter counter = null; try { counter = (Counter) AppContext.getDataManager().getBinding("id"); } catch (NameNotBoundException e) { AppContext.getDataManager().setBinding("id", new Counter()); counter = (Counter) AppContext.getDataManager().getBinding("id"); } id = counter.next(); logger.info("Done ~ " + id); logger.info(" NewTask 4 "); long time2 = System.currentTimeMillis(); logger.info("last for " + (time2 - time1) + " ms!"); } } ScalableList<BaseTask> testTasks2 = new ScalableList<BaseTask>(); testTasks2.add(new NewTask2()); ScalableList<BaseTask> testTasks3 = new ScalableList<BaseTask>(); testTasks3.add(new TestTask()); testTasks3.add(new TestTask()); testTasks3.add(new TestTask()); testTasks2.add(new NewTask3(testTasks3)); testTasks2.add(new NewTask4()); testTasks.add(new NewTask(testTasks2)); testTasks.add(new TestTask()); testTasks.add(new TestTask()); testTasks.add(new TestTask()); AppContext.getTaskManager().scheduleTask(new TestTask(testTasks)); long time2 = System.currentTimeMillis(); logger.info("************** last for " + (time2 - time1) + " ms ***************"); logger.info("initialize over ... "); } public ClientSessionListener loggedIn(ClientSession clientsession) { return null; } }

 

 

package cn.vicky.sgs.task; import java.io.Serializable; import com.sun.sgs.app.AppContext; import com.sun.sgs.app.ManagedReference; import com.sun.sgs.app.Task; import com.sun.sgs.app.util.ScalableList; public abstract class BaseTask implements Task, Serializable { private static final long serialVersionUID = 1L; private ManagedReference<ScalableList<BaseTask>> subTasksRef; public BaseTask() { } public BaseTask(ScalableList<BaseTask> subTasks) { subTasksRef = AppContext.getDataManager().createReference(subTasks); } public void doSubTask() { if (subTasksRef != null && subTasksRef.get() != null) { BaseTask task = null; try { task = subTasksRef.getForUpdate().remove(0); } catch (IndexOutOfBoundsException e) { } if (task != null) { if (task.getSubTasksRef() != null && task.getSubTasksRef().get() != null && task.getSubTasksRef().get().size() > 0) { subTasksRef.get().addAll(0,task.getSubTasksRef().get()); } task.setSubTasksRef(subTasksRef); AppContext.getTaskManager().scheduleTask(task); } } } public void setSubTasksRef(ManagedReference<ScalableList<BaseTask>> subTasksRef) { this.subTasksRef = subTasksRef; } public ManagedReference<ScalableList<BaseTask>> getSubTasksRef() { return subTasksRef; } }

 

其他参见:darkstar处理超时,分解Task的方案!

 

输出

2010-4-13 16:52:37 com.sun.sgs.impl.kernel.Kernel <init> 信息: The Kernel is ready, version: 0.9.11 2010-4-13 16:52:37 com.sun.sgs.impl.service.data.store.DataStoreImpl <init> 信息: Creating database directory : G:/学习/SubTask/data/MainApp/dsdb 2010-4-13 16:52:42 com.sun.sgs.impl.service.watchdog.WatchdogServerImpl registerNode 信息: node:com.sun.sgs.impl.service.watchdog.NodeImpl[1,health:GREEN,backup:(none)]@12D5D97FF9764F4 registered [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - initialize] - initialize begin ... [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - initialize] - ************** last for 0 ms *************** [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - initialize] - initialize over ... 2010-4-13 16:52:43 com.sun.sgs.impl.kernel.Kernel startApplication 信息: MainApp: application is ready [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - Done ~ 1 [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - last for 16 ms! [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - Done ~ 2 [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - last for 0 ms! [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - Done ~ 3 [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - last for 0 ms! [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - Done ~ 4 [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - last for 16 ms! [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - Done ~ 5 [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - last for 0 ms! [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - Done ~ 6 [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - last for 16 ms! [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - run] - Done ~ 7 [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - run] - NewTask 1 [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - run] - last for 0 ms! [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - run] - Done ~ 8 [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - run] - NewTask 2 [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - run] - last for 0 ms! [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - run] - Done ~ 9 [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - run] - NewTask 3 [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - run] - last for 15 ms! [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - Done ~ 10 [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - last for 0 ms! [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - Done ~ 11 [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - last for 16 ms! [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - Done ~ 12 [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - last for 0 ms! [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - run] - Done ~ 13 [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - run] - NewTask 4 [2010-04-13 16:52:43][cn.vicky.sgs.app.MainApp - run] - last for 16 ms! [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - Done ~ 14 [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - last for 0 ms! [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - Done ~ 15 [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - last for 15 ms! [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - Done ~ 16 [2010-04-13 16:52:43][cn.vicky.sgs.task.impl.TestTask - run] - last for 0 ms!

你可能感兴趣的:(exception,properties,null,application,Class,2010)