构造函数
OpcUaSession 提供多个重载: 最简化的 endpoint-only 构造、用户名构造、加密构造, 以及全配置构造. 还有两个静态加密快捷工厂.
前置阅读
- 加密参数 (SecurityMode / SecurityPolicyUri / 证书) 见 Security 加密.
- 服务端预扫描见 Discovery.
- 连上后的操作见 连接管理.
简化构造 (推荐)
public OpcUaSession(String endpointUrl)
public OpcUaSession(String endpointUrl, String username, String password)
public OpcUaSession(String endpointUrl,
Enums.MessageSecurityMode mode,
String username, String password)
例子
// 1. 匿名 + 明文 (开发期)
try (OpcUaSession ua = new OpcUaSession("opc.tcp://localhost:4840")) {
ua.connect();
}
// 2. 用户名 + 明文
try (OpcUaSession ua = new OpcUaSession(
"opc.tcp://server:4840", "operator", "secret")) {
ua.connect();
}
加密快捷工厂 (推荐)
public static OpcUaSession secureSign(String endpointUrl, String clientCertPath)
public static OpcUaSession secureSignAndEncrypt(String endpointUrl, String clientCertPath)
封装好默认参数 (Basic256Sha256, 自动重连, 心跳 10 秒), 只需 endpoint + PFX:
// Sign (防篡改, 不防嗅探)
try (OpcUaSession ua = OpcUaSession.secureSign(
"opc.tcp://server:4840", "C:\\certs\\client.pfx")) {
ua.connect();
}
// SignAndEncrypt (典型生产)
try (OpcUaSession ua = OpcUaSession.secureSignAndEncrypt(
"opc.tcp://server:4840", "C:\\certs\\client.pfx")) {
ua.connect();
}
全配置构造
public OpcUaSession(String endpointUrl,
Enums.MessageSecurityMode securityMode,
String username, String password,
int sessionTimeoutMs, int requestTimeoutMs, int connectTimeoutMs,
int keepAliveIntervalMs, boolean autoReconnect, boolean autoPublishOnConnect,
String clientCertPath, String clientKeyPath, String serverCertPath,
String securityPolicyUri)
参数
| 类别 | 属性 | 类型 | 访问 | 说明 |
|---|---|---|---|---|
| 必填 | endpointUrl | String | in | OPC UA endpoint, 形如 opc.tcp://host:port |
| 安全 | securityMode | Enums.MessageSecurityMode | in | None / Sign / SignAndEncrypt |
securityPolicyUri | String | in | null = 按 mode 自动选 (None → #None, 其他 → #Basic256Sha256) | |
clientCertPath | String | in | 客户端 PFX 路径 (Sign / SignAndEncrypt 必填) | |
clientKeyPath | String | in | PFX 密码 | |
serverCertPath | String | in | 服务端 DER, null = TOFU (Trust On First Use) | |
| 登录 | username | String | in | null = Anonymous, 非空 = UsernameIdentityToken |
password | String | in | 配 username 用 | |
| 超时 | sessionTimeoutMs | int | in | 服务端 Session 超时 (典型 600000 = 10 分钟) |
requestTimeoutMs | int | in | 单 RPC 超时 (典型 10000 = 10 秒) | |
connectTimeoutMs | int | in | 建链超时 (典型 10000 = 10 秒) | |
| 运行 | keepAliveIntervalMs | int | in | KeepAlive 心跳周期 (典型 10000, 0 = 禁用) |
autoReconnect | boolean | in | 断线后自动重连 | |
autoPublishOnConnect | boolean | in | connect() 时自动启动后台 Publish 线程 |
例子 — 匿名 + Sign 加密
try (OpcUaSession ua = new OpcUaSession(
"opc.tcp://server:4840",
Enums.MessageSecurityMode.Sign,
null, null, // 匿名
600_000, 10_000, 10_000, // 超时
10_000, true, true, // KeepAlive / autoReconnect / autoPublish
"C:\\certs\\client.pfx", "123456", // PFX + 密码
null, null)) { // serverCert TOFU + 默认 Basic256Sha256
ua.connect();
}
例子 — 按 GetEndpoints 结果工厂构造
List<EndpointDescription> endpoints =
OpcUaDiscovery.getEndpoints("opc.tcp://server:4840");
EndpointDescription ep = endpoints.stream()
.filter(e -> e.getSecurityMode() == Enums.MessageSecurityMode.SignAndEncrypt)
.findFirst()
.orElseThrow();
try (OpcUaSession ua = new OpcUaSession(
ep.getEndpointUrl(),
ep.getSecurityMode(),
"operator", "secret",
600_000, 10_000, 10_000,
10_000, true, true,
"C:\\certs\\client.pfx", "123456",
null, // TOFU
ep.getSecurityPolicyUri())) { // 复用 server 公布的 policy URI
ua.connect();
}
异常
| 异常 | 原因 |
|---|---|
IllegalArgumentException | endpointUrl 为空 |
OpcUaException (DarraOutOfMemory) | Stack 初始化失败 |
OpcUaException (BadCertificateInvalid) | 客户端证书加载失败 |
资源管理
OpcUaSession 实现 AutoCloseable, 内部维护进程级引用计数 + native Stack 初始化. 必须用 try-with-resources 或显式 close:
// 推荐: try-with-resources 自动 close
try (OpcUaSession ua = new OpcUaSession("opc.tcp://server:4840")) {
ua.connect();
// ...
}
// 或显式
OpcUaSession ua = new OpcUaSession("opc.tcp://server:4840");
try {
ua.connect();
// ...
} finally {
ua.close();
}
close() 内部自动调用 disconnect(), 一般 try-with-resources 即可.
Stack 引用计数
SDK 进程级共享 native Stack. 第一个 OpcUaSession 创建时 init Stack, 最后一个 close 时 shutdown Stack. 漏 close 会让 Stack 永驻进程, 不影响功能但内存涨.