In this example, the sample below shows how to simply configure an Ultra Messaging application how to configure and run in the "sequential" mode of operation.
When you create a context (lbm_context_create()) with the operational_mode set to sequential, the context thread is NOT created. It becomes the application's responsibility to donate a thread to UM by calling lbm_context_process_events() regularly, typically in a tight loop. Use Sequential mode for circumstances where your application wants control over the attributes of the context thread. For example, some applications raise the priority of the context thread so as to obtain more consistent latencies. In sequential mode, no separate thread is spawned when a context is created.
Important things to note when using sequential mode
This example very simply sets the attirbute to enable sequential mode, spawns a thread that spins on processing context events, while the main application thread waits forever.
There is one program source file:
Standard API's to create a context object. The important thing to note here is the fact that the operational_mode is being set to "sequential". Setting the operational_mode is required to take advantage of the sequential mode of operation.
00015 /* Initialization: create necessary UM objects. */ 00016 try { 00017 LBMContextAttributes ctxAttr = new LBMContextAttributes(); 00018 ctxAttr.setValue("operational_mode", "sequential"); 00019 ctx = new LBMContext(ctxAttr); 00020 } 00021 catch(LBMException ex) 00022 { 00023 System.Console.Error.WriteLine("Error initializing LBM objects: " + ex.Message); 00024 System.Environment.Exit(1); 00025 }
In this example, the application spawns a new thread whose sole purpose will be to process context events. The application could use this to set a higher thread priority, or possibly set the process affinity to offer better performance for message delivery. Notice the thread function is "seq_thread", which will be explained in the next block.
00027 SeqThread ThreadHandler = new SeqThread(ctx); 00028 ThreadHandler.start();
Here is where the function is declared that will be the thread that processes context events. This function very simply
calls the process events API, ensures there are no errors, and repeats.
00042 public class SeqThread
00043 {
00044 private LBMContext _ctx;
00045 private Thread myThread;
00046
00047 public SeqThread(LBMContext ctx)
00048 {
00049 _ctx = ctx;
00050 }
00051
00052 public void start()
00053 {
00054 myThread = new Thread(new ThreadStart(run));
00055 myThread.Start();
00056 }
00057
00058 public void run()
00059 {
00060 while (true)
00061 {
00062 try {
00063 _ctx.processEvents(500);
00064 }
00065 catch(LBMException ex)
00066 {
00067 System.Console.Error.WriteLine("Error processing context events: " + ex.Message);
00068 System.Environment.Exit(1);
00069 }
00070 }
00071 }
00072 }