メインコンテンツまでスキップ

コンストラクタ

OpcUaSession 複数のオーバーロードを提供しています:最もシンプルな「エンドポイントのみ」のコンストラクタ、ユーザー名付きコンストラクタ、暗号化コンストラクタ、および完全設定コンストラクタです。さらに、二つの静的暗号化用ショートカットファクトリも用意されています。

事前学習
  • 暗号化パラメータ (SecurityMode / SecurityPolicyUri / 証明書) については、以下を参照してください。 セキュリティ 暗号化.
  • サーバー側の事前スキャンについては、以下を参照してください。 Discovery.
  • 接続後の操作については、以下を参照してください。 接続管理.

構造の簡略化(推奨)​

public OpcUaSession(String endpointUrl)
public OpcUaSession(String endpointUrl, String username, String password)
public OpcUaSession(String endpointUrl,
Enums.MessageSecurityMode mode,
String username, String password)
public OpcUaSession(String endpointUrl,
Enums.MessageSecurityMode mode,
String username, String password,
int sessionTimeoutMs, int requestTimeoutMs, int connectTimeoutMs)

最初の二つのコンストラクタは Enums.MessageSecurityMode.None (平文) を使用します。後半の二つのコンストラクタでは、記載されていないパラメータはデフォルト値になります: keepAliveIntervalMs = 10000、autoReconnect = true、autoPublishOnConnect = true。証明書の四つのパラメータ (clientCertPath / clientKeyPath / serverCertPath / securityPolicyUri) はすべて null です。

例​

import com.darra.opcua.*;

// 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秒)が設定済みなので、エンドポイントに「 PFX:」を付けるだけで済みます:

import com.darra.opcua.*;

// 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)

パラメータ​

カテゴリプロパティ型アクセス説明
必須endpointUrlStringinOPC UA エンドポイント。例: opc.tcp://host:port
セキュリティsecurityModeEnums.MessageSecurityModeinNone / Sign / SignAndEncrypt
securityPolicyUriStringinnull = mode に応じて自動的に選択 (None → #None、その他 → #Basic256Sha256)
clientCertPathStringinクライアント PFX パス (Sign / SignAndEncrypt 必須)、秘密鍵を内包
clientKeyPathStringin秘密鍵のパス — 現在の秘密鍵は PFX 自体に同梱されています。このパラメータは Stack と揃えるために残されており、null を指定すれば問題ありません。PFX のパスワードではありません
serverCertPathStringinオプション:サーバー証明書の DER パス。指定しない場合は None→Secure への自動アップグレードが行われます
ログインusernameStringinnull = 匿名、空でない = UsernameIdentityToken
passwordStringinusername と組み合わせて使用
タイムアウトsessionTimeoutMsintinサーバー側のセッションタイムアウト/接続維持時間(デフォルトは 60000 = 60 秒。短く設定しすぎないようにしてください)
requestTimeoutMsintin単一 RPC タイムアウト (デフォルト 5000 = 5 秒、Read / Write / Browse の大きなリクエストを含む)
connectTimeoutMsintin接続確立のタイムアウト(デフォルト 10000 = 10 秒、4 ステップのハンドシェイクを含む)
実行keepAliveIntervalMsintinKeepAlive ハートビート間隔(標準値 10000、0 = 無効)
autoReconnectbooleanin切断後の自動再接続
autoPublishOnConnectbooleaninconnect() 時にバックグラウンドの Publish スレッドを自動的に起動する

例 — 匿名 + Sign 暗号化​

import com.darra.opcua.*;

try (OpcUaSession ua = new OpcUaSession(
"opc.tcp://server:4840",
Enums.MessageSecurityMode.Sign,
null, null, // 匿名
60_000, 5_000, 10_000, // セッション / RPC / 接続タイムアウト
10_000, true, true, // KeepAlive / autoReconnect / autoPublish
"C:\\certs\\client.pfx", null, // clientCertPath = PFX (秘密鍵を含む); clientKeyPath = 秘密鍵のパス。現在は PFX に付属しており、null を渡す
null, null)) { // serverCertPath 「非伝送」→「None」→「Secure」への自動アップグレード; 空欄 securityPolicyUri のままにすると、モードに応じて自動的に選択されます
ua.connect();
}

例 — 結果ファクトリによる GetEndpoints 構築​

import com.darra.opcua.*;
import java.util.List;

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",
60_000, 5_000, 10_000,
10_000, true, true,
"C:\\certs\\client.pfx", null, // clientCertPath = PFX; null を clientKeyPath 渡す
null, // serverCertPath 非公開 → None → セキュア 自動アップグレード
ep.getSecurityPolicyUri())) { // サーバーが公開したポリシーを再利用 URI
ua.connect();
}

異常​

異常理由
IllegalArgumentExceptionendpointUrl null または空文字列
OpcUaException ("Session_Create failed")下層の DarraUa_Session_Create が 0 以外を返す; ex.statusCode = Enums.StatusCode.fromCode(rc), ex.rawCode = 生の int コード

構築時に例外 OpcUaException をスローするのは唯一 Session_Create のこの経路だけです。ensureStackInitialized() は DarraUa_Initialize() の戻り値を検査しないため、構築時に「Stack の初期化に失敗」例外がスローされることはありません。証明書の読み込み失敗は connect() の段階で発生し、構築時には発生しません。

リソース管理​

OpcUaSession 実装 AutoCloseable:プロセスレベルの参照カウントの内部管理およびネイティブスタックの初期化。 try-with-resources を使用するか、明示的に close を実行する必要があります:

import com.darra.opcua.*;

// おすすめ:try-with-resources による自動クローズ
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() 実行すると、セッションが破棄 DarraUa_Session_Close され、プロセスレベルのスタックの参照カウントが減算されます(ゼロになるとスタックがシャットダウンされます)。通常はこの処理で問題 try-with-resources ありません。close を実行すると、そのインスタンスには再度接続できなくなるため、新規に作成する必要があります OpcUaSession。

スタックの参照カウント

SDK :プロセス間で共有されるネイティブスタック。最初の OpcUaSession が作成された際にスタックが初期化され、最後の OpcUaSession が閉じられた際にスタックがシャットダウンされる。閉じ忘れがあると、スタックがプロセス内に永続的に残ってしまう。機能には影響しないが、メモリ使用量が増加する。

次のステップ​