Skip to content

Commit

Permalink
Fixing autoreconnection issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Marziali committed Apr 16, 2018
1 parent d585327 commit 15ea757
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/hurence/opc/da/OpcDaOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void connect(OpcDaConnectionProfile connectionProfile) {
syncIO = group.getSyncIO();
} catch (Exception e) {
try {
cleanup();
disconnect();
} finally {
throw new OpcException("Unexpected exception occurred while connecting", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,16 @@ public void connect(S connectionProfile) {
executorService = Executors.newSingleThreadExecutor();
shouldKeepAlive = true;
executorService.execute(() -> {
while (awaitDisconnected()) {
while (shouldKeepAlive) {
if (getConnectionState() == ConnectionState.CONNECTED) {
awaitDisconnected();
}
if (shouldKeepAlive) {
logger.info("Detected disconnection. Triggering reconnection");
try {
connect(connectionProfile);
} catch (OpcException e) {
awaitConnected();
} catch (Exception e) {
logger.warn("Error while reconnecting. Retrying in 1 second", e);
try {
Thread.sleep(1000);
Expand Down
1 change: 0 additions & 1 deletion src/test/java/com/hurence/opc/da/OpcDaOperationsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public void testWriteValuesFails() {
public void testAutoReconnect() throws Exception {
AutoReconnectOpcOperations autoReconnectOpcOperations = new AutoReconnectOpcOperations(opcDaOperations);
opcDaOperations.disconnect();

autoReconnectOpcOperations.connect(connectionProfile);
Assert.assertTrue(autoReconnectOpcOperations.awaitConnected());
//force disconnect
Expand Down

0 comments on commit 15ea757

Please sign in to comment.