Skip to main content

Session — OpcUaSession

OpcUaSession It is Java SDK the Client superclass (equivalent to in C# DarraOpcUa), with one instance corresponding to one OPC UA Session. All access operations (Read / Write / Browse / Subscribe / Call / History) originate from this object.

Implement AutoCloseable, Must Use it try-with-resources within, otherwise native resources and the server-side session will be leaked.

Subpage Redirect
  • For structural and connection specifications, please refer to Constructor.
  • For connecting/disconnecting, please refer to Connection Management.
  • For single-item and batch read/write operations, please refer to Read / Write.
  • For information on the unified channel for internal incidents, please refer to Events.
  • For namespace lookup, please refer to Namespaces.
  • For information on heartbeat and session keep-alive, please refer to KeepAlive.

Public Fields / Properties​

CategoryPropertyTypeAccessDescription
ConnectionsgetEndpointUrl()StringRThe endpoint URL specified at construction
getSecurityMode()Enums.MessageSecurityModeRSecurity mode (None / Sign / SignAndEncrypt)
StatusgetState()Enums.SessionStateRCurrent session status (Disconnected / Connecting / Connected / Reconnecting / Closing / Failed)
isConnected()booleanRWhether a connection is established (= getState() == Connected)
Child ObjectsnodesNodeCollectionRLazy Load node accessor (final field)
namespacesNamespaceCollectionRNamespace URI ↔ Index Conversion (final fields)
EventseventsOpcUaEventsRUnified channel for session-level events (Connected / Disconnected / DataChange / ServerEvent / ...) (final fields)
ConfigurationgetKeepAliveIntervalMs() / setKeepAliveIntervalMs(int)intRWKeepAlive heartbeat interval (default 10000 ms, 0 = disabled)
isAutoPublish() / setAutoPublish(boolean)booleanRWEnables or disables the background publish thread (default is true after connecting)
Internalhandle()intRC-layer handle (for debugging)

Public Methods​

Connection Management​

  • connect() — Establish a connection (Hello → SecureChannel → Session → Activate)
  • disconnect() — Connection terminated voluntarily; the instance can reconnect
  • close() — Release resources, automatically disconnect (from AutoCloseable)

Heartbeat and Release​

  • publish(int timeoutMs) — Manually trigger a Publish once (usually not necessary)

Read / Write​

Browse​

Subscribe​

Method Call​

History​

Control​

  • cancel(int requestHandle) — Cancel Unfinished Requests

Complete Example​

import com.darra.opcua.*;
import com.darra.opcua.OpcUaEvents.ConnectionEventArgs;

try (OpcUaSession ua = new OpcUaSession("opc.tcp://localhost:4840")) {
ua.events.onConnected.add(e ->
System.out.println("Connected to " + e.endpointUrl));
ua.events.onDisconnected.add(e ->
System.out.println("Disconnected: " + e.message));
ua.connect();

// Read
try (OpcUaDataValue dv = ua.read("ns=2;s=Temperature")) {
System.out.println("T = " + dv.variant().asDouble() + " °C");
}

// Write
try (OpcUaVariant v = new OpcUaVariant().setDouble(42.0)) {
Enums.StatusCode st = ua.write("ns=2;s=Setpoint", v);
System.out.println("Write status = " + st);
}

// Subscribe
try (OpcUaSubscription sub = ua.createSubscription(500.0)) {
sub.add("ns=2;s=Counter", e ->
System.out.println(e.nodeId + " = " + e.valueString + " (" + e.status + ")"));
Thread.sleep(60_000);
}
}

Next Step​