Skip to main content

Darra OPC UA Client — Java SDK

Darra OPC UA Client Java Bindings. Advanced cross-language alignment API—the concepts you learn here can be directly applied to C#, Python, C++, Rust, and C, but API naming follows Java conventions (camelCase methods + try-with-resources + java.util.List collections).

Chapter Navigation
  • Getting Started → Session (Construct / Connect / Read / Write / KeepAlive)
  • Browse → Nodes (Browse / Path Resolution / RegisterNodes)
  • Real-time → Subscription (CreateSubscription / MonitoredItem / DataChanged)
  • History → History (Raw / Modified / AtTime / Processed / Events / Update / Delete)
  • Call → Method (Call)
  • Discover → Discovery (GetEndpoints / FindServers)
  • Data → Data Types (Variant / DataValue / WellKnownNodes)
  • Security → Security (Certificates / Encryption)

Installation​

<dependency>
<groupId>xyz.darra</groupId>
<artifactId>darra-opcua-sdk</artifactId>
<version>0.6.0</version>
</dependency>

The runtime libraries are already packaged in the JAR file; it's ready to use right after installation.

System Requirements​

ItemRequirements
JDK17+
OSWindows 10/11
Platformx64
AgreementOPC UA TCP (opc.tcp://)
Memory100MB+ (Stack itself 30MB)

Package Structure​

import com.darra.opcua.*;

Main Types:

TypeUses
OpcUaSessionClient Superclass (corresponding to C# DarraOpcUa)
OpcUaSubscriptionData Subscription Container
OpcUaEventSubscriptionEvent Subscriptions (Alarms & Conditions)
NodeCollection / OpcUaNodeLazy Load Node Access
OpcUaVariant / OpcUaDataValueData Encapsulation (Implementing AutoCloseable)
OpcUaDiscovery (Static)GetEndpoints / FindServers
WellKnownNodes (Static)OPC Foundation Standard Node Constants
OpcUaExceptionRuntimeException, carrying StatusCode
Enums.*StatusCode / NodeClass / AttributeId / MessageSecurityMode / UserTokenType / SessionState / BuiltinType / MonitoringMode

Cross-Language Superclass Mapping​

ConceptC#Java
Client Base ClassDarraOpcUaOpcUaSession
Structural Connectionsusing var ua = new DarraOpcUa(url) + ua.Connect()try (OpcUaSession ua = new OpcUaSession(url)) { ua.connect(); }
Data Value ContainerVariantOpcUaVariant (Chain-based setXxx)
Single-point readingvar dv = ua.Read(nid)OpcUaDataValue dv = ua.read(nid)
Event Subscriptionua.Events.Connected += hua.events.onConnected.add(consumer)
Resource Managementusingtry-with-resources
Time TypeDateTimejava.time.Instant (uses long FileTime internally)
Set ReturnIReadOnlyList<T>java.util.List<T>

Quick Start in One Minute​

import com.darra.opcua.*;

try (OpcUaSession ua = new OpcUaSession("opc.tcp://localhost:4840")) {
ua.connect();

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

// Write
try (OpcUaVariant v = new OpcUaVariant().setDouble(42.0)) {
ua.write("ns=2;s=Setpoint", v);
}

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

Advanced API Overview​

FeatureEntry APIDescription
Connection / Encryptionnew OpcUaSession(...) + connect()None / Sign / SignAndEncrypt + Username / Anonymous / Certificate
Single-Point Read/Writeua.read / writeSingle Node, Single Attribute
Batch Readua.readMany(nodes, attr)Once RPC, N nodes
Browse Treeua.browse / browseMany / browseWithPaging / browseNext / browseAllFull pagination support
Path Resolutionua.resolve / translateBrowsePathsBrowse Path → NodeId
Registration Nodeua.registerNodes / unregisterNodesReduces server-side parsing overhead during frequent, repeated requests to the same node (benefits based on actual server-side testing)
Data Subscriptionsua.createSubscription + sub.add / addManypublishing / sampling, two-layer
Event Subscriptionua.subscribeEventsAlarms & Conditions
Adjustment Methodsua.call(objId, mtdId, ...)OPC UA Method Node
History Readingua.readHistory / readHistoryModified / readHistoryAtTime / readHistoryProcessed / readHistoryEventsRaw / Modified / AtTime / Processed / Events
Writing Historyua.updateHistory / deleteHistoryRangeInsert / Replace / Update / Delete
Heartbeatua.setKeepAliveIntervalMs(int)Background Periodic Monitoring
Automatic Reconnection(Built-in, via the constructor parameter autoReconnect=true)Check for Disconnections → Republish Subscribe
Unified Eventua.events.onAny.add(...)Session / Subscription / Data / Communication Errors
Service DiscoveryOpcUaDiscovery.getEndpoints / findServersNo session required

Java-Specific Constraints​

AutoCloseable must implement close

OpcUaSession / OpcUaSubscription / OpcUaDataValue / OpcUaVariant AutoCloseable; failing to call close will cause a native memory leak. Always use try-with-resources.

Callbacks must hold a strong reference

DataChange / Event / StateChange—these callbacks must be held by the Java side with a strong reference. As long as they are not explicitly released, they are OpcUaSubscription safe.

Swing / JavaFX UI Threads

Event callbacks are invoked on the native Publish thread. UI Operations must SwingUtilities.invokeLater / Platform.runLater switch back to the thread UI.

  • Instant ↔ FileTime: Windows is used SDK internally FileTime (100ns since 1601 UTC); history is API automatically converted Instant. When customizing, use DateTime Variant setDateTime(long fileTime) direct transfer FileTime
  • OpcUaException is a RuntimeException: Try-catch is not required; the business layer handles exceptions as needed.
  • No event += Syntax: Use events.onConnected.add(consumer) / events.onConnected.remove(consumer)

Cross-Language Alignment​

The API naming and behavior of the C# SDK are the reference implementation for bindings in other languages. The Java binding follows Java naming conventions (camelCase, getXxx getters), but the semantics are consistent (e.g. C# ua.Read ↔ Java ua.read ↔ Python ua.read). Start with Java; switching to another language later only requires renaming — the concepts and semantics stay the same.

Just read through the left sidebar in order to master everything SDK API.