milo:Bad_SessionIdInvalid, The session id is not valid

风格不统一 提交于 2019-12-11 14:23:00

问题


I am getting a "The session id is not valid." exception when reading the value of a node ,but just sometimes,I konw the session is not timeout,so why?

the exception :

 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

Exception in thread "pool-2-thread-2" java.lang.NullPointerException at com.ggnykj.smartems.cloud.server.autocontrol.controller.OpcClientServerController.readNode(OpcClientServerController.java:116) 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)

there is my code:

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

there is the connect and read code:

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;
}

when I read the node,there is the problem:

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

but I don't know why


回答1:


This StatusCode comes from the server.

Maybe you can capture this behavior on Wireshark to ensure you're really using the same OpcUaClient instance you think you are and not one that has had its session timeout somehow. Or maybe the capture will show the server is at fault and returning this StatusCode when you read on a freshly created Session.



来源:https://stackoverflow.com/questions/57008517/milobad-sessionidinvalid-the-session-id-is-not-valid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!