@Override public boolean cancel(boolean mayInterruptIfRunning) { if (mayInterruptIfRunning) { messageProducer.close(); } return result.cancel(mayInterruptIfRunning); }
/** * Parse a header attribute and set the corresponding data in the {@link Headers} fields. */ protected void parseHeader(String line, Headers headers) { int sepIndex = line.indexOf(':'); if (sepIndex >= 0) { String key = line.substring(0, sepIndex).trim(); switch (key) { case CONTENT_LENGTH_HEADER: try { headers.contentLength = Integer.parseInt(line.substring(sepIndex + 1).trim()); } catch (NumberFormatException e) { fireError(e); } break; case CONTENT_TYPE_HEADER: { int charsetIndex = line.indexOf("charset="); if (charsetIndex >= 0) headers.charset = line.substring(charsetIndex + 8).trim(); break; } } } }
@Test public void testStopOnSocketClosed() throws Throwable { executorService.submit(() -> { InputStream input = new InputStream() { @Override public int read() throws IOException { throw new SocketException("Socket closed"); } }; MessageJsonHandler jsonHandler = new MessageJsonHandler(Collections.emptyMap()); StreamMessageProducer messageProducer = new StreamMessageProducer(input, jsonHandler); messageProducer.listen(message -> {}); messageProducer.close(); }).get(TIMEOUT, TimeUnit.MILLISECONDS); }
fireError(new IllegalStateException("Missing header " + CONTENT_LENGTH_HEADER + " in input \"" + debugBuilder + "\"")); } else { boolean result = handleMessage(input, headers); if (!result) keepRunning = false; } else if (headerBuilder != null) { parseHeader(headerBuilder.toString(), headers); headerBuilder = null; fireStreamClosed(exception); else throw new JsonRpcException(exception);
StreamMessageProducer reader = new StreamMessageProducer(input, jsonHandler, remoteEndpoint); MessageConsumer messageConsumer = wrapMessageConsumer(remoteEndpoint); ExecutorService execService = executorService != null ? executorService : Executors.newCachedThreadPool();
@Test public void testStopOnInterrrupt() throws Exception { executorService.submit(() -> { InputStream input = new InputStream() { @Override public int read() throws IOException { throw new InterruptedIOException(); } }; MessageJsonHandler jsonHandler = new MessageJsonHandler(Collections.emptyMap()); StreamMessageProducer messageProducer = new StreamMessageProducer(input, jsonHandler); messageProducer.listen(message -> {}); messageProducer.close(); }).get(TIMEOUT, TimeUnit.MILLISECONDS); }
fireError(new IllegalStateException("Missing header " + CONTENT_LENGTH_HEADER + " in input \"" + debugBuilder + "\"")); } else { boolean result = handleMessage(input, headers); if (!result) keepRunning = false; } else if (headerBuilder != null) { parseHeader(headerBuilder.toString(), headers); headerBuilder = null; fireStreamClosed(exception); else throw new JsonRpcException(exception);
remoteProxy = (T) ServiceEndpoints.toServiceObject(remoteEndpoint, (Collection<Class<?>>) (Object) remoteInterfaces, classLoader); StreamMessageProducer reader = new StreamMessageProducer(input, jsonHandler, remoteEndpoint); MessageConsumer messageConsumer = wrapMessageConsumer(remoteEndpoint); ExecutorService execService = executorService != null ? executorService : Executors.newCachedThreadPool();
@Test public void testStopOnChannelClosed() throws Exception { executorService.submit(() -> { InputStream input = new InputStream() { @Override public int read() throws IOException { throw new ClosedChannelException(); } }; MessageJsonHandler jsonHandler = new MessageJsonHandler(Collections.emptyMap()); StreamMessageProducer messageProducer = new StreamMessageProducer(input, jsonHandler); messageProducer.listen(message -> {}); messageProducer.close(); }).get(TIMEOUT, TimeUnit.MILLISECONDS); }
/** * Parse a header attribute and set the corresponding data in the {@link Headers} fields. */ protected void parseHeader(String line, Headers headers) { int sepIndex = line.indexOf(':'); if (sepIndex >= 0) { String key = line.substring(0, sepIndex).trim(); switch (key) { case CONTENT_LENGTH_HEADER: try { headers.contentLength = Integer.parseInt(line.substring(sepIndex + 1).trim()); } catch (NumberFormatException e) { fireError(e); } break; case CONTENT_TYPE_HEADER: { int charsetIndex = line.indexOf("charset="); if (charsetIndex >= 0) headers.charset = line.substring(charsetIndex + 8).trim(); break; } } } }
@Test(expected=JsonRpcException.class) public void testIOException() throws Throwable { try { executorService.submit(() -> { InputStream input = new InputStream() { @Override public int read() throws IOException { throw new SocketException("Permission denied: connect"); } }; MessageJsonHandler jsonHandler = new MessageJsonHandler(Collections.emptyMap()); StreamMessageProducer messageProducer = new StreamMessageProducer(input, jsonHandler); messageProducer.listen(message -> {}); messageProducer.close(); }).get(TIMEOUT, TimeUnit.MILLISECONDS); } catch (ExecutionException e) { throw e.getCause(); } }