KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4odoc > f1 > clientserver > StartServer


1 package com.db4odoc.f1.clientserver;
2
3 import com.db4o.*;
4 import com.db4o.messaging.*;
5
6 /**
7  * starts a db4o server with the settings from {@link ServerConfiguration}.
8  * <br><br>This is a typical setup for a long running server.
9  * <br><br>The Server may be stopped from a remote location by running
10  * StopServer. The StartServer instance is used as a MessageRecipient and
11  * reacts to receiving an instance of a StopServer object.
12  * <br><br>Note that all user classes need to be present on the server
13  * side and that all possible Db4o.configure() calls to alter the db4o
14  * configuration need to be executed on the client and on the server.
15  */

16 public class StartServer
17     implements ServerConfiguration, MessageRecipient {
18   
19   /**
20    * setting the value to true denotes that the server should be closed
21    */

22   private boolean stop = false;
23   
24   /**
25    * starts a db4o server using the configuration from
26    * {@link ServerConfiguration}.
27    */

28   public static void main(String JavaDoc[] arguments) {
29     new StartServer().runServer();
30   }
31   // end main
32

33   /**
34    * opens the ObjectServer, and waits forever until close() is called
35    * or a StopServer message is being received.
36    */

37   public void runServer(){
38     synchronized(this){
39       ObjectServer db4oServer = Db4o.openServer(FILE, PORT);
40       db4oServer.grantAccess(USER, PASS);
41       
42       // Using the messaging functionality to redirect all
43
// messages to this.processMessage
44
db4oServer.ext().configure().setMessageRecipient(this);
45       
46       // to identify the thread in a debugger
47
Thread.currentThread().setName(this.getClass().getName());
48       
49       // We only need low priority since the db4o server has
50
// it's own thread.
51
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
52       try {
53           if(! stop){
54             // wait forever for notify() from close()
55
this.wait(Long.MAX_VALUE);
56           }
57       } catch (Exception JavaDoc e) {
58         e.printStackTrace();
59       }
60       db4oServer.close();
61     }
62   }
63   // end runServer
64

65   /**
66    * messaging callback
67    * @see com.db4o.messaging.MessageRecipient#processMessage(ObjectContainer, Object)
68    */

69   public void processMessage(ObjectContainer con, Object JavaDoc message) {
70     if(message instanceof StopServer){
71       close();
72     }
73   }
74   // end processMessage
75

76   /**
77    * closes this server.
78    */

79   public void close(){
80     synchronized(this){
81       stop = true;
82       this.notify();
83     }
84   }
85   // end close
86
}
87
Popular Tags