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.hub;
 
 import java.util.List;
 
 
Clock has three uses: 1) Control the date time settings - DivConq's DateTime are based on Joda's library. With Joda it is possible to set the effective timezone and the current date time itself. Through configuration the Clock can be made to enforce a fixed time, a sped up time (clock moving faster than 1 second per system second), or a regular time. Also the time zone can be set. 2) There are some methods for scheduling work on another thread. However, typically one should use divconq.scheduler.Scheduler for task scheduling. 3) DivConq supports obfuscated (encrypted) settings in the config files. This class provides access to the feature for encrypting and decrypting config settings.

Author(s):
Andy
 
 public class Clock {
 	class ClockThreadFactory implements ThreadFactory {
 		protected String name = null;
 		protected boolean daemon = true;
 		
 		public ClockThreadFactory(String nameboolean daemon) {
 			this. = name;
 			this. = daemon;
 		}
 		
 		public Thread newThread(Runnable r) {
 			Thread t = new Thread(rthis.);
 			
 			t.setDaemon(this.);
 			
 			// TODO consider this option
 			//t.setUncaughtExceptionHandler(eh);
 			
 			return t;
 		}
 	}
 	
     // periodic internal intervals, not for use with scheduling real tasks like dcTasks 
 	// does not preserve user context, uses system clock not divconq clock
 
 	// for the fast clock which run only tasks that are very quick to execute, < 10 ms total per entire run
 			new ClockThreadFactory("FastScheduler"false)   		//  we need something to keep us alive in Service mode - fast is not a daemon
 	);
 	
 	protected ScheduledFuture<?> fastclock = null;
 	protected List<ISystemWorkfastsyswork = new CopyOnWriteArrayList<>(); 
 	protected int fastsysworkcycle = 1;
 	protected SysReporter fastreporter = new SysReporter();
 
 	// for the clock itself
 	
 	// how many seconds to travel per system second - only enabled if using CoreDate
 	protected long speed = 0;
 	protected XElement config = null;	
 	protected ISettingsObfuscator obfus = null;	
 	protected DateTimeZone serverTimeZone = null;
 	
 	// for the slow clock - which is still fairly fast - execute system tasks that take less < 100ms total per entire run.
	// in a bad case, if it took a little longer not a problem...but generally runs fastish
			new ClockThreadFactory("SlowScheduler"true)
	);
	protected ScheduledFuture<?> slowclock = null;	
	protected List<ISystemWorkslowsyswork = new CopyOnWriteArrayList<>(); 
	protected int slowsysworkcycle = 1;	
	protected SysReporter slowreporter = new SysReporter();
		return this.;
	}
		return this.;
	}
		return this.;
	}
	public Clock() {
		this. = DateTimeZone.getDefault();
	}

    
Called from Hub.start this method configures the clock features.

Parameters:
or logger for the initialize
config xml holding the configuration
	public void init(OperationResult orXElement config) {
		this. = config;
		if (config != null) {
			String timeZone = config.getAttribute("TimeZone""UTC");
			if (StringUtil.isNotEmpty(timeZone))
				DateTimeZone.setDefault(DateTimeZone.forID(timeZone));
			String coreDate = config.getAttribute("CoreDate");
			if (StringUtil.isNotEmpty(coreDate)) {
				this.setAppClock(TimeUtil.parseDateTime(coreDate));
			}
			this. = StringUtil.parseInt(config.getAttribute("Speed"), 0);
			String obclass = config.getAttribute("TimerClass");
			if (StringUtil.isNotEmpty(obclass)) {
				try {
					Class<?> obc = this.getClass().getClassLoader().loadClass(obclass);
				}
				catch (Exception x) {
					Logger.error("Unable to load custom Settings Obfuscator class: " + obclass"Code""207");
				}
			}
		}
		if (this. == null)
		this..init(config);
	}

If the framework time is configured to use an accelerated clock, this method gets that accelerated clock going.

Parameters:
or logger for the start up
	public void start(OperationResult or) {		
		// we check our app schedule ever 1 (or so) system seconds
			public void run() {
				OperationContext.useHubContext();
				int cycle = Clock.this.;
				Clock.this..setStatus("Slow Sys starting cycle: " + cycle);
				for (ISystemWork work : Clock.this.) {
					try {
						int period = work.period();
						if (period > 300)
							period = 300;		// max allowed						
						if (cycle % period  == 0) {
							Clock.this..setStatus("before sys work: " + work.getClass());
							Clock.this..setStatus("after sys work: " + work.getClass());
						}
					}
					catch(Exception x) {
						..println("sys scheduler error: " + x);
					}
				}
				Clock.this..setStatus("Slow Sys finished cycle: " + cycle);
				cycle++;
				if (cycle > 300)
					cycle = 1;
				Clock.this. = cycle;
			}
		}, 1, 1, .);
		// we check our app schedule ever 1 (or so) system seconds
			public void run() {
				OperationContext.useHubContext();
				int cycle = Clock.this.;
				Clock.this..setStatus("Fast Sys starting cycle: " + cycle);
				for (ISystemWork work : Clock.this.) {
					try {
						int period = work.period();
						if (period > 300)
							period = 300;		// max allowed						
						if (cycle % period  == 0) {
							Clock.this..setStatus("before sys work: " + work.getClass());
							Clock.this..setStatus("after sys work: " + work.getClass());
						}
					}
					catch(Exception x) {
						..println("sys scheduler error: " + x);
					}
				}
				Clock.this..setStatus("Fast Sys finished cycle: " + cycle);
				cycle++;
				if (cycle > 300)
					cycle = 1;
				Clock.this. = cycle;
			}
		}, 1, 1, .);
		if (this. != 0) 
				public int period() {
					return 1;
				}
				public void run(SysReporter reporter) {
					reporter.setStatus("Before set date time");
					DateTimeUtils.setCurrentMillisFixed(DateTimeUtils.currentTimeMillis() + (Clock.this. * 1000));
					reporter.setStatus("After set date time");
				}
			});


Terminate tasks being run on the Clock's scheduler. Called by Hub.stop during the final steps of shutdown.

Parameters:
or logger for the shutdown
	public void stop(OperationResult or) {
		if (this. != null)
			this..cancel(false);
		if (this. != null)
			this..cancel(false);
		try {
		catch (InterruptedException e) {
			// TODO 
		}
	}
	// your period must be 300 or less or it will never occur
	// you must be very careful with this, problems in your sysworker will lock down the whole hub process 
	public void addSlowSystemWorker(ISystemWork v) {
		if (v == null)
			return;
	}

Typically use divconq.scheduler.Scheduler for task scheduling. However, when a task should run on the system clock rather than the app clock, these methods may be used. Use sparingly as these are run on a single threaded pool. These methods also do not preserve the TaskContext.

Parameters:
v new work for the fast thread
	// your period must be 300 or less or it will never occur
	// you must be very careful with this, problems in your sysworker will lock down the whole hub process 
	public void addFastSystemWorker(ISystemWork v) {
		if (v == null)
			return;
	}

set framework's time to use the system time
	public void resetAppClock() {
		DateTimeUtils.setCurrentMillisSystem();
	}

set framework's time to use a fixed time

Parameters:
time fixed time to use
	public void setAppClock(ReadableInstant time) {
		DateTimeUtils.setCurrentMillisFixed(time.getMillis());
	}

Typically use divconq.scheduler.Scheduler for task scheduling. However, when a task should run on the system clock rather than the app clock, these methods may be used. Use sparingly as these are run on a single threaded pool. These methods also do not preserve the TaskContext.

Parameters:
command the code for the task to run
delaySecs how many seconds until the task is run
Returns:
the token for canceling the task
	public ScheduledFuture<?> scheduleOnceInternal(Runnable commandlong delaySecs) {
		return this..schedule(commanddelaySecs.);
	}

Typically use divconq.scheduler.Scheduler for task scheduling. However, when a task should run on the system clock rather than the app clock, these methods may be used. Use sparingly as these are run on a single threaded pool. These methods also do not preserve the TaskContext.

Parameters:
command the code for the task to run
delay how many "units" until the task is run
unit time unit used with delay
Returns:
the token for canceling the task
	public ScheduledFuture<?> scheduleOnceInternal(Runnable commandlong delayTimeUnit unit) {
		return this..schedule(commanddelayunit);
	}

Typically use divconq.scheduler.Scheduler for task scheduling. However, when a task should run on the system clock rather than the app clock, these methods may be used. Use sparingly as these are run on a single threaded pool. These methods also do not preserve the TaskContext.

Parameters:
command the code for the task to run
periodSecs how many seconds until the task is run first time, and then how long between runs
Returns:
the token for canceling the task
	public ScheduledFuture<?> schedulePeriodicInternal(Runnable commandlong periodSecs) {
		return this..scheduleAtFixedRate(commandperiodSecsperiodSecs.);
	}

Typically use divconq.scheduler.Scheduler for task scheduling. However, when a task should run on the system clock rather than the app clock, these methods may be used. Use sparingly as these are run on a single threaded pool. These methods also do not preserve the TaskContext.

Parameters:
command the code for the task to run
initialDelay how many "units" until the task is first run
period how many "units" between task runs, after first run
unit time unit used with delay and period
Returns:
the token for canceling the task
	public ScheduledFuture<?> schedulePeriodicInternal(Runnable commandlong initialDelaylong periodTimeUnit unit) {
		return this..scheduleAtFixedRate(commandinitialDelayperiodunit);
	}

Config file settings containing sensitive info may be obscured (encypted) to make it hard for a hacker to get anything useful from just a copy of the config file. How the settings are obscured is based on the Clock's settings. For more details on how this works

Returns:
the obfuscator used with this settings file
See also:
divconq.util.ISettingsObfuscator
		return this.;
	}
New to GrepCode? Check out our FAQ X