Top Description Fields Constructors Methods
org.apache.derby.impl.services.daemon

public Class BasicDaemon

extends Object
implements DaemonService, Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable, org.apache.derby.iapi.services.daemon.DaemonService
Imports
org.apache.derby.iapi.services.context.ContextService, .ContextManager, org.apache.derby.iapi.services.daemon.DaemonService, .Serviceable, org.apache.derby.iapi.services.monitor.Monitor, .ModuleFactory, org.apache.derby.shared.common.sanity.SanityManager, org.apache.derby.shared.common.error.StandardException, java.util.Vector, .List, org.apache.derby.iapi.util.InterruptStatus

A BasicDaemon is a background worker thread which does asynchronous I/O and general clean up. It should not be used as a general worker thread for parallel execution. One cannot count on the order of request or count on when the daemon will wake up, even with serviceNow requests. Request are not persistent and not recoverable, they are all lost when the system crashes or is shutdown. System shutdown, even orderly ones, do not wait for daemons to finish its work or empty its queue. Furthermore, any Serviceable subscriptions, including onDemandOnly, must tolerate spurious services. The BasicDaemon will setup a context manager with no context on it. The Serviceable object's performWork must provide useful context on the context manager to do its work. The BasicDaemon will wrap performWork call with try / catch block and will use the ContextManager's error handling to clean up any error. The BasicDaemon will guarentee serviceNow request will not be lost as long as the jbms does not crash - however, if N serviceNow requests are made by the same client, it may only be serviced once, not N times. Many Serviceable object will subscribe to the same BasicDaemon. Their performWork method should be well behaved - in other words, it should not take too long or hog too many resources or deadlock with anyone else. And it cannot (should not) error out. The BasicDaemon implementation manages the DaemonService's data structure, handles subscriptions and enqueues requests, and determine the service schedule for its Serviceable objects. The BasicDaemon keeps an array (Vector) of Serviceable subscriptions it also keeps 2 queues for clients that uses it for one time service - the 1st queue is for a serviceNow enqueue request, the 2nd queue is for non serviceNow enqueue request. This BasicDaemon services its clients in the following order: 1. any subscribed client that have made a serviceNow request that has not been fulfilled 2. serviceable clients on the 1st queue 3. all subscribed clients that are not onDemandOnly 4. serviceable clients 2nd queue

Field Summary

Modifier and TypeField and Description
private boolean
protected final ContextManager
protected final ContextService
private int
private final List<ServiceRecord>
highPQ

Queues for the work to be done.

private boolean
private long
private int
nextService

which subscribed clients to service next?

private final List<ServiceRecord>
private int
private static final int
private boolean
private boolean
private boolean
private final Vector<ServiceRecord>
private boolean
waiting

true if I'm waiting, if this is false then I am running and a notify is not required.

Constructor Summary

AccessConstructor and Description
public
BasicDaemon(ContextService contextService)

make a BasicDaemon

Method Summary

Modifier and TypeMethod and Description
public synchronized void
clear()

Implements org.apache.derby.iapi.services.daemon.DaemonService.clear.

Get rid of all queued up Serviceable tasks.

public boolean
enqueue(Serviceable
the object that needs a one time service
newClient
,
boolean
if true, this client should be serviced ASAP, as if a serviceNow has been issued. If false, then this client will be serviced with the normal scheduled.
serviceNow
)

Implements org.apache.derby.iapi.services.daemon.DaemonService.enqueue.

Request a one time service from the Daemon.

pack-priv static ModuleFactory
getMonitor()

Must be package private so that user code can't call this entry point.

private synchronized boolean
protected ServiceRecord
nextAssignment(boolean urgent)

public void
private boolean
rest()

Returns true if awakened by some notification, false if wake up by timer

public void
resume()

Implements org.apache.derby.iapi.services.daemon.DaemonService.resume.

Resume service after a pause

public void
run()

Implements java.lang.Runnable.run.

Runs this operation.

protected void
public void
serviceNow(int
the number that uniquely identifies the client
clientNumber
)

Implements org.apache.derby.iapi.services.daemon.DaemonService.serviceNow.

Service this subscription ASAP.

private static void
setThreadPriority(ModuleFactory mf, final int priority)

Change the priority of the current thread, but only if it was created by ModuleFactory#getDaemonThread.

public void
stop()

Implements org.apache.derby.iapi.services.daemon.DaemonService.stop.

Finish what we are doing and at the next convenient moment, get rid of the thread and make the daemon object goes away if possible.

private synchronized boolean
public int
subscribe(Serviceable
a Serviceable object this daemon will service from time to time
newClient
,
boolean
only service this client when it ask for service with a serviceNow request
onDemandOnly
)

Implements org.apache.derby.iapi.services.daemon.DaemonService.subscribe.

Add a new client that this daemon needs to service

public void
unsubscribe(int
client identifier
clientNumber
)

Implements org.apache.derby.iapi.services.daemon.DaemonService.unsubscribe.

Removes a client from the list of subscribed clients.

public void
protected synchronized void
private void
work(boolean urgentOnly)

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

awakenedback to summary
private boolean awakened
contextMgrback to summary
protected final ContextManager contextMgr
contextServiceback to summary
protected final ContextService contextService
earlyWakeupCountback to summary
private int earlyWakeupCount
highPQback to summary
private final List<ServiceRecord> highPQ

Queues for the work to be done. These are synchronized by this object.

inPauseback to summary
private boolean inPause
lastServiceTimeback to summary
private long lastServiceTime
nextServiceback to summary
private int nextService

which subscribed clients to service next? only accessed by daemon thread

normPQback to summary
private final List<ServiceRecord> normPQ
numClientsback to summary
private int numClients
OPTIMAL_QUEUE_SIZEback to summary
private static final int OPTIMAL_QUEUE_SIZE
runningback to summary
private boolean running
stoppedback to summary
private boolean stopped
stopRequestedback to summary
private boolean stopRequested
subscriptionback to summary
private final Vector<ServiceRecord> subscription
waitingback to summary
private boolean waiting

true if I'm waiting, if this is false then I am running and a notify is not required.

Constructor Detail

BasicDaemonback to summary
public BasicDaemon(ContextService contextService)

make a BasicDaemon

Method Detail

clearback to summary
public synchronized void clear()

Implements org.apache.derby.iapi.services.daemon.DaemonService.clear.

Get rid of all queued up Serviceable tasks.

enqueueback to summary
public boolean enqueue(Serviceable newClient, boolean serviceNow)

Implements org.apache.derby.iapi.services.daemon.DaemonService.enqueue.

Doc from org.apache.derby.iapi.services.daemon.DaemonService.enqueue.

Request a one time service from the Daemon. Unless performWork returns REQUEUE (see Serviceable), the daemon will service this client once and then it will get rid of this client. Since no client number is associated with this client, it cannot request to be serviced or be unsubscribed. The work is always added to the deamon, regardless of the state it returns.

Parameters
newClient:Serviceable

the object that needs a one time service

serviceNow:boolean

if true, this client should be serviced ASAP, as if a serviceNow has been issued. If false, then this client will be serviced with the normal scheduled.

Returns:boolean

true if the daemon indicates it is being overloaded, false it's happy.

getMonitorback to summary
pack-priv static ModuleFactory getMonitor()

Must be package private so that user code can't call this entry point.

inPauseback to summary
private synchronized boolean inPause()
nextAssignmentback to summary
protected ServiceRecord nextAssignment(boolean urgent)
pauseback to summary
public void pause()

Implements org.apache.derby.iapi.services.daemon.DaemonService.pause.

Doc from org.apache.derby.iapi.services.daemon.DaemonService.pause.

Pause. No new service is performed until a resume is issued.

restback to summary
private boolean rest()

Returns true if awakened by some notification, false if wake up by timer

resumeback to summary
public void resume()

Implements org.apache.derby.iapi.services.daemon.DaemonService.resume.

Doc from org.apache.derby.iapi.services.daemon.DaemonService.resume.

Resume service after a pause

runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

serviceClientback to summary
protected void serviceClient(ServiceRecord clientRecord)
serviceNowback to summary
public void serviceNow(int clientNumber)

Implements org.apache.derby.iapi.services.daemon.DaemonService.serviceNow.

Doc from org.apache.derby.iapi.services.daemon.DaemonService.serviceNow.

Service this subscription ASAP. When this method is called, the subscriber's performWork() method is guaranteed to be invoked at some point in the future. However, there is no guarantee that a subscriber's performWork() is called the same number of times as the subscriber calls this method. More precisely, if a subscriber is waiting for this daemon service to invoke its performWork() method, the daemon service may, but is not required to, ignore requests from that subscriber until the performWork() method has been invoked.

Parameters
clientNumber:int

the number that uniquely identifies the client

setThreadPriorityback to summary
private static void setThreadPriority(ModuleFactory mf, final int priority)

Change the priority of the current thread, but only if it was created by ModuleFactory#getDaemonThread.

stopback to summary
public void stop()

Implements org.apache.derby.iapi.services.daemon.DaemonService.stop.

Finish what we are doing and at the next convenient moment, get rid of the thread and make the daemon object goes away if possible. remember we are calling from another thread

stopRequestedback to summary
private synchronized boolean stopRequested()
subscribeback to summary
public int subscribe(Serviceable newClient, boolean onDemandOnly)

Implements org.apache.derby.iapi.services.daemon.DaemonService.subscribe.

Doc from org.apache.derby.iapi.services.daemon.DaemonService.subscribe.

Add a new client that this daemon needs to service

Parameters
newClient:Serviceable

a Serviceable object this daemon will service from time to time

onDemandOnly:boolean

only service this client when it ask for service with a serviceNow request

Returns:int

a client number that uniquely identifies this client (this subscription)

unsubscribeback to summary
public void unsubscribe(int clientNumber)

Implements org.apache.derby.iapi.services.daemon.DaemonService.unsubscribe.

Removes a client from the list of subscribed clients. The call does not wait for the daemon to finish the work it is currently performing. Therefore, the client must tolerate that its performWork() method could be invoked even after the call to unsubscribe() has returned (but not more than once).

Parameters
clientNumber:int

client identifier

waitUntilQueueIsEmptyback to summary
public void waitUntilQueueIsEmpty()

Implements org.apache.derby.iapi.services.daemon.DaemonService.waitUntilQueueIsEmpty.

wakeUpback to summary
protected synchronized void wakeUp()
workback to summary
private void work(boolean urgentOnly)
yieldNowback to summary
private void yieldNow()