forked from JavaCourse00/JavaCourseCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/JavaCourse00/JavaCourseCodes …
…into main
- Loading branch information
Showing
74 changed files
with
3,301 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.kimmking.kmq; | ||
|
||
import lombok.SneakyThrows; | ||
|
||
import java.io.File; | ||
import java.lang.reflect.Method; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
|
||
public class TestAddUrl { | ||
|
||
@SneakyThrows | ||
public static void main(String[] args) { | ||
URLClassLoader classLoader = (URLClassLoader) TestAddUrl.class.getClassLoader(); | ||
String dir = "/Users/kimmking/Downloads/Hello"; | ||
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); | ||
method.setAccessible(true); | ||
method.invoke(classLoader, new File(dir).toURL()); | ||
|
||
Class klass = Class.forName("Hello",true, classLoader); | ||
Object obj = klass.newInstance(); | ||
Method hello = klass.getDeclaredMethod("hello"); | ||
hello.invoke(obj); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>java0.nio01</groupId> | ||
<artifactId>nio01</artifactId> | ||
<version>1.0</version> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>${maven.compile.source}</source> | ||
<target>${maven.compile.target}</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>3.2.0</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<transformers> | ||
<transformer> | ||
<manifestEntries> | ||
<Main-Class>${app.main.class}</Main-Class> | ||
<X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK> | ||
<X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK> | ||
</manifestEntries> | ||
</transformer> | ||
</transformers> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<properties> | ||
<app.main.class>Main</app.main.class> | ||
<maven.compile.source>8</maven.compile.source> | ||
<maven.compile.target>8</maven.compile.target> | ||
</properties> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import java0.nio01.HttpServer01; | ||
import java0.nio01.HttpServer02; | ||
import java0.nio01.HttpServer03; | ||
import java0.nio01.netty.NettyHttpServer; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
Map<String, Class> map = new HashMap<>(); | ||
map.put("1", HttpServer01.class); | ||
map.put("2", HttpServer02.class); | ||
map.put("3", HttpServer03.class); | ||
map.put("8", NettyHttpServer.class); | ||
|
||
String id = (null == args || args.length == 0) ? "1" : args[0]; | ||
Class clazz = map.get(id); | ||
if( null == clazz ) { | ||
System.out.println("No class for id: " + id); | ||
} | ||
|
||
try { | ||
Method method = clazz.getDeclaredMethod("main", new Class[]{String[].class}); | ||
method.invoke(null, new Object[]{new String[]{}}); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
02nio/nio01/src/main/java/java0/nio01/netty/HttpHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package java0.nio01.netty; | ||
|
||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import io.netty.handler.codec.http.DefaultFullHttpResponse; | ||
import io.netty.handler.codec.http.FullHttpRequest; | ||
import io.netty.handler.codec.http.FullHttpResponse; | ||
import io.netty.handler.codec.http.HttpUtil; | ||
import io.netty.util.ReferenceCountUtil; | ||
|
||
import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; | ||
import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE; | ||
import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT; | ||
import static io.netty.handler.codec.http.HttpResponseStatus.OK; | ||
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; | ||
|
||
public class HttpHandler extends ChannelInboundHandlerAdapter { | ||
|
||
@Override | ||
public void channelReadComplete(ChannelHandlerContext ctx) { | ||
ctx.flush(); | ||
} | ||
|
||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) { | ||
try { | ||
//logger.info("channelRead流量接口请求开始,时间为{}", startTime); | ||
FullHttpRequest fullRequest = (FullHttpRequest) msg; | ||
String uri = fullRequest.uri(); | ||
//logger.info("接收到的请求url为{}", uri); | ||
if (uri.contains("/test")) { | ||
handlerTest(fullRequest, ctx); | ||
} | ||
|
||
} catch(Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
ReferenceCountUtil.release(msg); | ||
} | ||
} | ||
|
||
private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx) { | ||
FullHttpResponse response = null; | ||
try { | ||
String value = null; // "hello,kimmking"; // 对接上次作业的httpclient或者okhttp请求另一个url的响应数据 | ||
|
||
// httpGet ... http://localhost:8801 | ||
// 返回的响应,"hello,nio"; | ||
// value = reponse.... | ||
|
||
response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8"))); | ||
response.headers().set("Content-Type", "application/json"); | ||
response.headers().setInt("Content-Length", response.content().readableBytes()); | ||
|
||
} catch (Exception e) { | ||
System.out.println("处理出错:"+e.getMessage()); | ||
response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT); | ||
} finally { | ||
if (fullRequest != null) { | ||
if (!HttpUtil.isKeepAlive(fullRequest)) { | ||
ctx.write(response).addListener(ChannelFutureListener.CLOSE); | ||
} else { | ||
response.headers().set(CONNECTION, KEEP_ALIVE); | ||
ctx.write(response); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { | ||
cause.printStackTrace(); | ||
ctx.close(); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
02nio/nio01/src/main/java/java0/nio01/netty/HttpInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package java0.nio01.netty; | ||
|
||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.ChannelPipeline; | ||
import io.netty.channel.socket.SocketChannel; | ||
import io.netty.handler.codec.http.HttpObjectAggregator; | ||
import io.netty.handler.codec.http.HttpServerCodec; | ||
|
||
public class HttpInitializer extends ChannelInitializer<SocketChannel> { | ||
|
||
@Override | ||
public void initChannel(SocketChannel ch) { | ||
ChannelPipeline p = ch.pipeline(); | ||
p.addLast(new HttpServerCodec()); | ||
//p.addLast(new HttpServerExpectContinueHandler()); | ||
p.addLast(new HttpObjectAggregator(1024 * 1024)); | ||
p.addLast(new HttpHandler()); | ||
} | ||
} |
Oops, something went wrong.