|
25 | 25 | import org.apache.flink.streaming.api.functions.source.SourceFunction; |
26 | 26 | import org.apache.flink.types.Row; |
27 | 27 | import org.apache.flink.util.IOUtils; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
28 | 30 |
|
29 | 31 | import java.io.BufferedReader; |
30 | 32 | import java.io.IOException; |
|
42 | 44 | * @author maqi |
43 | 45 | */ |
44 | 46 | public class CustomerSocketTextStreamFunction implements SourceFunction<Row> { |
| 47 | + private static final Logger LOG = LoggerFactory.getLogger(CustomerSocketTextStreamFunction.class); |
45 | 48 |
|
46 | 49 | /** |
47 | 50 | * Default delay between successive connection attempts. |
48 | 51 | */ |
49 | | - private static final int DEFAULT_CONNECTION_RETRY_SLEEP = 500; |
| 52 | + private static final int DEFAULT_CONNECTION_RETRY_SLEEP = 2000; |
50 | 53 |
|
51 | 54 | /** |
52 | 55 | * Default connection timeout when connecting to the server socket (infinite). |
@@ -92,32 +95,33 @@ public void run(SourceContext<Row> ctx) throws Exception { |
92 | 95 | long attempt = 0; |
93 | 96 |
|
94 | 97 | while (isRunning) { |
95 | | - |
96 | | - try (Socket socket = new Socket()) { |
| 98 | + try { |
| 99 | + Socket socket = new Socket(); |
97 | 100 | currentSocket = socket; |
98 | | - |
99 | 101 | socket.connect(new InetSocketAddress(tableInfo.getHostname(), tableInfo.getPort()), CONNECTION_TIMEOUT_TIME); |
100 | | - try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { |
101 | | - |
102 | | - char[] cbuf = new char[8192]; |
103 | | - int bytesRead; |
104 | | - while (isRunning && (bytesRead = reader.read(cbuf)) != -1) { |
105 | | - buffer.append(cbuf, 0, bytesRead); |
106 | | - int delimPos; |
107 | | - String delimiter = tableInfo.getDelimiter(); |
108 | | - while (buffer.length() >= delimiter.length() && (delimPos = buffer.indexOf(delimiter)) != -1) { |
109 | | - String record = buffer.substring(0, delimPos); |
110 | | - // truncate trailing carriage return |
111 | | - if (delimiter.equals("\n") && record.endsWith("\r")) { |
112 | | - record = record.substring(0, record.length() - 1); |
113 | | - } |
114 | | - ctx.collect(convertToRow(record)); |
115 | | - buffer.delete(0, delimPos + delimiter.length()); |
| 102 | + |
| 103 | + BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
| 104 | + char[] cbuf = new char[8192]; |
| 105 | + int bytesRead; |
| 106 | + while (isRunning && (bytesRead = reader.read(cbuf)) != -1) { |
| 107 | + buffer.append(cbuf, 0, bytesRead); |
| 108 | + int delimPos; |
| 109 | + String delimiter = tableInfo.getDelimiter(); |
| 110 | + while (buffer.length() >= delimiter.length() && (delimPos = buffer.indexOf(delimiter)) != -1) { |
| 111 | + String record = buffer.substring(0, delimPos); |
| 112 | + // truncate trailing carriage return |
| 113 | + if (delimiter.equals("\n") && record.endsWith("\r")) { |
| 114 | + record = record.substring(0, record.length() - 1); |
116 | 115 | } |
| 116 | + ctx.collect(convertToRow(record)); |
| 117 | + buffer.delete(0, delimPos + delimiter.length()); |
117 | 118 | } |
118 | 119 | } |
| 120 | + } catch (Exception e) { |
| 121 | + LOG.info("Connection server failed, Please check configuration !!!!!!!!!!!!!!!!"); |
119 | 122 | } |
120 | 123 |
|
| 124 | + |
121 | 125 | // if we dropped out of this loop due to an EOF, sleep and retry |
122 | 126 | if (isRunning) { |
123 | 127 | attempt++; |
|
0 commit comments