Add MonitoredItem
- First, create a subscription. See Create a Subscription.
- For the DataChanged callback field, see DataChanged Callback.
- To change the sampling parameters, see Modify.
sub.add(...)
public int add(String nodeId, Consumer<DataChangeEventArgs> onChange);
public int add(String nodeId, Consumer<DataChangeEventArgs> onChange,
Enums.AttributeId attribute, double samplingIntervalMs);
| Parameters | Default | Note |
|---|---|---|
| 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). |
| attribute | Value (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:
| Field | Type | Note |
|---|---|---|
| monitoredItemHandle | int | Triggered MI Handle |
| nodeId | String | It's about time MI NodeId |
| valueString | String | String representation of a value (pre-extracted, cross-thread safe) |
| dataTypeName | String | Names of built-in data type enumerations (e.g., Double) |
| status | Enums.StatusCode | DataValue Status |
| sourceTimestampFt | long | Data source FileTime, 0 = null |
| serverTimestampFt | long | Server FileTime, 0 = null |
| arrivedAtUtc | Instant | Arrival time recorded on the Java side |
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).