Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* ************************************************************************
  #
  #  DivConq
  #
  #  http://divconq.com/
  #
  #  Copyright:
  #    Copyright 2014 eTimeline, LLC. All rights reserved.
  #
 #  License:
 #    See the license.txt file in the project's top-level directory for details.
 #
 #  Authors:
 #    * Andy White
 #
 ************************************************************************ */
 package divconq.lang.op;
 
 
Provides the same function support as

Author(s):
Andy
See also:
OperationResult, however used with callbacks when the method called is asynchronous.
 
 abstract public class OperationCallback extends OperationResult {
 	protected boolean called = false;
 	protected ISchedule timeout = null;
 	protected ReentrantLock oplock = new ReentrantLock();		
 	
 	public OperationCallback() {
 		super();
 	}
 	
 	public OperationCallback(TimeoutPlan plan) {
 		this();
 		
 		Task timeouttask = new Task()
 			.withWork(new IWork() {
 			public void run(TaskRun task) {
 				task.complete();
 			}
 		});
 		
 		this. = ..getScheduler().runIn(timeouttaskplan.getSeconds());
 	}
 	
 		super(ctx);
 	}
 	
 		this(ctx);
 		
 		Task timeouttask = new Task()
 			.withContext(ctx)
 			.withWork(new IWork() {
 			public void run(TaskRun task) {
 				task.complete();
 			}
 		});
 		
 		this. = ..getScheduler().runIn(timeouttaskplan.getSeconds());
 	}
 	
 	public void resetCalledFlag() {
 		this. = false;
 	}
 	
 	// override if need to do something on timeout/giveup on operation
 	// return true if timeout occurred, false if already completed
 	public boolean abandon() {
 		// courtesy only, no need to lock if we do know called is true, real called check below
 		if (this.)
 			return false;
 		
 		this..lock();
 		
 		try {
 			if (this.)
 				return false;
 			
 			this.errorTr(218, this..freezeToSafeRecord());
 		}
 		finally {
			this..unlock();
		}
		this.complete();
		return true;
	}
	public void complete() {
		// courtesy only, no need to look if we do know called is true, real called check below
		if (this.)
			return;
		this..lock();
		try {
			// check for race condition
			if (this.)
				return;
			this. = true;
		}
		finally {
			this..unlock();
		}
		// be sure we restore the context
		OperationContext ctx = OperationContext.get();
		try {
			OperationContext.set(this.);
			if (this. != null)
			OperationContext.set(this.);
			this.markEnd();
			this.callback();
			// TODO review, this may not be useful
			//this.opcontext.fireEvent(OperationEvents.COMPLETED, null);
		}
		finally {
			OperationContext.set(ctx);
		}
	}
	abstract public void callback();
New to GrepCode? Check out our FAQ X