Sometimes a client needs to send a special message to a server in order to tell the server to do something. The server may need to be signalled to perform a defragment or it may need to be signalled to shut itself down gracefully.
This is configured by calling setMessageRecipient(), passing the object that will process client-initiated messages.
01/** 02
* opens the ObjectServer, and waits forever until close() is called 03
* or a StopServer message is being received. 04
*/ 05
public void runServer(){ 06
synchronized(this){ 07
ObjectServer db4oServer = Db4o.openServer(FILE, PORT); 08
db4oServer.grantAccess(USER, PASS); 09
10
// Using the messaging functionality to redirect all 11
// messages to this.processMessage 12
db4oServer.ext().configure().clientServer().setMessageRecipient(this); 13
14
// to identify the thread in a debugger 15
Thread.currentThread().setName(this.getClass().getName()); 16
17
// We only need low priority since the db4o server has 18
// it's own thread. 19
Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 20
try { 21
if(! stop){ 22
// wait forever for notify() from close() 23
this.wait(Long.MAX_VALUE); 24
} 25
} catch (Exception e) { 26
e.printStackTrace(); 27
} 28
db4oServer.close(); 29
} 30
}