Subscription
OpcUaSubscription It is a subscription container that manages a set of MonitoredItem (MI), and provides a list of callbacks onDataChanged (Java uses List<Consumer> instead of C# event +=).
Implement AutoCloseable, Must Use it within a try-with-resources block.
Subpage Redirect
- To create a subscription, please refer to Create a Subscription.
- Add / Add in Bulk MonitoredItem Please refer to Add MonitoredItem.
- Modify publishingInterval / lifetime / sampling—please refer to Modify.
- For "Disabled / Sampling / Reporting," please refer to SetMonitoringMode.
- To request a resend for a specific SeqNo, please refer to Republish.
- For information on the
onDataChangedfield and the threading model, please refer to DataChanged Callback. - For OPC UA alarms and event subscriptions, see Event Subscription.
One-Minute Overview
import com.darra.opcua.*;
import java.util.Arrays;
import java.util.List;
// Create
try (OpcUaSubscription sub = ua.createSubscription(500.0 /* publishingIntervalMs */)) {
// Data Change Callback (Using a List in Java)<Consumer> Alternative to C#'s `event +=`)
sub.onDataChanged.add(e -> System.out.printf(
"%s = %s (%s)%n", e.nodeId, e.valueString, e.status));
// Add MI
int h1 = sub.add("ns=2;s=Counter", null);
int h2 = sub.add("ns=2;s=Temperature", null);
// Add in Bulk
List<String> nodes = Arrays.asList("ns=2;s=T1", "ns=2;s=T2", "ns=2;s=T3");
List<int[]> results = sub.addMany(nodes, null, Enums.AttributeId.Value);
// Change the sampling interval MI for a single item
ModifyMonitoredItemResult mod = sub.modifyItem(h1, 100.0);
// Pause / Resume Collection
sub.setMode(h1, Enums.MonitoringMode.Disabled);
sub.setMode(h1, Enums.MonitoringMode.Reporting);
// Remove
sub.remove(h1);
Thread.sleep(60_000); // Let the callback run for a while
}
Subscribe to Lifecycle Events
All subscription-related internal events are routed through a unified ua.events.onAny channel, categorized as follows:
ua.events.onAny.add(e -> {
switch (e.category) {
case SubscriptionCreated: /* sub Create */ break;
case MonitoredItemAdded: /* mi Add */ break;
case MonitoredItemRemoved: /* mi Remove */ break;
case SubscriptionLost: /* Subscription Lost */ break;
case DataChange: /* Data Changes */ break;
}
});
For more information, see Session Events.
OpcUaSubscription Main API
| Methods | Note |
|---|---|
| add(nodeId, Consumer) | Add Order MI (Value, samplingInterval=-1) |
| add(nodeId, Consumer, AttributeId, samplingMs) | Add Order MI (Full Parameters) |
| addMany(nodeIds, Consumer, AttributeId) | Add in Bulk |
| remove(int handle) | Removal Order MI |
| removeByNodeId(String nodeId) | Press NodeId to remove all matching items MI |
| modify(double publishingMs, ...) | Change Subscription Settings |
| modifyItem(handle, samplingMs, queueSize, discardOldest) | Order Change MI Sampling |
| modifyMonitoredItems(List<MonitoredItemModifyRequest>) | Batch Edit |
| setMonitoringMode(MonitoringMode, List<Integer>) | Batch Cut Mode |
| setMode(handle, MonitoringMode) | Cut Order MI mode |
| setPublishingEnabled(boolean) | Subscribe or Unsubscribe to the Entire Series |
| republish(int seqNo) | Request Resend |
| pump(int timeoutMs) | Manually trigger a Publish once |
| getSession() / getSubscriptionId() / getPublishingIntervalMs() / size() / getMonitoredNodeIds() | Status Inquiry |
| close() | Release, Server-side DeleteSubscription |