Immutable Blocks Overview

Written on: July 24, 2018 6:10 pm

This sample shows you how to use the Immutable class. The immutable class represents persistent storage that can be accessed at the low level as an array of primitive types. This information will withstand reboots and application terminations. You can view Immutable data by typing the ‘nv’ command in the command line of a JNIOR. If using only one data type, you can express to create an immutable array of that data type, but if you want your array to have mixed data types inside it, you have to use a byte array. 

Using Immutable Blocks

This example shows the use of an array of longs. The long values in this application are date values that represent when the application was started. The long array is part of an Immutable block, therefore the application start times don’t go away until overwritten or manual removed. This application holds up to the last five application start times.

package immutableexample;

import com.integpg.system.Immutable;
import java.util.Date;

public class ImmutableExample {

    public static void main(String[] args) {
// get an array of long values that represent the last 5 boot times 
        long[] lastFiveBootTimes = Immutable.getLongArray("BootTimes");
// if the array does not already exist then create an array of 5 long values 
        if (lastFiveBootTimes == null) {
            lastFiveBootTimes = Immutable.createLongArray("BootTimes", 5);
        }
// shift the previous 4 boot times 
        for (int i = 4; i > 0; i--) {
            lastFiveBootTimes[i] = lastFiveBootTimes[i - 1];
        }
// assign the most recent boot time 
        lastFiveBootTimes[0] = System.currentTimeMillis();
// loop through our last five boot times and print them in a readable format 
        for (int i = 0; i < 5; i++) {
            long bootTime = lastFiveBootTimes[i];
            if (bootTime > 0) {
                System.out.println(new Date(bootTime));
            }
        }
    }
}

I put the built jar file of this example application into the JNIOR’s flash folder and ran it from the Web UI’s console tab. After it has successfully run, I run the application multiple times, and it shows each time it runs a new application start time added to its immutable array.

By | Updated On October 24, 2022 2:23 pm | No Comments | Categories: , | Tags:


 

INTEG Process Group, inc. © 2022

Real-Time
Mon - Fri, 8am - 4pm EST
P: 724-933-9350
PureChat
Always Available
Contact Form
sales@integpg.com
support@integpg.com

@integpg
@jniordev