Skip to content

Commit

Permalink
Merge pull request #57 from kamshory/Multiple-Modem
Browse files Browse the repository at this point in the history
Prevent Duplicated WebSocket Client
  • Loading branch information
kamshory authored Jun 27, 2021
2 parents ec3905c + 4ee0553 commit 0819e00
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/planetbiru/ClientReceiverWS.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.planetbiru;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -79,4 +80,10 @@ public void init()
this.tool.start();
}

@PreDestroy
public void stopThread()
{
this.tool.stopThread();
}

}
4 changes: 4 additions & 0 deletions src/main/java/com/planetbiru/receiver/ws/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public void run()
}
while(!connected && !isStoped());
}
public void stopThreade()
{
this.stoped = true;
}
public static void sleep(long interval)
{
try
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/com/planetbiru/receiver/ws/WebSocketTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public class WebSocketTool extends Thread{
private WebSocketClient client;
private long reconnectDelay = 10000;
private boolean stopend = false;

public WebSocketTool(long reconnectDelay)
{
Expand All @@ -21,19 +22,28 @@ public void run()
this.client.start();
}
}

public void stopThread()
{
this.stopend = true;
this.client.stopThreade();
}

public void restartThread() {
try
if(!this.stopend)
{
Thread.sleep(this.reconnectDelay);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
try
{
Thread.sleep(this.reconnectDelay);
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
this.client = new WebSocketClient(this);
this.client.setStoped(false);
this.client.start();
}
this.client = new WebSocketClient(this);
this.client.setStoped(false);
this.client.start();
}
}

0 comments on commit 0819e00

Please sign in to comment.