处理我的 OPC UA 客户端中的服务器连接问题

问题描述 投票:0回答:1

我有一个 OPC UA 客户端,它使用订阅从我的电脑中的模拟服务器获取标签值。如果我关闭服务器并再次打开它,我希望我的 OPC UA 客户端继续订阅,但它会遇到 SocketException 并卡住。我已将订阅侦听器添加到我的订阅管理器中。 这是代码

    private final OpcUaClient client;

    public MySubscriptionListener(OpcUaClient client) {
        this.client = client;
    }

    public void onPublishFailure(UaSubscription subscription) {
        // This is triggered when a publish cycle fails
        logger.warn("Subscription publish failure: {}", subscription.getSubscriptionId());
    }

    public void onSubscriptionTransferFailed(UaSubscription subscription) {
        // This is triggered when a subscription transfer fails (i.e., after a reconnect)
        logger.error("Failed to transfer subscription {} after reconnection", subscription.getSubscriptionId());
        client.connect();
        // Resubscribe the nodes that failed to transfer
        try {
            createSubscription(client, subscription.getSubscriptionId().intValue()); 
        } catch (Exception e) {
            logger.error("Failed to recreate subscription after transfer failure", e);
        }
    }

这是我收到的异常

15:36:45.971 [milo-netty-event-loop-1] ERROR o.e.m.o.s.c.t.u.UascClientMessageHandler - [remote=KCT-L-610/192.168.41.223:53530] Exception caught: Connection reset
java.net.SocketException: Connection reset
    at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:401)
    at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:434)
    at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:258)
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132)
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:357)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:151)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:722)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:584)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:496)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:995)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at java.base/java.lang.Thread.run(Thread.java:1583)
15:36:53.089 [milo-shared-thread-pool-12] WARN  o.e.milo.opcua.sdk.client.SessionFsm - [2] Keep Alive failureCount=2 exceeds failuresAllowed=1```
java opc-ua opcua-client eclipse-milo
1个回答
0
投票

不要在

client.connect()
内打电话给
onSubscriptionTransferFailed

客户端会自动为您重新连接,当您处于此回调中时,意味着重新连接已经发生,旧的订阅不存在或无法转移

© www.soinside.com 2019 - 2024. All rights reserved.