There is one program source file:
The example source code is organized as a Java class.
In order to configure a UM context, an attributes object must first be created:
00024 LBMContextAttributes ctxAttr = new LBMContextAttributes(); 00025 ctxAttr.setObjectRecycler(objRec, null); 00026 00027 /* Modify resolver address by setting attributes */ 00028 ctxAttr.setValue("resolver_multicast_address", "224.10.11.12");
When an attributes object is created, a snapshot of the current configuration is loaded. In this example, the attributes contained in ctxAttr will be default. This is not always the case. If a configuration file has been loaded via the API, or set via environmental variables, these values may not be default.
These attributes will be used to configure a new context:
00030 ctx = new LBMContext(ctxAttr);
The constructor is passed a reference to the attributes object, letting the UM library know that it should configure the new context with these specific attributes.
Sources are configured slightly differently. To create a source, a topic ojbect is required. So instead of passing the attributes object directly into the sources constructor, it is used to initialized a topic object:
00032 srcAttr = new LBMSourceAttributes(); 00033 srcAttr.setObjectRecycler(objRec, null); 00034 00035 /* Set configuration value using strings */ 00036 srcAttr.setValue("transport", "lbtrm"); 00037 00038 /* The Java API only accepts string values. ints, for example */ 00039 /* must be converted to strings */ 00040 desPort = 14001; 00041 srcAttr.setValue("transport_lbtrm_destination_port", Integer.toString(desPort)); 00042 00043 /* Create topic for the first source with configured attributes */ 00044 topic1 = new LBMTopic(ctx, "test.topic1", srcAttr);This topic object is then used to initialize a source with the given set of attributes.
00045 src1 = new LBMSource(ctx, topic1);
In this example a second source is configured to use a different transport_lbtrm_destination_port. It is common for topics to be programatically configured to use different settings, and this example demonstrates how the same source attributes object can be used to configure 2 different sources with their own unique set of attributes:
00047 desPort = 14002; 00048 srcAttr.setValue("transport_lbtrm_destination_port", Integer.toString(desPort)); 00049 00050 /* Create second topic and source using modified configuration values */ 00051 topic2 = new LBMTopic(ctx, "test.topic2", srcAttr); 00052 src2 = new LBMSource(ctx, topic2);
In object oriented languages, UM uses slightly different aproach to cleaning up attributes. Due to shared resources with the unerdlying native C library, the .close method is used to clean up native memory allocation. *Attributes objects are different from other UM objects in that they do not have .close() methods. In order to clean them up, they can be assigned an LBMObjectRecycler which can be .close()d. This is done via the setObjectRecycler method as shown below:
00062 objRec.close(); 00063 src2.close(); 00064 src1.close(); 00065 ctx.close();