0

我收到“会话 ID 无效”。读取节点值时出现异常,但有时,我知道会话没有超时,那为什么呢?

例外:</p>

 java.util.concurrent.ExecutionException: UaServiceFaultException: status=Bad_SessionIdInvalid, message=The session id is not valid.
at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:395)
at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1999)
at com.ggnykj.smartems.cloud.server.autocontrol.client.OpcClientServer.readNode(OpcClientServer.java:230)
at com.ggnykj.smartems.cloud.server.autocontrol.controller.OpcClientServerController.readNode(OpcClientServerController.java:115)
at com.ggnykj.smartems.cloud.server.autocontrol.server.SaveExOpcData.run(SaveExOpcData.java:59)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: UaServiceFaultException: status=Bad_SessionIdInvalid, message=The session id is not valid.
at org.eclipse.milo.opcua.stack.client.UaTcpStackClient.lambda$receiveResponse$16(UaTcpStackClient.java:367)
at org.eclipse.milo.opcua.stack.core.util.ExecutionQueue$PollAndExecute.run(ExecutionQueue.java:107)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java)
... 3 more

com.ggnykj.smartems.cloud.server.autocontrol.controller.OpcClientServerController.readNode(OpcClientServerController.java:116) 在 com.ggnykj.smartems.cloud 的线程“pool-2-thread-2”java.lang.NullPointerException 中的异常.server.autocontrol.server.SaveExOpcData.run(SaveExOpcData.java:59) 在 java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) 在 java.base/java.util.concurrent.ThreadPoolExecutor $Worker.run(ThreadPoolExecutor.java:628) 在 java.base/java.lang.Thread.run(Thread.java:834)

有我的代码:

MyX509IdentityProvider x509IdentityProvider = new MyX509IdentityProvider(clientOption.opcServer.getCertFilePath(),
            clientOption.opcServer.getPrivateKeyPath());
    X509Certificate cert = x509IdentityProvider.getCertificate();
    KeyPair keyPair = new KeyPair(cert.getPublicKey(), x509IdentityProvider.getPrivateKey());

    OpcUaClientConfig config = OpcUaClientConfig.builder()
        .setApplicationName(LocalizedText.english(clientOption.opcServer.getOpcServerName()))
        //.setApplicationUri("urn:eclipse:milo:examples:client")
        .setApplicationUri("")
        //.setCertificate(loader.getClientCertificate())
        //.setKeyPair(loader.getClientKeyPair())
        .setCertificate(cert)
        .setKeyPair(keyPair)
        .setEndpoint(endpoint)
        .setIdentityProvider(clientOption.getIdentityProvider())
        .setChannelLifetime(uint(600000000))
        .setSessionTimeout(uint(600000000))
        //.setRequestTimeout(uint(5000))
        .build();

    return new OpcUaClient(config);

有连接和读取代码:

private OpcUaClient getOpcClient(OpcServer opcServer){
    OpcUaClient client = null;
    if(null!=opcClientMap.get(opcServer.getOpcServerId())){
        client = opcClientMap.get(opcServer.getOpcServerId());
    }else{
        try {
            ClientOption clientOption = new ClientOption(opcServer);
            client =  new ClientOptionRunner(clientOption).createClient();
        }catch (Throwable t){
            logger.error("Error getting client: {}", t.getMessage(), t);
        }
        opcClientMap.put(opcServer.getOpcServerId(),client);
    }
    return client;
}



public DataValue readNode(OpcServer opcServer,NodeId nodeId){
    OpcUaClient client = getOpcClient(opcServer);
    DataValue value = null;

    try {
        client.connect().get();  
        value = client.readValue(0.0, TimestampsToReturn.Both, nodeId).get(); 
    }catch (Exception e){
        e.printStackTrace();
    }
    return value;
}

当我读取节点时,出现了问题:

value = client.readValue(0.0, TimestampsToReturn.Both, nodeId).get();

但我不知道为什么

4

1 回答 1

0

此 StatusCode 来自服务器。

也许您可以在 Wireshark 上捕获此行为,以确保您真正使用的是您认为自己使用的同一 OpcUaClient 实例,而不是某个会话超时的实例。或者,当您阅读新创建的 Session 时,捕获将显示服务器出现故障并返回此 StatusCode。

于 2019-07-12T15:06:44.800 回答