Subscription Resumes Automatically
Overview
After the session reconnects, the server-side subscription may still be alive or may already have been cleaned up. The SDK uses a two-step process:
- transferSubscriptions — SubscriptionId Transfer the old session to the new one
- Recreate — If the transfer fails, rebuild Sub + All using the locally cached monitoring item configuration. MI
No manual processing is required on the business side.
API
| Members / Events | Category | Reading and Writing | Note |
|---|---|---|---|
| ua.transferSubscriptions(subscriptionIds, sendInitialValues) | Methods | Write | Transfer Subscription, Returns List<Enums.StatusCode> |
| ua.events.onSubscriptionLost | Event Channel | Read | List<Consumer<SubscriptionLostEventArgs>>.add(...) Subscription lost |
| ua.events.onSubscriptionRestored | Event Channel | Read | List<Consumer<OpcUaEventEntry>>.add(...) Subscription Restored |
Code Example
import com.darra.opcua.*;
import java.util.List;
try (OpcUaSession ua = new OpcUaSession("opc.tcp://localhost:4840")) {
ua.events.onSubscriptionLost.add(e ->
System.out.println("订阅丢失: subId=" + e.subscriptionId
+ " willReconnect=" + e.willReconnect));
ua.events.onSubscriptionRestored.add(e ->
System.out.println("订阅已恢复: " + e.source));
ua.connect();
// Create "sub +" MI; remains unchanged throughout
OpcUaSubscription sub = ua.createSubscription(500);
int mi = sub.add("ns=2;s=Temp", null, Enums.AttributeId.Value, 200,
Enums.MonitoringMode.Reporting, 10, true,
new MonitoringFilter.Filter(MonitoringFilter.Trigger.STATUS_VALUE,
Enums.DeadbandType.Absolute, 0.5));
sub.onDataChanged.add(args ->
System.out.println(args.valueString + " @ " + args.sourceTimestampFt));
// Unplug the network cable for 30 seconds, then check the logs:
// reconnecting → reconnected → onSubscriptionRestored
// Data is automatically continued
// Manual transfer
List<Enums.StatusCode> rcs = ua.transferSubscriptions(
List.of(sub.getSubscriptionId()), true);
}
Recovery Process
Reconnect succeeded (ua.events.onReconnected)
↓
For each local Subscription (handled automatically by the SDK):
1. transferSubscriptions(oldSubId, true)
├─ Good → onSubscriptionRestored
└─ Failed → go to 2
2. Subscription_Create gets a new sub
3. Rebuild MI one by one per the local miCache
- Replay SamplingInterval / Mode / QueueSize / DiscardOldest
- DataChangeFilter is cached only, not passed through; the business side must re-set it
- Triggering Links are not in the cache; the business side must re-set them
4. onSubscriptionRestored
Event subscriptions go through OpcUaEventSubscription.recreateAsync instead, re-attaching the Event MI per the cached targetNodeId
Cached Fields
miCache Retain: NodeId / AttributeId / SamplingInterval / Mode / QueueSize / DiscardOldest / filter. Event subscriptions also cache targetNodeId + filter.
Play back everything during recovery; the mi handle will change: the reconciliation key in the callback is args.monitoredItemHandle(int); after reconstruction the old handle becomes invalid, so you must look up the new handle by NodeId and then reconcile.
Best Practices
- For reconciliation use
args.monitoredItemHandle; do not persist the mi handle (it will change after reconstruction) - Monitoring
ua.events.onSubscriptionRestored - Do not manually recreate it
onSubscriptionLostin [...]; [...], is already handling SDK it. - To change parameters, use
sub.modifyItem(...)/sub.setMode(...); do not delete and MI rebuild.
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| TransferSubscriptions | transfer_subscriptions | transferSubscriptions | TransferSubscriptions | transfer_subscriptions | DarraUa_Session_TransferSubscriptions |