Simple db4o Server

Let's implement a simple standalone db4o server with a special client that can tell the server to shut itself down gracefully on demand.

First, both the client and the server need some shared configuration information. We will provide this using an interface:

ServerConfiguration.java
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02package com.db4odoc.clientserver; 03 04/** 05 * Configuration used for {@link StartServer} and {@link StopServer}. 06 */ 07public interface ServerConfiguration { 08 09 /** 10 * the host to be used. 11 * <br>If you want to run the client server examples on two computers, 12 * enter the computer name of the one that you want to use as server. 13 */ 14 public String HOST = "localhost"; 15 16 /** 17 * the database file to be used by the server. 18 */ 19 public String FILE = "reference.db4o"; 20 21 /** 22 * the port to be used by the server. 23 */ 24 public int PORT = 0xdb40; 25 26 /** 27 * the user name for access control. 28 */ 29 public String USER = "db4o"; 30 31 /** 32 * the pasword for access control. 33 */ 34 public String PASS = "db4o"; 35}

Now we'll create the server:

StartServer.java
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02package com.db4odoc.clientserver; 03 04import com.db4o.*; 05import com.db4o.messaging.*; 06 07/** 08 * starts a db4o server with the settings from {@link ServerConfiguration}. 09 * <br><br>This is a typical setup for a long running server. 10 * <br><br>The Server may be stopped from a remote location by running 11 * StopServer. The StartServer instance is used as a MessageRecipient and 12 * reacts to receiving an instance of a StopServer object. 13 * <br><br>Note that all user classes need to be present on the server 14 * side and that all possible Db4o.configure() calls to alter the db4o 15 * configuration need to be executed on the client and on the server. 16 */ 17public class StartServer 18 implements ServerConfiguration, MessageRecipient { 19 20 /** 21 * setting the value to true denotes that the server should be closed 22 */ 23 private boolean stop = false; 24 25 /** 26 * starts a db4o server using the configuration from 27 * {@link ServerConfiguration}. 28 */ 29 public static void main(String[] arguments) { 30 new StartServer().runServer(); 31 } 32 // end main 33 34 /** 35 * opens the ObjectServer, and waits forever until close() is called 36 * or a StopServer message is being received. 37 */ 38 public void runServer(){ 39 synchronized(this){ 40 ObjectServer db4oServer = Db4o.openServer(FILE, PORT); 41 db4oServer.grantAccess(USER, PASS); 42 43 // Using the messaging functionality to redirect all 44 // messages to this.processMessage 45 db4oServer.ext().configure().clientServer().setMessageRecipient(this); 46 47 // to identify the thread in a debugger 48 Thread.currentThread().setName(this.getClass().getName()); 49 50 // We only need low priority since the db4o server has 51 // it's own thread. 52 Thread.currentThread().setPriority(Thread.MIN_PRIORITY); 53 try { 54 if(! stop){ 55 // wait forever for notify() from close() 56 this.wait(Long.MAX_VALUE); 57 } 58 } catch (Exception e) { 59 e.printStackTrace(); 60 } 61 db4oServer.close(); 62 } 63 } 64 // end runServer 65 66 /** 67 * messaging callback 68 * @see com.db4o.messaging.MessageRecipient#processMessage(ObjectContainer, Object) 69 */ 70 public void processMessage(MessageContext context, Object message) { 71 if(message instanceof StopServer){ 72 close(); 73 } 74 } 75 // end processMessage 76 77 /** 78 * closes this server. 79 */ 80 public void close(){ 81 synchronized(this){ 82 stop = true; 83 this.notify(); 84 } 85 } 86 // end close 87}

And last but not least, the client that stops the server.

StopServer.java
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02package com.db4odoc.clientserver; 03 04import com.db4o.*; 05import com.db4o.messaging.*; 06 07/** 08 * stops the db4o Server started with {@link StartServer}. 09 * <br><br>This is done by opening a client connection 10 * to the server and by sending a StopServer object as 11 * a message. {@link StartServer} will react in it's 12 * processMessage method. 13 */ 14public class StopServer implements ServerConfiguration { 15 16 /** 17 * stops a db4o Server started with StartServer. 18 * @throws Exception 19 */ 20 public static void main(String[] args) { 21 ObjectContainer objectContainer = null; 22 try { 23 24 // connect to the server 25 objectContainer = Db4o.openClient(HOST, PORT, USER, PASS); 26 27 } catch (Exception e) { 28 e.printStackTrace(); 29 } 30 31 if(objectContainer != null){ 32 33 // get the messageSender for the ObjectContainer 34 MessageSender messageSender = objectContainer.ext() 35 .configure().clientServer().getMessageSender(); 36 37 // send an instance of a StopServer object 38 messageSender.send(new StopServer()); 39 40 // close the ObjectContainer 41 objectContainer.close(); 42 } 43 } 44 // end main 45}

Keywords:

start remote instance