diff --git a/02nio/nio02/README.md b/02nio/nio02/README.md index 82214410..f244b0dd 100644 --- a/02nio/nio02/README.md +++ b/02nio/nio02/README.md @@ -1,6 +1,9 @@ # netty-gateway -``` - +> 说明:由于本机环境问题,将项目的 proxyPort 修改为 9999 + +### 作业 + +- 第一题:使用OkHttp实现,修改了 HttpInboundHandler ,修改了 OkhttpOutboundHandler +- 第二题:修改了 HttpInboundHandler ,修改了 OkhttpOutboundHandler,增加 HeaderRequestFilter -``` \ No newline at end of file diff --git a/02nio/nio02/pom.xml b/02nio/nio02/pom.xml index 6cbbeffd..89d85cfe 100644 --- a/02nio/nio02/pom.xml +++ b/02nio/nio02/pom.xml @@ -52,7 +52,12 @@ httpasyncclient 4.1.4 - + + com.squareup.okhttp3 + okhttp + 3.8.1 + + okhttp header + Headers.Builder headersBuilder = new Headers.Builder(); + fullRequest.headers().forEach(stringStringEntry -> { + headersBuilder.add(stringStringEntry.getKey(), stringStringEntry.getValue()) + ; + }); + Headers headers = headersBuilder.build(); + + Request request = new Request.Builder() + .url(url) + .headers(headers) + .build(); + FullHttpResponse response = null; + try (Response endponitResponse = httpClient.newCall(request).execute()) { + if (!endponitResponse.isSuccessful()) throw new IOException("Unexpected code " + endponitResponse); + Headers responseHeaders = endponitResponse.headers(); + for (int i = 0; i < responseHeaders.size(); i++) { + System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i)); + } +// System.out.println(endponitResponse.body().string()); + response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(endponitResponse.body().bytes())); + response.headers().set("Content-Type", "application/json"); + response.headers().setInt("Content-Length", Integer.parseInt(endponitResponse.headers().get("Content-Length"))); + } catch (IOException e){ + e.printStackTrace(); + response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT); + e.printStackTrace(); + ctx.close(); + } finally { + + if (fullRequest != null) { + if (!HttpUtil.isKeepAlive(fullRequest)) { + ctx.write(response).addListener(ChannelFutureListener.CLOSE); + } else { + //response.headers().set(CONNECTION, KEEP_ALIVE); + ctx.write(response); + } + } + ctx.flush(); + //ctx.close(); + } + } } diff --git a/03concurrency/0301/src/main/java/java0/conc0301/DaemonThread.java b/03concurrency/0301/src/main/java/java0/conc0301/DaemonThread.java index 5a00ab24..35683ba5 100644 --- a/03concurrency/0301/src/main/java/java0/conc0301/DaemonThread.java +++ b/03concurrency/0301/src/main/java/java0/conc0301/DaemonThread.java @@ -2,7 +2,7 @@ public class DaemonThread { - public static void main(String[] args) { + public static void main(String[] args) throws InterruptedException { Runnable task = new Runnable() { @Override public void run() { @@ -19,6 +19,7 @@ public void run() { thread.setName("test-thread-1"); thread.setDaemon(false); thread.start(); + Thread.currentThread().sleep(6000); } diff --git a/03concurrency/0301/src/main/java/java0/conc0303/Homework03.java b/03concurrency/0301/src/main/java/java0/conc0303/Homework03.java index dd7d4375..b5fa7981 100644 --- a/03concurrency/0301/src/main/java/java0/conc0303/Homework03.java +++ b/03concurrency/0301/src/main/java/java0/conc0303/Homework03.java @@ -1,5 +1,8 @@ package java0.conc0303; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.FutureTask; import java.util.concurrent.CountDownLatch; /** @@ -11,17 +14,26 @@ */ public class Homework03 { - public static void main(String[] args) { - + public static void main(String[] args) throws InterruptedException, ExecutionException { + long start=System.currentTimeMillis(); + // 在这里创建一个线程或线程池, // 异步执行 下面方法 + + FutureTask task = new FutureTask(new Callable() { + @Override + public Integer call() throws Exception { + return sum(); + } + }); + + new Thread(task).start(); - int result = sum(); //这是得到的返回值 + int result = task.get(); //这是得到的返回值 // 确保 拿到result 并输出 System.out.println("异步计算结果为:"+result); - System.out.println("使用时间:"+ (System.currentTimeMillis()-start) + " ms"); // 然后退出main线程