Skip to main content

Add MonitoredItem

Prerequisite Reading

sub.add(...)​

public int add(String nodeId, Consumer<DataChangeEventArgs> onChange);
public int add(String nodeId, Consumer<DataChangeEventArgs> onChange,
Enums.AttributeId attribute, double samplingIntervalMs);
ParametersDefaultNote
nodeId—Monitored NodeId
onChange—A single, exclusive MI callback will also trigger the global callbacks listed in sub.onDataChanged (passing null skips the exclusive callback).
attributeValue (13)Monitored Attributes
samplingIntervalMs-1 (Subscribe)Server sampling interval. 0 = As soon as possible

Returns: int MonitoredItem handle (local handle, used for subsequent modify / remove operations)

Exception: OpcUaException (BadNodeIdUnknown etc.)

int h  = sub.add("ns=2;s=Temperature", e ->
System.out.println(e.nodeId + " = " + e.valueString));
int h2 = sub.add("ns=2;s=Pressure", null,
Enums.AttributeId.Value, 100.0);

sub.addMany Add in Bulk​

Add RPC multiple items at once MI; in a loop, each item add is processed one at a time MI RPC:

public List<int[]> addMany(List<String> nodeIds,
Consumer<DataChangeEventArgs> onChange,
Enums.AttributeId attribute);

Return: List<int[]>, where each item is a tuple { handle, statusCode }.

List<String> nodes = java.util.stream.IntStream.rangeClosed(1, 100)
.mapToObj(i -> "ns=2;s=T" + i)
.toList();

List<int[]> results = sub.addMany(nodes, e -> {
// Shared callbacks, distinguished e.monitoredItemHandle by e.nodeId /
System.out.println(e.nodeId + " = " + e.valueString);
}, Enums.AttributeId.Value);

for (int i = 0; i < nodes.size(); i++) {
int handle = results.get(i)[0];
int status = results.get(i)[1];
if (status == 0) {
System.out.println(" " + nodes.get(i) + " -> handle=" + handle);
} else {
System.out.println(" " + nodes.get(i) + " FAILED: " + Enums.StatusCode.fromCode(status));
}
}

Performance: 100 calls MI take approximately addMany 1 cycle RPC; the loop runs add approximately 100 times RPC. The actual time taken varies depending on the network and server; please test it in your own environment.

DataChangeEventArgs Fields​

Callback parameters OpcUaSubscription.DataChangeEventArgs:

FieldTypeNote
monitoredItemHandleintTriggered MI Handle
nodeIdStringIt's about time MI NodeId
valueStringStringString representation of a value (pre-extracted, cross-thread safe)
dataTypeNameStringNames of built-in data type enumerations (e.g., Double)
statusEnums.StatusCodeDataValue Status
sourceTimestampFtlongData source FileTime, 0 = null
serverTimestampFtlongServer FileTime, 0 = null
arrivedAtUtcInstantArrival time recorded on the Java side
Key Points

valueString / dataTypeName: When retrieved, it is a regular Java value and is thread-safe. There is no need to manually clean up the callback; simply OpcUaSubscription release it.

Remove​

sub.remove(handle);
sub.removeByNodeId("ns=2;s=Temperature"); // Press NodeId to remove all matching items

Monitoring Different Attributes​

In addition to the default "Value" metric, you can also monitor "Status" and "Quality":

sub.add("ns=2;s=T1", null, Enums.AttributeId.Value, -1);
sub.add("ns=2;s=T1", null, Enums.AttributeId.NodeClass, -1); // Focus only on changes in node categories

DataChangeFilter (Dead Zone)​

Currently, no filter is set by the SDK by default (any changes are pushed). If you need an absolute or percentage deadband, use the low-level API (this API will be exposed in future versions).

Next Step​