WaitTask.java :  » Web-Crawler » jspider » net » javacoding » jspider » mockobjects » Java Open Source

Java Open Source » Web Crawler » jspider 
jspider » net » javacoding » jspider » mockobjects » WaitTask.java
package net.javacoding.jspider.mockobjects;

import net.javacoding.jspider.core.SpiderContext;
import net.javacoding.jspider.core.task.work.BaseWorkerTaskImpl;
import net.javacoding.jspider.core.task.WorkerTask;

/**
 * Mock implementation of a Worker task.  No real work is done, but there is
 * simply done a wait for a certain amount of milliseconds.
 *
 * $Id: WaitTask.java,v 1.8 2003/04/10 16:19:25 vanrogu Exp $
 *
 * @author  Gnther Van Roey
 */
public class WaitTask extends BaseWorkerTaskImpl {

    /** the number of milliseconds to wait. */
    protected int wait;

    /** the number of milliseconds to wait during prepare method. */
    protected int waitDuringPrepare;

    /**
     * Public constructor.
     * @param context the spidercontext in user
     * @param wait the number of milliseconds to wait
     */
    public WaitTask ( SpiderContext context, int wait ) {
        this ( context, wait, 0);
    }

    /**
     * Public constructor.
     * @param context the spidercontext in user
     * @param wait the number of milliseconds to wait
     */
    public WaitTask ( SpiderContext context, int wait, int waitDuringPrepare ) {
        super(context, WorkerTask.WORKERTASK_THINKERTASK);
        this.wait = wait;
        this.waitDuringPrepare = waitDuringPrepare;
    }

    public void prepare() {
        if ( waitDuringPrepare > 0 ) {
            //System.out.println("Waiting in prepare() : " + waitDuringPrepare + " ms");
            try {
                Thread.currentThread().wait(waitDuringPrepare);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

    /**
     * Worker method implementation.  Will just wait the specified number
     * of milliseconds, and then return.
     */
    public void execute() {
        //System.out.println("Waiting in execute() : " + wait + " ms");
        try {
            Thread.currentThread().wait(wait);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    // overridden here to avoid NullPointerException when BaseImpl accesses Agent
    public void tearDown() {
    }

    public int getType() {
        return WorkerTask.WORKERTASK_THINKERTASK;
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.