rxhulcm
rxhulcm

Reputation: 21

Issue in handling server connection issues in my OPC UA client

I have an OPC UA client that uses subscriptions to take values of tags from a simulation server in my pc. In case I close my server and open it again, I want my OPC UA client to continue the subscriptions, but instead it encounters a SocketException and gets stuck. I have added a Subscription Listener to my Subscription Manager. This is the code

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

And this is the Exception that I'm receiving

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```

Upvotes: 1

Views: 57

Answers (1)

Kevin Herron
Kevin Herron

Reputation: 7005

Don't call client.connect() inside onSubscriptionTransferFailed.

The client automatically reconnects for you, and when you are inside this callback that means the reconnect has happened, the old Subscriptions don't exist or can't be transferred, and it's time to create new ones to replace them.

Upvotes: 0

Related Questions