Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

about disconnect #77

Closed
kliein opened this issue Sep 26, 2024 · 9 comments
Closed

about disconnect #77

kliein opened this issue Sep 26, 2024 · 9 comments

Comments

@kliein
Copy link

kliein commented Sep 26, 2024

How can I detect a disconnected connection? I need to perform some business-level operations when the connection is lost, as well as attempt to reconnect.

@kevinherron
Copy link
Contributor

You can check the status with ModbusClient::isConnected or ModbusClientTransport::isConnected.

The client will automatically reconnect, with some variation of behavior depending on the values of connectPersistent and reconnectLazy in the NettyClientTransportConfig.

@mrksngl
Copy link
Contributor

mrksngl commented Sep 26, 2024

What I do is to use the CompletableFuture returned by e.g. the sendRequest method...

CompletableFuture<> f = modbusMaster.<ReadHoldingRegistersResponse>sendRequest(new ReadHoldingRegistersRequest(…), …)

...and then to handle exceptions on the future, using

f.whenComplete((answer, t) -> {
  if (t != null && t.getCause() instanceof IOException) { … }
});

Looks a bit cumbersome, especially because the exception I'm interested in is a nested exception, but works ;)

The nested exception could also be a ModbusTimeoutException or ModbusResponseException which you could also be interested in.

@kevinherron
Copy link
Contributor

kevinherron commented Sep 26, 2024

My answer was concerning the current 2.0.0-RC3 version of the library.

It seems you're asking about the 1.2.x version? I'm not sure the answer changes much.

I can probably add a listener/callback to NettyTcpClientTransport in the 2.x version.

@kliein
Copy link
Author

kliein commented Sep 26, 2024

You can check the status with ModbusClient::isConnected or ModbusClientTransport::isConnected.

The client will automatically reconnect, with some variation of behavior depending on the values of connectPersistent and reconnectLazy in the NettyClientTransportConfig.

Understood. I have determined online status based on the isConnected() method and set the connectPersistent property to true, and reconnectLazy to false. Setting reconnectLazy to false means it will automatically reconnect if the connection is lost, right? Instead of relying on manual triggering; what is the purpose of the connectPersistent property?

@kliein
Copy link
Author

kliein commented Sep 26, 2024

My answer was concerning the current 2.0.0-RC3 version of the library.

It seems you're asking about the 1.2.x version? I'm not sure the answer changes much.

I can probably add a listener/callback to NettyTcpClientTransport in the 2.x version.

image
it`s 2.0.0-RC3

@kevinherron
Copy link
Contributor

kevinherron commented Sep 26, 2024

When connectPersistent is true it means that even if the initial call to connect() fails it will keep retrying automatically. Otherwise if that initial call fails it will just remain disconnected. Only influences the behavior of the initial call to connect().

reconnectLazy means the reconnect happens the next time the connection is actually needed, instead of happening eagerly/immediately.

@kliein
Copy link
Author

kliein commented Sep 26, 2024

When connectPersistent is true it means that even if the initial call to connect() fails it will keep retrying automatically. Otherwise if that initial call fails it will just remain disconnected. Only influences the behavior of the initial call to connect().

reconnectLazy means the reconnect happens the next time the connection is actually needed, instead of happening eagerly/immediately.

okay,thanks

@kevinherron
Copy link
Contributor

I will soon add a connection listener callback as well: #78

@kliein kliein closed this as completed Sep 26, 2024
@kliein
Copy link
Author

kliein commented Sep 26, 2024

the issue has been solved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants