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
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Connections | getEndpointUrl() | String | R | The endpoint URL specified at construction |
getSecurityMode() | Enums.MessageSecurityMode | R | Security mode (None / Sign / SignAndEncrypt) | |
| Status | getState() | Enums.SessionState | R | Current session status (Disconnected / Connecting / Connected / Reconnecting / Closing / Failed) |
isConnected() | boolean | R | Whether a connection is established (= getState() == Connected) | |
| Child Objects | nodes | NodeCollection | R | Lazy Load node accessor (final field) |
namespaces | NamespaceCollection | R | Namespace URI ↔ Index Conversion (final fields) | |
| Events | events | OpcUaEvents | R | Unified channel for session-level events (Connected / Disconnected / DataChange / ServerEvent / ...) (final fields) |
| Configuration | getKeepAliveIntervalMs() / setKeepAliveIntervalMs(int) | int | RW | KeepAlive heartbeat interval (default 10000 ms, 0 = disabled) |
isAutoPublish() / setAutoPublish(boolean) | boolean | RW | Enables or disables the background publish thread (default is true after connecting) | |
| Internal | handle() | int | R | C-layer handle (for debugging) |
Public Methods
Connection Management
connect()— Establish a connection (Hello → SecureChannel → Session → Activate)disconnect()— Connection terminated voluntarily; the instance can reconnectclose()— Release resources, automatically disconnect (from AutoCloseable)
Heartbeat and Release
publish(int timeoutMs)— Manually trigger a Publish once (usually not necessary)
Read / Write
read(String nodeId)— Reading a Single-Node Value Attributeread(nodeId, AttributeId)— Read a Single Node's Specified AttributereadMany(List<String>, AttributeId)— Batch Read (Single Attribute)write(nodeId, OpcUaVariant)— Order-writing node Valuewrite(nodeId, OpcUaVariant, AttributeId)— Specify Attribute for the Order Creation Node
Browse
browse(nodeId)/browse(nodeId, NodeClass)— Single-page viewbrowseMany(List<String>, NodeClass)— Batch ViewbrowseWithPaging(nodeId, NodeClass)— Includes ContinuationPoint page-turning supportbrowseNext(byte[] cp, boolean release)— Continue to the next pagebrowseAll(nodeId)/browseAll(nodeId, filter, maxPages)— Auto-paging: Load AlltranslateBrowsePaths(List<Map.Entry<String,String>>)— Batch parse the paths to NodeIdtranslateBrowsePathsAt(start, paths)— Convenient reloading from the same starting pointresolve(path)— Parsing a Single Absolute Pathresolve(startNodeId, path)— Parsing a Single Relative PathregisterNodes(List<String>)— Register for a Temporary High-Efficiency Plan NodeIdunregisterNodes(List<String>)— Cancel Temporary NodeId
Subscribe
createSubscription(double publishingIntervalMs)— Create a data subscriptionsubscribeEvents()— Subscribe to Server events (default i=2253)subscribeEvents(nodeId)— Subscribe to events from a specific nodesubscribeEvents(nodeId, existingSub)— Reuse an existing subscription containertransferSubscriptions(List<Integer>, boolean)— Migrating Subscriptions Across Sessions
Method Call
call(objectId, methodId, OpcUaVariant... inputs)— Call the node OPC UA Method
History
readHistory(nodeId, Instant start, Instant end, int maxValues)— Reading Ancient HistoryreadHistoryModified(nodeId, start, end, maxValues, returnBounds)— Reading a history that includes revision recordsreadHistoryAtTime(nodeId, List<Instant> times, useSimpleBounds)— Read the interpolated value at a specified time pointreadHistoryProcessed(nodeId, start, end, processingMs, aggregateNodeId)— Aggregation (Avg / Min / Max / TimeAverage / ...)readHistoryEvents(nodeId, start, end, maxEvents)— Reading About Historical EventsupdateHistory(nodeId, HistoryUpdateType, List<OpcUaDataValue>)— Insert / Replace / Update / Remove HistorydeleteHistoryRange(nodeId, start, end, deleteModifiedToo)— Delete history by time period
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);
}
}