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
IObjectServer db4oServer = Db4oFactory.OpenServer(FILE, PORT); 10
db4oServer.GrantAccess(USER, PASS); 11
12
// Using the messaging functionality to redirect all 13
// messages to this.processMessage 14
db4oServer.Ext().Configure().SetMessageRecipient(this); 15
try 16
{ 17
if (! stop) 18
{ 19
// wait forever until Close will change stop variable 20
Monitor.Wait(this); 21
} 22
} 23
catch (Exception e) 24
{ 25
Console.WriteLine(e.ToString()); 26
} 27
db4oServer.Close(); 28
} 29
}
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
SyncLock Me 07
Dim db4oServer As IObjectServer = Db4oFactory.OpenServer(FILE, PORT) 08
db4oServer.GrantAccess(User, PASS) 09
' Using the messaging functionality to redirect all 10
' messages to this.processMessage 11
db4oServer.Ext().Configure().SetMessageRecipient(Me) 12
Try 13
If Not [stop] Then 14
' wait forever until Close will change stop variable 15
Monitor.Wait(Me) 16
End If 17
Catch e As Exception 18
Console.WriteLine(e.ToString()) 19
End Try 20
db4oServer.Close() 21
End SyncLock 22
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, PASS); 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().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
Dim objectContainer As IObjectContainer = Nothing 17
Try 18
' connect to the server 19
objectContainer = Db4oFactory.OpenClient(HOST, PORT, User, PASS) 20
Catch e As Exception 21
Console.WriteLine(e.ToString()) 22
End Try 23
If Not objectContainer Is Nothing Then 24
' get the messageSender for the IObjectContainer 25
Dim messageSender As IMessageSender = objectContainer.Ext().Configure().GetMessageSender() 26
' send an instance of a StopServer object 27
messageSender.Send(New StopServer()) 28
' close the IObjectContainer 29
objectContainer.Close() 30
End If 31
End Sub