KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > f1 > chapter5 > StartServer


1 package com.db4o.f1.chapter5;
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   
32   /**
33    * opens the ObjectServer, and waits forever until close() is called
34    * or a StopServer message is being received.
35    */

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

67   public void processMessage(ObjectContainer con, Object JavaDoc message) {
68     if(message instanceof StopServer){
69       close();
70     }
71   }
72   
73   /**
74    * closes this server.
75    */

76   public void close(){
77     synchronized(this){
78       stop = true;
79       this.notify();
80     }
81   }
82 }
83
Popular Tags