Sometimes a client needs to send a special message to a server in order to tell the server to do something. The server may need to be signalled to perform a defragment or it may need to be signalled to shut itself down gracefully.
This is configured by calling setMessageRecipient(), passing the object that will process client-initiated messages.
01/// <summary> 02
/// opens the IObjectServer, and waits forever until Close() is called 03
/// or a StopServer message is being received. 04
/// </summary> 05
public void RunServer() 06
{ 07
lock(this) 08
{ 09
// Using the messaging functionality to redirect all 10
// messages to this.processMessage 11
IConfiguration configuration = Db4oFactory.NewConfiguration(); 12
configuration.ClientServer().SetMessageRecipient(this); 13
14
IObjectServer db4oServer = Db4oFactory.OpenServer(configuration, FileName, Port); 15
db4oServer.GrantAccess(User, Password); 16
17
try 18
{ 19
if (! stop) 20
{ 21
// wait forever until Close will change stop variable 22
Monitor.Wait(this); 23
} 24
} 25
catch (Exception e) 26
{ 27
Console.WriteLine(e.ToString()); 28
} 29
db4oServer.Close(); 30
} 31
}
01''' <summary> 02
''' opens the IObjectServer, and waits forever until Close() is called 03
''' or a StopServer message is being received. 04
''' </summary> 05
Public Sub RunServer() 06
' Using the messaging functionality to redirect all 07
' messages to this.processMessage 08
Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 09
configuration.ClientServer().SetMessageRecipient(Me) 10
SyncLock Me 11
Dim db4oServer As IObjectServer = Db4oFactory.OpenServer(configuration, File, Port) 12
db4oServer.GrantAccess(User, Password) 13
Try 14
If Not [stop] Then 15
' wait forever until Close will change stop variable 16
Monitor.Wait(Me) 17
End If 18
Catch e As Exception 19
Console.WriteLine(e.ToString()) 20
End Try 21
db4oServer.Close() 22
End SyncLock 23
End Sub
The message is received and processed by a processMessage() method:
01/// <summary> 02
/// messaging callback 03
/// see com.db4o.messaging.MessageRecipient#ProcessMessage() 04
/// </summary> 05
public void ProcessMessage(IObjectContainer con, object message) 06
{ 07
if (message is StopServer) 08
{ 09
Close(); 10
} 11
}
1''' <summary> 2
''' messaging callback 3
''' see com.db4o.messaging.MessageRecipient#ProcessMessage() 4
''' </summary> 5
Public Sub ProcessMessage(ByVal con As IObjectContainer, ByVal message As Object) Implements IMessageRecipient.ProcessMessage 6
If TypeOf message Is StopServer Then 7
Close() 8
End If 9
End Sub
Db4o allows a client to send an arbitrary signal or message to a server by sending a plain user object to the server. The server will receive a callback message, including the object that came from the client. The server can interpret this message however it wants.
01namespace Db4objects.Db4odoc.ClientServer 02
{ 03
/// <summary> 04
/// stops the db4o Server started with StartServer. 05
/// This is done by opening a client connection 06
/// to the server and by sending a StopServer object as 07
/// a message. StartServer will react in it's 08
/// processMessage method. 09
/// </summary> 10
public class StopServer : ServerConfiguration 11
{ 12
/// <summary> 13
/// stops a db4o Server started with StartServer. 14
/// </summary> 15
/// <exception cref="Exception" /> 16
public static void Main(string[] args) 17
{ 18
IObjectContainer objectContainer = null; 19
try 20
{ 21
// connect to the server 22
objectContainer = Db4oFactory.OpenClient(Host, Port, User, Password); 23
} 24
catch (Exception e) 25
{ 26
Console.WriteLine(e.ToString()); 27
} 28
29
if (objectContainer != null) 30
{ 31
// get the messageSender for the IObjectContainer 32
IMessageSender messageSender = objectContainer.Ext() 33
.Configure().ClientServer().GetMessageSender(); 34
35
// send an instance of a StopServer object 36
messageSender.Send(new StopServer()); 37
38
// close the IObjectContainer 39
objectContainer.Close(); 40
} 41
}
01Namespace Db4objects.Db4odoc.ClientServer 02
''' <summary> 03
''' stops the db4o Server started with StartServer. 04
''' This is done by opening a client connection 05
''' to the server and by sending a StopServer object as 06
''' a message. StartServer will react in it's 07
''' processMessage method. 08
''' </summary> 09
Public Class StopServer 10
Inherits ServerConfiguration 11
''' <summary> 12
''' stops a db4o Server started with StartServer. 13
''' </summary> 14
''' <exception cref="Exception" /> 15
Public Shared Sub Main(ByVal args As String()) 16
' get the messageSender for the IObjectContainer 17
Dim configuration As IConfiguration = Db4oFactory.NewConfiguration() 18
Dim messageSender As IMessageSender = configuration.ClientServer().GetMessageSender() 19
Dim objectContainer As IObjectContainer = Nothing 20
Try 21
' connect to the server 22
objectContainer = Db4oFactory.OpenClient(configuration, Host, Port, User, Password) 23
Catch e As Exception 24
Console.WriteLine(e.ToString()) 25
End Try 26
If Not objectContainer Is Nothing Then 27
' send an instance of a StopServer object 28
messageSender.Send(New StopServer()) 29
' close the IObjectContainer 30
objectContainer.Close() 31
End If 32
End Sub