Skip to content

Commit 67e96e8

Browse files
committed
refactor ServerEventsClient so interrupting bgThread it will throw InterruptedException
1 parent 12948e4 commit 67e96e8

File tree

2 files changed

+52
-24
lines changed

2 files changed

+52
-24
lines changed

src/AndroidClient/android/src/main/java/net/servicestack/client/sse/ServerEventsClient.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.FileNotFoundException;
1717
import java.io.IOException;
1818
import java.io.InputStream;
19+
import java.io.InterruptedIOException;
1920
import java.io.UnsupportedEncodingException;
2021
import java.lang.reflect.Method;
2122
import java.lang.reflect.Modifier;
@@ -274,11 +275,18 @@ public String getConnectionDisplayName() {
274275
: "(not connected)";
275276
}
276277

277-
public ServerEventsClient start(){
278+
private synchronized void interruptBackgroundThread() {
278279
if (bgThread != null){
279280
bgThread.interrupt();
281+
try {
282+
bgThread.join();
283+
} catch (InterruptedException ignore) {}
280284
bgThread = null;
281285
}
286+
}
287+
288+
public ServerEventsClient start(){
289+
interruptBackgroundThread();
282290

283291
stopped.set(false);
284292
bgThread = new Thread(new EventStream(this));
@@ -359,10 +367,7 @@ private synchronized void internalStop() {
359367
}
360368

361369
connectionInfo = null;
362-
if (bgThread != null)
363-
bgThread.interrupt();
364-
365-
bgThread = null;
370+
interruptBackgroundThread();
366371
}
367372

368373
private void onCommandReceived(ServerEventMessage e) {
@@ -564,26 +569,35 @@ public void run() {
564569
InputStream is = new BufferedInputStream(req.getInputStream());
565570
errorsCount.set(0);
566571
readStream(is);
567-
568-
running.set(false);
572+
} catch (InterruptedException ie){
573+
Log.i("EventStream.run(): Caught InterruptedException"); //thrown by interruptBackgroundThread()
574+
return;
569575
} catch (Exception e) {
570576
Log.e("Error reading from event-stream, continuous errors: " + errorsCount.incrementAndGet(), e);
571577
Log.e(Utils.getStackTrace(e));
572-
running.set(false);
573578
} finally {
574-
if (!running.get()){
575-
client.restart();
576-
}
579+
running.set(false);
580+
}
581+
582+
if (!running.get()){
583+
client.restart();
577584
}
578585
}
579586

580-
private void readStream(InputStream inputStream) throws IOException {
587+
private void readStream(InputStream inputStream) throws IOException, InterruptedException {
581588
byte[] buffer = new byte[BufferSize];
582589
String overflowText = "";
583590

584591
int len = 0;
585592
while (true) {
593+
while (true) {
594+
int available = inputStream.available();
595+
if (available > 0) break;
596+
Thread.sleep(5);
597+
}
598+
586599
len = inputStream.read(buffer);
600+
587601
if (len <= 0)
588602
break;
589603

src/AndroidClient/client/src/main/java/net/servicestack/client/sse/ServerEventsClient.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.FileNotFoundException;
1717
import java.io.IOException;
1818
import java.io.InputStream;
19+
import java.io.InterruptedIOException;
1920
import java.io.UnsupportedEncodingException;
2021
import java.lang.reflect.Method;
2122
import java.lang.reflect.Modifier;
@@ -274,11 +275,18 @@ public String getConnectionDisplayName() {
274275
: "(not connected)";
275276
}
276277

277-
public ServerEventsClient start(){
278+
private synchronized void interruptBackgroundThread() {
278279
if (bgThread != null){
279280
bgThread.interrupt();
281+
try {
282+
bgThread.join();
283+
} catch (InterruptedException ignore) {}
280284
bgThread = null;
281285
}
286+
}
287+
288+
public ServerEventsClient start(){
289+
interruptBackgroundThread();
282290

283291
stopped.set(false);
284292
bgThread = new Thread(new EventStream(this));
@@ -359,10 +367,7 @@ private synchronized void internalStop() {
359367
}
360368

361369
connectionInfo = null;
362-
if (bgThread != null)
363-
bgThread.interrupt();
364-
365-
bgThread = null;
370+
interruptBackgroundThread();
366371
}
367372

368373
private void onCommandReceived(ServerEventMessage e) {
@@ -564,26 +569,35 @@ public void run() {
564569
InputStream is = new BufferedInputStream(req.getInputStream());
565570
errorsCount.set(0);
566571
readStream(is);
567-
568-
running.set(false);
572+
} catch (InterruptedException ie){
573+
Log.i("EventStream.run(): Caught InterruptedException"); //thrown by interruptBackgroundThread()
574+
return;
569575
} catch (Exception e) {
570576
Log.e("Error reading from event-stream, continuous errors: " + errorsCount.incrementAndGet(), e);
571577
Log.e(Utils.getStackTrace(e));
572-
running.set(false);
573578
} finally {
574-
if (!running.get()){
575-
client.restart();
576-
}
579+
running.set(false);
580+
}
581+
582+
if (!running.get()){
583+
client.restart();
577584
}
578585
}
579586

580-
private void readStream(InputStream inputStream) throws IOException {
587+
private void readStream(InputStream inputStream) throws IOException, InterruptedException {
581588
byte[] buffer = new byte[BufferSize];
582589
String overflowText = "";
583590

584591
int len = 0;
585592
while (true) {
593+
while (true) {
594+
int available = inputStream.available();
595+
if (available > 0) break;
596+
Thread.sleep(5);
597+
}
598+
586599
len = inputStream.read(buffer);
600+
587601
if (len <= 0)
588602
break;
589603

0 commit comments

Comments
 (0)