01: package com.db4o.f1.chapter5;
02:
03: import com.db4o.*;
04: import com.db4o.messaging.*;
05:
06: /**
07: * starts a db4o server with the settings from {@link ServerConfiguration}.
08: * <br><br>This is a typical setup for a long running server.
09: * <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 implements ServerConfiguration,
17: 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[] 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().clientServer()
44: .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 e) {
58: e.printStackTrace();
59: }
60: db4oServer.close();
61: }
62: }
63:
64: /**
65: * messaging callback
66: * @see com.db4o.messaging.MessageRecipient#processMessage(ObjectContainer, Object)
67: */
68: public void processMessage(ObjectContainer con, Object message) {
69: if (message instanceof StopServer) {
70: close();
71: }
72: }
73:
74: /**
75: * closes this server.
76: */
77: public void close() {
78: synchronized (this ) {
79: stop = true;
80: this.notify();
81: }
82: }
83: }
|