Skip to main content

DataChanged Callback

OpcUaSubscription.onDataChanged is List<Consumer<DataChangeEventArgs>> (CopyOnWriteArrayList), triggered by a push from the server when data MI changes.

public final List<Consumer<DataChangeEventArgs>> onDataChanged;
Related Events

DataChangeEventArgs​

FieldTypeNote
monitoredItemHandleintTriggered MI Local Handle
nodeIdStringIt's about time MI NodeId
valueStringStringString representation of the value (extracted synchronously within the native Publish thread; thread-safe)
dataTypeNameStringNames of built-in data type enumerations (e.g., Double, Int32)
statusEnums.StatusCodeDataValue Status
sourceTimestampFtlongData source FileTime, 0 = null
serverTimestampFtlongServer FileTime, 0 = null
arrivedAtUtcInstantArrival time recorded on the Java side

Design Trade-offs​

In the past, raw OpcUaDataValue pointers were exposed during callbacks, but because the native memory lifecycle spans threads, this often caused crashes due to use-after-free conditions. Now, SDK within the Publish thread,Synchronously extract all fields For flat Java objects (strings, enums, and longs FileTime), there is zero risk when using them across threads, but the trade-off is that you cannot perform OpcUaVariant advanced operations on the primitives.

If you need the original value ( OpcUaVariant e.g., for complex parsing ExtensionObject), use it to re-read the data synchronously ua.read(e.nodeId) in the callback—this is more reliable than managing the native lifecycle.

Examples​

Simple Log​

sub.onDataChanged.add(e ->
System.out.printf("%s = %s (%s) ft=%d%n",
e.nodeId, e.valueString, e.dataTypeName, e.sourceTimestampFt));

Write a document​

final Object writeLock = new Object();
sub.onDataChanged.add(e -> {
synchronized (writeLock) {
try (java.io.FileWriter fw = new java.io.FileWriter("data.csv", true)) {
long ft = e.sourceTimestampFt;
Instant t = (ft > 0)
? Instant.ofEpochMilli((ft - 116444736000000000L) / 10000L)
: Instant.now();
fw.write(t + "," + e.nodeId + "," + e.valueString + "\n");
} catch (Exception ex) { /* Log */ }
}
});

UI Binding (Swing)​

sub.onDataChanged.add(e -> {
SwingUtilities.invokeLater(() -> {
labelTemp.setText(e.valueString);
labelStatus.setForeground(e.status == Enums.StatusCode.Good
? Color.GREEN : Color.RED);
});
});
You must use invokeLater.

Otherwise, cross-thread UI operations will trigger a Swing model exception (modifying components outside EDT of the model).

JavaFX uses Platform.runLater(() -> { ... }) equivalents.

Thread Model​

  • Callback at C-layer Publish Thread Execution: Single-threaded and serial (no concurrency within the same subscription)
  • In the callbackDo not perform long operations (>100 ms), otherwise the Publish queue will be blocked
  • Exceptions thrown in callbacks are SDK swallowed (the try-catch block is ignored), It won't propagate to native
  • Cross-subscription callbacks run on different Publish threads and may occur concurrently—if sharing data, implement your own locking mechanism.
  • onDataChanged Use CopyOnWriteArrayList, add(consumer) / remove(consumer) thread-safe; can be dynamically added or removed during distribution

Relationship with ua.events.onAny​

// Option A: Single Subscription Tier
sub.onDataChanged.add(e -> /* ... */);

// Method B: Global Unified Channel (All Subscriptions Consolidated)
ua.events.onAny.add(entry -> {
if (entry.category == Enums.OpcUaEventCategory.DataChange) {
// entry.source = nodeId, entry.message = valueString
}
});

Both events will be triggered for the same data point; just choose one to use.

Next Step​