use of java.net.Socket in project camel by apache.
the class JavaSocketTests method testSocketReadOnClosedConnection.
@Test
public void testSocketReadOnClosedConnection() throws Exception {
final Thread acceptThread = new Thread() {
Logger log = LoggerFactory.getLogger("acceptThread");
@Override
public void run() {
boolean running = true;
try {
Socket echoSocket = serverSocket.accept();
log.info("Accepted connection: {}", echoSocket.getInetAddress());
echoSocket.setSoTimeout(2000);
while (echoSocket.isConnected() && !echoSocket.isClosed()) {
StringBuilder responseBuilder = new StringBuilder(500);
InputStream reader = echoSocket.getInputStream();
OutputStream writer = echoSocket.getOutputStream();
do {
int readByte = -1;
int available = -1;
try {
available = reader.available();
log.info("InputStream.available returned {}", available);
readByte = reader.read();
log.trace("Processing byte: {}", readByte);
switch(readByte) {
case -1:
if (echoSocket.isConnected() && !echoSocket.isClosed()) {
log.info("Available returned {}", reader.available());
log.warn("Socket claims to still be open, but END_OF_STREAM received - closing echoSocket");
try {
echoSocket.close();
} catch (Exception ex) {
log.warn("Exception encountered closing echoSocket after END_OF_STREAM received", ex);
}
}
running = false;
break;
case 10:
log.info("Complete Message - Sending Response");
byte[] response = responseBuilder.toString().getBytes();
responseBuilder.setLength(0);
writer.write(response, 0, response.length);
writer.write('\n');
break;
default:
responseBuilder.append((char) readByte);
}
} catch (SocketTimeoutException timeoutEx) {
log.info("Timeout reading data - available returned {}", available);
}
} while (echoSocket.isConnected() && !echoSocket.isClosed());
}
} catch (IOException ioEx) {
log.error("IOException in run method", ioEx);
} finally {
try {
serverSocket.close();
} catch (IOException ioEx) {
log.error("Exception encountered closing server socket", ioEx);
}
}
log.info("Finished processing connection");
}
};
acceptThread.start();
clientSocket = new Socket();
clientSocket.setSoTimeout(1000);
clientSocket.connect(serverSocket.getLocalSocketAddress(), 10000);
clientSocket.setTcpNoDelay(true);
log.info("Begining message send loop ");
byte[] message = "Hello World".getBytes();
BufferedReader reader;
for (int i = 1; i <= messageCount; ++i) {
reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
OutputStream writer = clientSocket.getOutputStream();
log.info("Sending payload");
writer.write(message, 0, message.length);
writer.flush();
log.info("Sending terminator");
writer.write('\n');
writer.flush();
log.info("Received Response #{}: {}", i, reader.readLine());
Thread.sleep(1000);
}
log.info("Message send loop complete - closing connection");
// Javadoc for Socket says closing the InputStream will close the connection
clientSocket.getInputStream().close();
if (!clientSocket.isClosed()) {
log.warn("Closing input stream didn't close socket");
clientSocket.close();
}
log.info("Sleeping ...");
Thread.sleep(5000);
}
use of java.net.Socket in project flink by apache.
the class ZooKeeperLeaderRetrievalTest method testConnectingAddressRetrievalWithDelayedLeaderElection.
/**
* Tests that LeaderRetrievalUtils.findConnectingAdress finds the correct connecting address
* in case of an old leader address in ZooKeeper and a subsequent election of a new leader.
* The findConnectingAddress should block until the new leader has been elected and his
* address has been written to ZooKeeper.
*/
@Test
public void testConnectingAddressRetrievalWithDelayedLeaderElection() throws Exception {
FiniteDuration timeout = new FiniteDuration(1, TimeUnit.MINUTES);
Configuration config = new Configuration();
long sleepingTime = 1000;
config.setString(HighAvailabilityOptions.HA_MODE, "zookeeper");
config.setString(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM, testingServer.getConnectString());
LeaderElectionService leaderElectionService = null;
LeaderElectionService faultyLeaderElectionService;
ServerSocket serverSocket;
InetAddress localHost;
Thread thread;
CuratorFramework[] client = new CuratorFramework[2];
try {
client[0] = ZooKeeperUtils.startCuratorFramework(config);
client[1] = ZooKeeperUtils.startCuratorFramework(config);
String wrongHostPort = NetUtils.unresolvedHostAndPortToNormalizedString("1.1.1.1", 1234);
String wrongAddress = JobManager.getRemoteJobManagerAkkaURL(AkkaUtils.getAkkaProtocol(config), wrongHostPort, Option.<String>empty());
try {
localHost = InetAddress.getLocalHost();
serverSocket = new ServerSocket(0, 50, localHost);
} catch (UnknownHostException e) {
// may happen if disconnected. skip test.
System.err.println("Skipping 'testNetworkInterfaceSelection' test.");
return;
} catch (IOException e) {
// may happen in certain test setups, skip test.
System.err.println("Skipping 'testNetworkInterfaceSelection' test.");
return;
}
InetSocketAddress correctInetSocketAddress = new InetSocketAddress(localHost, serverSocket.getLocalPort());
String hostPort = NetUtils.unresolvedHostAndPortToNormalizedString(localHost.getHostName(), correctInetSocketAddress.getPort());
String correctAddress = JobManager.getRemoteJobManagerAkkaURL(AkkaUtils.getAkkaProtocol(config), hostPort, Option.<String>empty());
faultyLeaderElectionService = ZooKeeperUtils.createLeaderElectionService(client[0], config);
TestingContender wrongLeaderAddressContender = new TestingContender(wrongAddress, faultyLeaderElectionService);
faultyLeaderElectionService.start(wrongLeaderAddressContender);
FindConnectingAddress findConnectingAddress = new FindConnectingAddress(config, timeout);
thread = new Thread(findConnectingAddress);
thread.start();
leaderElectionService = ZooKeeperUtils.createLeaderElectionService(client[1], config);
TestingContender correctLeaderAddressContender = new TestingContender(correctAddress, leaderElectionService);
Thread.sleep(sleepingTime);
faultyLeaderElectionService.stop();
leaderElectionService.start(correctLeaderAddressContender);
thread.join();
InetAddress result = findConnectingAddress.getInetAddress();
// check that we can connect to the localHost
Socket socket = new Socket();
try {
// port 0 = let the OS choose the port
SocketAddress bindP = new InetSocketAddress(result, 0);
// machine
socket.bind(bindP);
socket.connect(correctInetSocketAddress, 1000);
} finally {
socket.close();
}
} finally {
if (leaderElectionService != null) {
leaderElectionService.stop();
}
if (client[0] != null) {
client[0].close();
}
if (client[1] != null) {
client[1].close();
}
}
}
use of java.net.Socket in project flink by apache.
the class SocketTextStreamFunction method cancel.
@Override
public void cancel() {
isRunning = false;
// we need to close the socket as well, because the Thread.interrupt() function will
// not wake the thread in the socketStream.read() method when blocked.
Socket theSocket = this.currentSocket;
if (theSocket != null) {
IOUtils.closeSocket(theSocket);
}
}
use of java.net.Socket in project flink by apache.
the class SocketTextStreamFunction method run.
@Override
public void run(SourceContext<String> ctx) throws Exception {
final StringBuilder buffer = new StringBuilder();
long attempt = 0;
while (isRunning) {
try (Socket socket = new Socket()) {
currentSocket = socket;
LOG.info("Connecting to server socket " + hostname + ':' + port);
socket.connect(new InetSocketAddress(hostname, port), CONNECTION_TIMEOUT_TIME);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
char[] cbuf = new char[8192];
int bytesRead;
while (isRunning && (bytesRead = reader.read(cbuf)) != -1) {
buffer.append(cbuf, 0, bytesRead);
int delimPos;
while (buffer.length() >= delimiter.length() && (delimPos = buffer.indexOf(delimiter)) != -1) {
String record = buffer.substring(0, delimPos);
// truncate trailing carriage return
if (delimiter.equals("\n") && record.endsWith("\r")) {
record = record.substring(0, record.length() - 1);
}
ctx.collect(record);
buffer.delete(0, delimPos + delimiter.length());
}
}
}
// if we dropped out of this loop due to an EOF, sleep and retry
if (isRunning) {
attempt++;
if (maxNumRetries == -1 || attempt < maxNumRetries) {
LOG.warn("Lost connection to server socket. Retrying in " + delayBetweenRetries + " msecs...");
Thread.sleep(delayBetweenRetries);
} else {
// throw new EOFException("Reached end of stream and reconnects are not enabled.");
break;
}
}
}
// collect trailing data
if (buffer.length() > 0) {
ctx.collect(buffer.toString());
}
}
use of java.net.Socket in project flink by apache.
the class SocketClientSinkTest method testSocketSink.
@Test
public void testSocketSink() throws Exception {
final ServerSocket server = new ServerSocket(0);
final int port = server.getLocalPort();
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
Thread sinkRunner = new Thread("Test sink runner") {
@Override
public void run() {
try {
SocketClientSink<String> simpleSink = new SocketClientSink<>(host, port, simpleSchema, 0);
simpleSink.open(new Configuration());
simpleSink.invoke(TEST_MESSAGE + '\n');
simpleSink.close();
} catch (Throwable t) {
error.set(t);
}
}
};
sinkRunner.start();
Socket sk = server.accept();
BufferedReader rdr = new BufferedReader(new InputStreamReader(sk.getInputStream()));
String value = rdr.readLine();
sinkRunner.join();
server.close();
if (error.get() != null) {
Throwable t = error.get();
t.printStackTrace();
fail("Error in spawned thread: " + t.getMessage());
}
assertEquals(TEST_MESSAGE, value);
}
Aggregations