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:
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02
namespace Db4objects.Db4odoc.ClientServer 03
{ 04
/// <summary> 05
/// Configuration used for StartServer and StopServer. 06
/// </summary> 07
public class ServerConfiguration 08
{ 09
/// <summary> 10
/// the host to be used. 11
/// 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
/// </summary> 14
public const string Host = "localhost"; 15
16
/// <summary> 17
/// the database file to be used by the server. 18
/// </summary> 19
public const string FileName = "reference.db4o"; 20
21
/// <summary> 22
/// the port to be used by the server. 23
/// </summary> 24
public const int Port = 0xdb40; 25
26
/// <summary> 27
/// the user name for access control. 28
/// </summary> 29
public const string User = "db4o"; 30
31
/// <summary> 32
/// the pasword for access control. 33
/// </summary> 34
public const string Password = "db4o"; 35
} 36
}
01' Copyright (C) 2007 db4objects Inc. http://www.db4o.com 02
Namespace Db4objects.Db4odoc.ClientServer 03
''' <summary> 04
''' Configuration used for StartServer and StopServer. 05
''' </summary> 06
Public Class ServerConfiguration 07
''' <summary> 08
''' the host to be used. 09
''' If you want to run the client server examples on two computers, 10
''' enter the computer name of the one that you want to use as server. 11
''' </summary> 12
Public Const Host As String = "localhost" 13
14
''' <summary> 15
''' the database file to be used by the server. 16
''' </summary> 17
Public Const File As String = "reference.db4o" 18
19
''' <summary> 20
''' the port to be used by the server. 21
''' </summary> 22
Public Const Port As Integer = &HDB40 23
24
''' <summary> 25
''' the user name for access control. 26
''' </summary> 27
Public Const User As String = "db4o" 28
29
''' <summary> 30
''' the pasword for access control. 31
''' </summary> 32
Public Const Password As String = "db4o" 33
34
End Class 35
End Namespace
Now we'll create the server:
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02
using System; 03
using System.Threading; 04
05
using Db4objects.Db4o; 06
using Db4objects.Db4o.Config; 07
using Db4objects.Db4o.Messaging; 08
09
namespace Db4objects.Db4odoc.ClientServer 10
{ 11
/// <summary> 12
/// starts a db4o server with the settings from ServerConfiguration. 13
/// This is a typical setup for a long running server. 14
/// The Server may be stopped from a remote location by running 15
/// StopServer. The StartServer instance is used as a MessageRecipient 16
/// and reacts to receiving an instance of a StopServer object. 17
/// Note that all user classes need to be present on the server side 18
/// and that all possible calls to alter the db4o 19
/// configuration need to be executed on the client and on the server. 20
/// </summary> 21
public class StartServer : ServerConfiguration, IMessageRecipient 22
{ 23
/// <summary> 24
/// setting the value to true denotes that the server should be closed 25
/// </summary> 26
private bool stop = false; 27
28
/// <summary> 29
/// starts a db4o server using the configuration from 30
/// ServerConfiguration. 31
/// </summary> 32
public static void Main(string[] arguments) 33
{ 34
new StartServer().RunServer(); 35
} 36
// end Main 37
38
/// <summary> 39
/// opens the IObjectServer, and waits forever until Close() is called 40
/// or a StopServer message is being received. 41
/// </summary> 42
public void RunServer() 43
{ 44
lock(this) 45
{ 46
// Using the messaging functionality to redirect all 47
// messages to this.processMessage 48
IConfiguration configuration = Db4oFactory.NewConfiguration(); 49
configuration.ClientServer().SetMessageRecipient(this); 50
51
IObjectServer db4oServer = Db4oFactory.OpenServer(configuration, FileName, Port); 52
db4oServer.GrantAccess(User, Password); 53
54
try 55
{ 56
if (! stop) 57
{ 58
// wait forever until Close will change stop variable 59
Monitor.Wait(this); 60
} 61
} 62
catch (Exception e) 63
{ 64
Console.WriteLine(e.ToString()); 65
} 66
db4oServer.Close(); 67
} 68
} 69
// end RunServer 70
71
/// <summary> 72
/// messaging callback 73
/// see com.db4o.messaging.MessageRecipient#ProcessMessage() 74
/// </summary> 75
public void ProcessMessage(IMessageContext context, object message) 76
{ 77
if (message is StopServer) 78
{ 79
Close(); 80
} 81
} 82
// end ProcessMessage 83
84
/// <summary> 85
/// closes this server. 86
/// </summary> 87
public void Close() 88
{ 89
lock(this) 90
{ 91
stop = true; 92
Monitor.PulseAll(this); 93
} 94
} 95
// end Close 96
} 97
}
01' Copyright (C) 2007 db4objects Inc. http://www.db4o.com 02
Imports System 03
Imports System.Threading 04
05
Imports Db4objects.Db4o 06
Imports Db4objects.Db4o.Config 07
Imports Db4objects.Db4o.Messaging 08
09
Namespace Db4objects.Db4odoc.ClientServer 10
''' <summary> 11
''' starts a db4o server with the settings from ServerConfiguration. 12
''' This is a typical setup for a long running server. 13
''' The Server may be stopped from a remote location by running 14
''' StopServer. The StartServer instance is used as a MessageRecipient 15
''' and reacts to receiving an instance of a StopServer object. 16
''' Note that all user classes need to be present on the server side 17
''' and that all possible calls to alter the db4o 18
''' configuration need to be executed on the client and on the server. 19
''' </summary> 20
Public Class StartServer 21
Inherits ServerConfiguration 22
Implements IMessageRecipient 23
''' <summary> 24
''' setting the value to true denotes that the server should be closed 25
''' </summary> 26
Private [stop] As Boolean = False 27
28
''' <summary> 29
''' starts a db4o server using the configuration from 30
''' ServerConfiguration. 31
''' </summary> 32
Public Shared Sub Main(ByVal arguments As String()) 33
Dim server As New StartServer 34
server.RunServer() 35
End Sub 36
' end Main 37
38
''' <summary> 39
''' opens the IObjectServer, and waits forever until Close() is called 40
''' or a StopServer message is being received. 41
''' </summary> 42
Public Sub RunServer() 43
' Using the messaging functionality to redirect all 44
' messages to this.processMessage 45
Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 46
configuration.ClientServer().SetMessageRecipient(Me) 47
SyncLock Me 48
Dim db4oServer As IObjectServer = Db4oFactory.OpenServer(configuration, File, Port) 49
db4oServer.GrantAccess(User, Password) 50
Try 51
If Not [stop] Then 52
' wait forever until Close will change stop variable 53
Monitor.Wait(Me) 54
End If 55
Catch e As Exception 56
Console.WriteLine(e.ToString()) 57
End Try 58
db4oServer.Close() 59
End SyncLock 60
End Sub 61
' end RunServer 62
63
''' <summary> 64
''' messaging callback 65
''' see com.db4o.messaging.MessageRecipient#ProcessMessage() 66
''' </summary> 67
Public Sub ProcessMessage(ByVal context As IMessageContext, ByVal message As Object) Implements IMessageRecipient.ProcessMessage 68
If TypeOf message Is StopServer Then 69
Close() 70
End If 71
End Sub 72
' end ProcessMessage 73
74
''' <summary> 75
''' closes this server. 76
''' </summary> 77
Public Sub Close() 78
SyncLock Me 79
[stop] = True 80
Monitor.PulseAll(Me) 81
End SyncLock 82
End Sub 83
' end Close 84
End Class 85
End Namespace
And last but not least, the client that stops the server.
01/* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com */ 02
using System; 03
using Db4objects.Db4o; 04
using Db4objects.Db4o.Messaging; 05
06
namespace Db4objects.Db4odoc.ClientServer 07
{ 08
/// <summary> 09
/// stops the db4o Server started with StartServer. 10
/// This is done by opening a client connection 11
/// to the server and by sending a StopServer object as 12
/// a message. StartServer will react in it's 13
/// processMessage method. 14
/// </summary> 15
public class StopServer : ServerConfiguration 16
{ 17
/// <summary> 18
/// stops a db4o Server started with StartServer. 19
/// </summary> 20
/// <exception cref="Exception" /> 21
public static void Main(string[] args) 22
{ 23
IObjectContainer objectContainer = null; 24
try 25
{ 26
// connect to the server 27
objectContainer = Db4oFactory.OpenClient(Host, Port, User, Password); 28
} 29
catch (Exception e) 30
{ 31
Console.WriteLine(e.ToString()); 32
} 33
34
if (objectContainer != null) 35
{ 36
// get the messageSender for the IObjectContainer 37
IMessageSender messageSender = objectContainer.Ext() 38
.Configure().ClientServer().GetMessageSender(); 39
40
// send an instance of a StopServer object 41
messageSender.Send(new StopServer()); 42
43
// close the IObjectContainer 44
objectContainer.Close(); 45
} 46
} 47
// end Main 48
} 49
}
01' Copyright (C) 2007 db4objects Inc. http://www.db4o.com 02
Imports System 03
04
Imports Db4objects.Db4o 05
Imports Db4objects.Db4o.Config 06
Imports Db4objects.Db4o.Messaging 07
08
Namespace Db4objects.Db4odoc.ClientServer 09
''' <summary> 10
''' stops the db4o Server started with StartServer. 11
''' This is done by opening a client connection 12
''' to the server and by sending a StopServer object as 13
''' a message. StartServer will react in it's 14
''' processMessage method. 15
''' </summary> 16
Public Class StopServer 17
Inherits ServerConfiguration 18
''' <summary> 19
''' stops a db4o Server started with StartServer. 20
''' </summary> 21
''' <exception cref="Exception" /> 22
Public Shared Sub Main(ByVal args As String()) 23
' get the messageSender for the IObjectContainer 24
Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 25
Dim messageSender As IMessageSender = configuration.ClientServer().GetMessageSender() 26
Dim objectContainer As IObjectContainer = Nothing 27
Try 28
' connect to the server 29
objectContainer = Db4oFactory.OpenClient(configuration, Host, Port, User, Password) 30
Catch e As Exception 31
Console.WriteLine(e.ToString()) 32
End Try 33
If Not objectContainer Is Nothing Then 34
' send an instance of a StopServer object 35
messageSender.Send(New StopServer()) 36
' close the IObjectContainer 37
objectContainer.Close() 38
End If 39
End Sub 40
'end Main 41
End Class 42
End Namespace
Keywords:
start remote instance