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).
- 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
- Maven (Recommended)
- Gradle
- Gradle (Kotlin DSL)
<dependency>
<groupId>xyz.darra</groupId>
<artifactId>darra-opcua-sdk</artifactId>
<version>0.6.0</version>
</dependency>
implementation 'xyz.darra:darra-opcua-sdk:0.6.0'
implementation("xyz.darra:darra-opcua-sdk:0.6.0")
The runtime libraries are already packaged in the JAR file; it's ready to use right after installation.
System Requirements
| Item | Requirements |
|---|---|
| JDK | 17+ |
| OS | Windows 10/11 |
| Platform | x64 |
| Agreement | OPC UA TCP (opc.tcp://) |
| Memory | 100MB+ (Stack itself 30MB) |
Package Structure
import com.darra.opcua.*;
Main Types:
| Type | Uses |
|---|---|
| OpcUaSession | Client Superclass (corresponding to C# DarraOpcUa) |
| OpcUaSubscription | Data Subscription Container |
| OpcUaEventSubscription | Event Subscriptions (Alarms & Conditions) |
| NodeCollection / OpcUaNode | Lazy Load Node Access |
| OpcUaVariant / OpcUaDataValue | Data Encapsulation (Implementing AutoCloseable) |
| OpcUaDiscovery (Static) | GetEndpoints / FindServers |
| WellKnownNodes (Static) | OPC Foundation Standard Node Constants |
| OpcUaException | RuntimeException, carrying StatusCode |
| Enums.* | StatusCode / NodeClass / AttributeId / MessageSecurityMode / UserTokenType / SessionState / BuiltinType / MonitoringMode |
Cross-Language Superclass Mapping
| Concept | C# | Java |
|---|---|---|
| Client Base Class | DarraOpcUa | OpcUaSession |
| Structural Connections | using var ua = new DarraOpcUa(url) + ua.Connect() | try (OpcUaSession ua = new OpcUaSession(url)) { ua.connect(); } |
| Data Value Container | Variant | OpcUaVariant (Chain-based setXxx) |
| Single-point reading | var dv = ua.Read(nid) | OpcUaDataValue dv = ua.read(nid) |
| Event Subscription | ua.Events.Connected += h | ua.events.onConnected.add(consumer) |
| Resource Management | using | try-with-resources |
| Time Type | DateTime | java.time.Instant (uses long FileTime internally) |
| Set Return | IReadOnlyList<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
| Feature | Entry API | Description |
|---|---|---|
| Connection / Encryption | new OpcUaSession(...) + connect() | None / Sign / SignAndEncrypt + Username / Anonymous / Certificate |
| Single-Point Read/Write | ua.read / write | Single Node, Single Attribute |
| Batch Read | ua.readMany(nodes, attr) | Once RPC, N nodes |
| Browse Tree | ua.browse / browseMany / browseWithPaging / browseNext / browseAll | Full pagination support |
| Path Resolution | ua.resolve / translateBrowsePaths | Browse Path → NodeId |
| Registration Node | ua.registerNodes / unregisterNodes | Reduces server-side parsing overhead during frequent, repeated requests to the same node (benefits based on actual server-side testing) |
| Data Subscriptions | ua.createSubscription + sub.add / addMany | publishing / sampling, two-layer |
| Event Subscription | ua.subscribeEvents | Alarms & Conditions |
| Adjustment Methods | ua.call(objId, mtdId, ...) | OPC UA Method Node |
| History Reading | ua.readHistory / readHistoryModified / readHistoryAtTime / readHistoryProcessed / readHistoryEvents | Raw / Modified / AtTime / Processed / Events |
| Writing History | ua.updateHistory / deleteHistoryRange | Insert / Replace / Update / Delete |
| Heartbeat | ua.setKeepAliveIntervalMs(int) | Background Periodic Monitoring |
| Automatic Reconnection | (Built-in, via the constructor parameter autoReconnect=true) | Check for Disconnections → Republish Subscribe |
| Unified Event | ua.events.onAny.add(...) | Session / Subscription / Data / Communication Errors |
| Service Discovery | OpcUaDiscovery.getEndpoints / findServers | No session required |
Java-Specific Constraints
OpcUaSession / OpcUaSubscription / OpcUaDataValue / OpcUaVariant AutoCloseable; failing to call close will cause a native memory leak. Always use try-with-resources.
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.
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 VariantsetDateTime(long fileTime)direct transfer FileTime - OpcUaException is a RuntimeException: Try-catch is not required; the business layer handles exceptions as needed.
- No
event +=Syntax: Useevents.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.