Automatic Reconnection
Overview
Sessions expire on the OPC UA Server during maintenance restarts or network jitter. Automatic reconnection is performed by the lower stack; the switch is the constructor parameter autoReconnect:
new OpcUaSession(..., autoReconnect = true, ...)Enable Auto-Reconnect- The stack automatically rebuilds after detecting a channel/session failure SecureChannel + Session
- State changes are forwarded via the SessionState callback to
events.onReconnecting/events.onReconnected
Normal read / write / browse / method calls / history read failuresSell immediately OpcUaException, no reconnection or retry; the Safe variant simply wraps the exception in the return value status, and likewise, does not reconnect or retry. Reconnection is handled entirely by the underlying stack in the background and is independent of RPC the success or failure of a single attempt.
The business side does not need to handle the reconnection process; the results are pushed through the ua.events channel Reconnecting Reconnected.
API
| Event Channel | Category | Reading and Writing | Note |
|---|---|---|---|
| events.onReconnecting.add(listener) | Event | Read | Reconnection Start (ConnectionEventArgs) |
| events.onReconnected.add(listener) | Event | Read | Reconnection Successful (ConnectionEventArgs) |
| events.onDisconnected.add(listener) | Event | Read | Disconnect (ConnectionEventArgs) |
| events.onSessionLost.add(listener) | Event | Read | Session Expiration Alert (OpcUaEventEntry) |
onXxx It is a field List<Consumer<...>> (CopyOnWriteArrayList), which is .remove(listener) added or removed using .add(listener) /.
Java SideNonereconnect() This is a public method. Use ua.disconnect() + when you need to manually re-establish a connection ua.connect().
Code Example
import com.darra.opcua.*;
try (OpcUaSession ua = new OpcUaSession("opc.tcp://localhost:4840")) {
ua.events.onReconnecting.add(e ->
System.out.println("重连中... " + e.endpointUrl));
ua.events.onReconnected.add(e ->
System.out.println("已恢复"));
ua.events.onSessionLost.add(e ->
logger.warn("Session 失效: " + e.message + " (" + e.statusCode + ")"));
ua.connect();
// The business logic does not need to use a `try/catch ` block to reconnect—the underlying stack handles the reconnection in the background.
while (running) {
ReadResult rr = ua.readSafe("ns=2;s=Temp");
// readSafe It won't reconnect for you; it simply wraps the exceptions thrown during the underlying stack reconnection into `rr.status()`.
Thread.sleep(1000);
}
}
Reconnection Policy
- Reconnection is performed by the underlying stack; switch = construction parameter
autoReconnect - Reconnect timeout = at creation time
connectTimeoutMs(default 10000) - The number of retries and the interval between retries are fixed at 3 / 2000 ms, This configuration is not exposed on the Java side., There are SDK no environment variable switches at all
- State Transition:
Connected→ (Reconnecting) →Connected/Failed; TriggeredFailedwhen the state changes fromConnectedtoDisconnectedorevents.onDisconnected
Subscriptions and Auto-Renewal
After a session reconnection, automatically SDK:
- Try
transferSubscriptions(oldSubId)Transfer - Failure → Rebuild everything using the cached MonitoredItemSpec MI
For more information, see Subscription Resumes Automatically。
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| ua.Reconnect() | ua.reconnect() | None (using disconnect() + connect()) | ua.Reconnect() | ua.reconnect() | None (configuration-driven auto_reconnect ) |
There is no Reconnect entry point on the C side; reconnection is driven by the three DarraUa_SessionConfig settings auto_reconnect / reconnect_max_retries / reconnect_delay_ms; the connection status is checked via the DarraUa_Session_SetStateCallback callback and the DarraUa_Session_GetState query.