milo icon indicating copy to clipboard operation
milo copied to clipboard

I want to Keep start for Subscription mode on client,How to automatic reconnect when disconnected

Open zk230 opened this issue 9 years ago • 15 comments

I want to Keep start for Subscription mode on client,How to automatic reconnect when disconnected @kevinherron

zk230 avatar Nov 28 '17 09:11 zk230

Once a connection has been established, re-connection is automatic. You don't need to explicitly enable it. The client will continue to reconnect and keep an active session until you tell it to disconnect.

kevinherron avatar Nov 28 '17 12:11 kevinherron

but, the SubscriptionExample is do not reconnect,why? @kevinherron

zk230 avatar Nov 29 '17 10:11 zk230

Sorry, I'm still not understanding you.

All of the client examples shut themselves down after 10-15 seconds, so you won't be able to observe any reconnect behavior in those.

kevinherron avatar Nov 29 '17 14:11 kevinherron

I am change Thread.sleep(5000) to Thread.sleep(99999999) , the SubscriptionExample is keep long time,I am stop the server ,but SubscriptionExample is do not reconnect @kevinherron

zk230 avatar Nov 29 '17 15:11 zk230

If you're looking to see more data arrive then this example won't show you that. It doesn't add a UaSubscriptionManager.SubscriptionListener that handles the transfer failure that will occur when it reconnects to the server you restarted.

See OpcUaClientIT#testFailedSubscriptionTransfer for a test that verifies the reconnect happens and subscriptions can continue if the listener is in place.

kevinherron avatar Nov 29 '17 16:11 kevinherron

Sorry, this method did not solve my problem. But I guess the location of the problem.

[ua-netty-event-loop-1] ERROR org.eclipse.milo.opcua.stack.client.handlers.UaTcpClientMessageHandler - [remote=localhost/127.0.0.1:12686] Exception caught: An existing connection was forcibly closed by the remote host.

"UaTcpClientMessageHandler" this class is netty client,but this class no implement "channelUnregistered" method,you can refer to UptimeClientHandler.java and UptimeClient.java @kevinherron

zk230 avatar Nov 30 '17 06:11 zk230

Implementing channelUnregistered is not needed. ClientChannelManager adds an adapter that implements channelInactive and starts the reconnect, seen here: https://github.com/eclipse/milo/blob/cd0da80b171d78d362cde2305214c07ad96c8478/opc-ua-stack/stack-client/src/main/java/org/eclipse/milo/opcua/stack/client/ClientChannelManager.java#L372-L390

Can you post a code example that explains and shows the problem you're having?

kevinherron avatar Nov 30 '17 12:11 kevinherron

Also, are you aware that ClientExampleRunner terminates after 15 seconds regardless of the amount of time your example sleeps for: https://github.com/eclipse/milo/blob/f388968c8fcfe73da2957028a0d2c11e91d6e78c/milo-examples/client-examples/src/main/java/org/eclipse/milo/examples/client/ClientExampleRunner.java#L117-L123

and upon termination it disconnects the client and shuts down the server.

kevinherron avatar Nov 30 '17 17:11 kevinherron

I know this time limit,but when extension of this time and split client and server then stop the server, It do not reconnect @kevinherron

zk230 avatar Dec 08 '17 11:12 zk230

Please provide a self-contained reproducible example code that demonstrates the issue.

All of the tests as well as production deployments of the client reconnect, so either there's some kind of communication problem between you and I or you've stumbled upon a legitimate bug that nobody else has reproduced so far.

kevinherron avatar Dec 08 '17 13:12 kevinherron

@kevinherron Hi,I'm sorry I can not reply in time,I will now explain this issue,on OpcUaClientIT#testFailedSubscriptionTransfer when OpcUaClientIT#startClientAndServer the client first started, this time can not be reconnected. Another question,How to always reconnect?

zk230 avatar Jan 02 '18 08:01 zk230

@kevinherron hello?Can you help me?

zk230 avatar Jan 23 '18 05:01 zk230

The state machine only keeps reconnecting once a successful connection has been established. The user of the client SDK is responsible for calling connect() and ensuring it completes successfully, as well as any subsequent calls to keep trying to make the initial connection.

A very simplified example of what this could look like:

    private static void connect(OpcUaClient client) {
        client.connect().whenComplete((c, ex) -> {
            if (ex != null) {
                Stack.sharedScheduledExecutor().schedule(
                    () -> connect(client), 1, TimeUnit.SECONDS);
            }
        });
    }

What works for your application will be different. You'll probably want to track the scheduled task, log failures, be able to cancel the task, etc...

kevinherron avatar Jan 23 '18 13:01 kevinherron

如果您希望看到更多数据到达,那么此示例将不会向您显示。它不会添加UaSubscriptionManager.SubscriptionListener用于处理重新连接到重新启动的服务器时发生的传输失败的情况。

查看OpcUaClientIT#testFailedSubscriptionTransfer验证重新连接的测试,如果侦听器就位,则可以继续订阅。

when i restart server the sdk respone message:" Received unmatched TransferSubscriptionsResponse with requestHandle=0, timestamp=DateTime{utcTime=0, javaDate=Mon Jan 01 08:00:00 CST 1601}", can you help me out ?

maicb avatar Jan 11 '19 12:01 maicb

You can easily change the


            try {
                clientExample.run(client, future);
                future.get(15, TimeUnit.SECONDS);
            } catch (Throwable t) {
                log.error("Error running com.dennis.client_milo example: {}", t.getMessage(), t);
                future.completeExceptionally(t);
            }

to


            try {
                clientExample.run(client, future);
//                future.get(15, TimeUnit.SECONDS);
                future.get();
            } catch (Throwable t) {
                log.error("Error running com.dennis.client_milo example: {}", t.getMessage(), t);
                future.completeExceptionally(t);
            }

And the subscription will be carrying on

0DennisLee0 avatar Feb 24 '20 10:02 0DennisLee0