org.cojen.util
Class BelatedCreator<T,E extends Exception>

java.lang.Object
  extended by org.cojen.util.BelatedCreator<T,E>

public abstract class BelatedCreator<T,E extends Exception>
extends Object

Generic one-shot factory which supports late object creation. If the object creation results in an exception or is taking too long, the object produced instead is a bogus one. After retrying, if the real object is created, then the bogus object turns into a wrapper to the real object.

Note: If a bogus object is created, the wrapper cannot always be a drop-in replacement for the real object. If the wrapper is cloned, it won't have the same behavior as cloning the real object. Also, synchronizing on the wrapper will not synchronize the real object.

Since:
2.1
Author:
Brian S O'Neill

Constructor Summary
protected BelatedCreator(Class<T> type, int minRetryDelayMillis)
           
 
Method Summary
protected abstract  T createBogus()
          Create instance of bogus object.
protected  void createdNotification(T object)
          Notification that createReal has produced the real object.
protected abstract  T createReal()
          Create instance of real object.
 T get(int timeoutMillis)
          Returns real or bogus object.
protected abstract  void timedOutNotification(long timedOutMillis)
          Notification that createReal is taking too long.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BelatedCreator

protected BelatedCreator(Class<T> type,
                         int minRetryDelayMillis)
Parameters:
type - type of object created
minRetryDelayMillis - minimum milliseconds to wait before retrying to create object after failure; if negative, never retry
Throws:
IllegalArgumentException - if type is null or is not an interface
Method Detail

get

public T get(int timeoutMillis)
      throws E extends Exception
Returns real or bogus object. If real object is returned, then future invocations of this method return the same real object instance. This method waits for the real object to be created, if it is blocked. If real object creation fails immediately, then this method will not wait, returning a bogus object immediately instead.

Parameters:
timeoutMillis - maximum time to wait for real object before returning bogus one; if negative, potentially wait forever
Throws:
E - exception thrown from createReal
E extends Exception

createReal

protected abstract T createReal()
                         throws E extends Exception
Create instance of real object. If there is a recoverable error creating the object, return null. Any error logging must be performed by the implementation of this method. If null is returned, expect this method to be called again in the future.

Returns:
real object, or null if there was a recoverable error
Throws:
E - unrecoverable error
E extends Exception

createBogus

protected abstract T createBogus()
Create instance of bogus object.


timedOutNotification

protected abstract void timedOutNotification(long timedOutMillis)
Notification that createReal is taking too long. This can be used to log a message.

Parameters:
timedOutMillis - milliseconds waited before giving up

createdNotification

protected void createdNotification(T object)
Notification that createReal has produced the real object. The default implementation does nothing.



Copyright © 2004-2008 Brian S O'Neill. All Rights Reserved.