Example index

Sequential Mode Example

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:

Program explanation: SequentialMode.java

Initialize and Create Context

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.

00010          /* Initialization: create necessary UM objects. */
00011          try {
00012              LBMContextAttributes ctxAttr = new LBMContextAttributes();
00013              ctxAttr.setValue("operational_mode", "sequential");
00014              ctx = new LBMContext(ctxAttr);
00015          }
00016          catch(LBMException ex)
00017          {
00018              System.err.println("Error initializing LBM objects: " + ex.toString());
00019              System.exit(1);
00020          }

Spawn a New Thread

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.

00022          (new Thread(new SeqThread(ctx))).start();

Sequential Context Thread

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.

00036      public static class SeqThread implements Runnable
00037      {
00038          private LBMContext _ctx;
00039  
00040          public SeqThread(LBMContext ctx)
00041          {
00042              _ctx = ctx;
00043          }
00044  
00045          public void run()
00046          {
00047              while (true)
00048              {
00049                  try {
00050                      _ctx.processEvents(500);
00051                  }
00052                  catch(LBMException ex)
00053                  {
00054                      System.err.println("Error processing context events: " + ex.toString());
00055                      System.exit(1);
00056                  }
00057              }
00058          }
00059      }