20
20
import java .io .OutputStream ;
21
21
import java .net .InetSocketAddress ;
22
22
import java .net .Socket ;
23
- import java .util .LinkedList ;
24
23
import lombok .extern .slf4j .Slf4j ;
25
24
26
25
/** A client that would connect to a TCP socket. */
@@ -30,8 +29,6 @@ public class TCPClient implements SocketClient {
30
29
private final Endpoint endpoint ;
31
30
private Socket socket ;
32
31
private boolean shouldConnect = true ;
33
- private final LinkedList <String > retryQueue = new LinkedList <>();
34
- private String lastMessageSent ;
35
32
36
33
public TCPClient (Endpoint endpoint ) {
37
34
this .endpoint = endpoint ;
@@ -61,28 +58,17 @@ public synchronized void sendMessage(String message) {
61
58
os = socket .getOutputStream ();
62
59
} catch (IOException e ) {
63
60
shouldConnect = true ;
64
- retryQueue .add (message );
65
61
throw new RuntimeException (
66
62
"Failed to write message to the socket. Failed to open output stream." , e );
67
63
}
68
64
69
65
try {
70
- while (!retryQueue .isEmpty ()) {
71
- String retryMessage = retryQueue .peek ();
72
- os .write (retryMessage .getBytes ());
73
- retryQueue .pop ();
74
- }
66
+ // Write a space to the socket to verify connection before sending event
67
+ os .write (32 );
75
68
76
69
os .write (message .getBytes ());
77
- lastMessageSent = message ;
78
70
} catch (IOException e ) {
79
- // For broken pipe exception, retry last sent message
80
- if (e .getMessage ().contains ("Broken pipe" )) {
81
- retryQueue .add (lastMessageSent );
82
- }
83
-
84
71
shouldConnect = true ;
85
- retryQueue .add (message );
86
72
throw new RuntimeException ("Failed to write message to the socket." , e );
87
73
}
88
74
}
0 commit comments