“Message Received”

0

Written on Thursday, December 06, 2007 by Edwin Sanchez

Db4o has a client-server feature called Messaging. It’s so cool it allows you to execute a process remotely. It has less overhead and the execution is asynchronous. I find it so useful that something like this will fit in with my current project. I need to do something like this:

  • The user will upload a text file thru a browser
    With the sounds of this, it is an ASP.Net application. The text file is delimited and it can be translated into columns of data.
  • After the file has been uploaded, a process should execute to transfer the data in the text file to my db4o database
    Each column of data in the text file will go to an object repository

Sounds simple. However, the text file is several MBs big so it will be a long running process. The user will be very bored uploading and processing the data. So I came up with the following additional requirements:

  • Once the text file is uploaded, an asynchronous operation needs to be done so the processing will actually happen in the server where the db4o database is located.
  • The processing logs certain information so that when the user inquires of it, he can see it in the browser page that displays the current status of the processing.
  • The user can close the browser anytime after the processing starts or go to another page while the processing is being done. He can inquire later on a page where he can see the current status. Wow! This is something that will make the user more productive.
  • In the server, I would like to know when a processing has been triggered and the current status of the processing so I cannot just shutdown the db4o server or do something bad for it.
  • I can also do the uploading of the text file and processing in the server application.

Now this is something more complicated. It made me excited to do this so I started the research. I came thru the db4o reference documentation on Client Server regarding Out-of-Band Signaling. I read it and decided that this is the thing I need. How does it work? Figure 1 shows a high-level diagram of how I did it. I’m used to do distributed apps in my Visual Basic 6 days so I tried something distributed in this case. Explaining the architecture further is out of the scope of this topic.






Now for some explaining to do. I will just cover the part where I’m actually sending and receiving the message. I will make it simple thru explaining with code blocks. I can’t post my entire code for some restrictions with my current employer. For a working example, you can go to the reference section of the db4o documentation. First, the objective of messaging is straightforward: The client will tell the server to execute a process. The message being sent is actually any object that is storable in db4o. With this you create a class in C# that you will use to be sent to the server.

Below is the class that we will use as our message class:

public class MyMessage
{
  private string Message;
  public MyMessage(string message)
  {
    Message = message;
  }
  get
  {
    return Message;
  }
  set
  {
    Message = value;
  }
}


In order to receive messages, you will need a class that will implement the IMessageRecipient. This can be on the same namespace where the server application resides.

public class MyMessageRecipient: IMessageRecipient
{
  void IMessageRecipient.ProcessMessage(IObjectContainer con, object message)
  {
   if (message is MyMessage) // MyMessage is the class we defined above
   {
     // call routines for your long processing and logging any status
   }
  }
}

Before a server can actually receive messages, it needs to be configured that way. There should be something like the code below be implemented in the server application when the server will be started.

IConfiguration config = Db4oFactory.NewConfiguration();
IObjectServer server;
Config.ClientServer().SetMessageRecipient(new MyMessageRecipient())
server = Db4oFactory.OpenServer(config, [yapfile], [port]);
server.GrantAccess([userid], [password]);
// some other code here, if desired

Now, sending messages is simple enough. You can implement something like the code below in the Business Rules layer.

IObjectContainer db = Db4oFactory.OpenClient([hostname], [port]);
IMessageSender sender = db.Ext().Configure().ClientServer().GetMessageSender();
MyMessage msg = new MyMessage(“Some Message”);
sender.Send(msg);

// some other code here, if desired

That’s how it is. You can try it yourself and see how this is a very good feature of db4o when dealing with client-server. Happy Messaging!

If you enjoyed this post Subscribe to our feed

No Comment

Post a Comment