Skip to main content

Event Subscription

OPC UA events (Alarms & Conditions) are subscribed to through subscribeEvents. These are OPC UA protocol events and are not the same thing as the SDK's internal ua.events.*.

Difference
  • ua.subscribeEvents(...) — OPC UA Protocol Events (Alarms / Conditions, Server-Side Push Notifications, This Section)
  • ua.events.* — SDK Internal Events (Session / Subscription Lifecycle, Protocol-Independent)

Signature​

public OpcUaEventSubscription subscribeEvents();
public OpcUaEventSubscription subscribeEvents(String nodeId);
public OpcUaEventSubscription subscribeEvents(String nodeId,
OpcUaSubscription existingSubscription);
ParametersDefaultNote
nodeIdnull (Server, i=2253)Event Source Node
existingSubscriptionnullReuse an existing subscription; if null, create a new one internally (1000ms interval)

OpcUaEventSubscription​

MembersNote
onEventArrivedList<Consumer<EventArrivedEventArgs>> Event Arrival Callback
close()Remove this event MI; if a new container was created internally, it will also be destroyed.

Implement AutoCloseable, Must Use it within a try-with-resources block.

EventArrivedEventArgs​

FieldTypeNote
severityintSeverity 1–1000
messageStringEvent Text
sourceNameStringEvent Source Name
timeFtlongTime of the incident (FileTime, 0 = null)
eventTypeStringEvent Type NodeId (Currently always null; not SelectClauses included in the underlying structure EventType)
arrivedAtUtcInstantArrival time recorded on the Java side

Usage​

Default Subscription Server Node​

try (OpcUaEventSubscription evSub = ua.subscribeEvents()) {
evSub.onEventArrived.add(e -> {
System.out.printf("[%d] %s: %s%n", e.severity, e.sourceName, e.message);
long ft = e.timeFt;
Instant t = (ft > 0)
? Instant.ofEpochMilli((ft - 116444736000000000L) / 10000L)
: Instant.now();
System.out.println(" Time=" + t);
});

Thread.sleep(java.time.Duration.ofMinutes(5).toMillis());
}

Subscribe to events from a specific node​

try (OpcUaEventSubscription evSub = ua.subscribeEvents("ns=2;s=Line1")) {
// Line1 It must be EventNotifier
evSub.onEventArrived.add(e -> /* ... */);
Thread.sleep(60_000);
}

Reusing an Existing Subscription Container​

try (OpcUaSubscription sub = ua.createSubscription(500.0)) {
sub.add("ns=2;s=Counter", null); // Data Subscriptions

// Attach the Event to MI the same container
try (OpcUaEventSubscription evSub = ua.subscribeEvents("ns=2;s=Line1", sub)) {
// evSub.close Remove only the Event MI; do not destroy the sub
evSub.onEventArrived.add(e -> /* ... */);
Thread.sleep(60_000);
}
}

EventNotifier​

Only nodes with a non-0 EventNotifier attribute (12) can generate events. By default, Server(i=2253) acts as an EventNotifier. Whether a custom node can act as an event source depends on the server implementation.

EventFilter (Advanced)​

OPC UA EventFilter can filter by EventType / Severity / SourceNode; the SDK currently subscribes to BaseEventType by default and selects the 4 standard fields (Severity / Message / SourceName / Time). Richer filtering arrives in a later version.

Alarm Operations (Acknowledge / Confirm)​

Upon receiving an AlarmConditionType event, the operator is typically required to acknowledge it. By call Acknowledgment Method for Calling Alarm Nodes:

try (OpcUaVariant evtId = new OpcUaVariant().setString("event-id-bytes-as-string");
OpcUaVariant cmnt = new OpcUaVariant().setString("Operator confirmed")) {
ua.call(conditionId, conditionId + ".Acknowledge", evtId, cmnt);
}
Subscription Lifecycle

Event subscriptions remain active for as long as they are referenced OpcUaEventSubscription; releasing them prematurely will cause event callbacks to stop, and they are released close() when no longer in use.

Next Step​