Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b81101d

Browse filesBrowse files
committed
add netty demo
1 parent 9e01a70 commit b81101d
Copy full SHA for b81101d

File tree

Expand file treeCollapse file tree

10 files changed

+245
-11
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

10 files changed

+245
-11
lines changed
Open diff view settings
Collapse file

‎JdkLearn/pom.xml‎

Copy file name to clipboardExpand all lines: JdkLearn/pom.xml
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020

2121
<dependencies>
2222

23+
<!-- netty -->
24+
<dependency>
25+
<groupId>io.netty</groupId>
26+
<artifactId>netty-all</artifactId>
27+
<version>4.1.6.Final</version>
28+
</dependency>
29+
2330
<!-- redis依赖 -->
2431
<dependency>
2532
<groupId>org.springframework.boot</groupId>
Collapse file

‎…va/com/learnjava/nettysource/Client.java‎ ‎…a/com/learnjava/io/netty/bio/Client.java‎JdkLearn/src/main/java/com/learnjava/nettysource/Client.java renamed to JdkLearn/src/main/java/com/learnjava/io/netty/bio/Client.java JdkLearn/src/main/java/com/learnjava/nettysource/Client.java renamed to JdkLearn/src/main/java/com/learnjava/io/netty/bio/Client.java

Copy file name to clipboardExpand all lines: JdkLearn/src/main/java/com/learnjava/io/netty/bio/Client.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.learnjava.nettysource;
1+
package com.learnjava.io.netty.bio;
22

33
import java.io.IOException;
44
import java.net.Socket;
Collapse file

‎…learnjava/nettysource/ClientHandler.java‎ ‎…earnjava/io/netty/bio/ClientHandler.java‎JdkLearn/src/main/java/com/learnjava/nettysource/ClientHandler.java renamed to JdkLearn/src/main/java/com/learnjava/io/netty/bio/ClientHandler.java JdkLearn/src/main/java/com/learnjava/nettysource/ClientHandler.java renamed to JdkLearn/src/main/java/com/learnjava/io/netty/bio/ClientHandler.java

Copy file name to clipboardExpand all lines: JdkLearn/src/main/java/com/learnjava/io/netty/bio/ClientHandler.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.learnjava.nettysource;
1+
package com.learnjava.io.netty.bio;
22

33
import java.io.IOException;
44
import java.io.InputStream;
Collapse file

‎…va/com/learnjava/nettysource/Server.java‎ ‎…a/com/learnjava/io/netty/bio/Server.java‎JdkLearn/src/main/java/com/learnjava/nettysource/Server.java renamed to JdkLearn/src/main/java/com/learnjava/io/netty/bio/Server.java JdkLearn/src/main/java/com/learnjava/nettysource/Server.java renamed to JdkLearn/src/main/java/com/learnjava/io/netty/bio/Server.java

Copy file name to clipboardExpand all lines: JdkLearn/src/main/java/com/learnjava/io/netty/bio/Server.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.learnjava.nettysource;
1+
package com.learnjava.io.netty.bio;
22

33
import java.io.IOException;
44
import java.net.ServerSocket;
Collapse file

‎…om/learnjava/nettysource/ServerBoot.java‎ ‎…m/learnjava/io/netty/bio/ServerBoot.java‎JdkLearn/src/main/java/com/learnjava/nettysource/ServerBoot.java renamed to JdkLearn/src/main/java/com/learnjava/io/netty/bio/ServerBoot.java JdkLearn/src/main/java/com/learnjava/nettysource/ServerBoot.java renamed to JdkLearn/src/main/java/com/learnjava/io/netty/bio/ServerBoot.java

Copy file name to clipboardExpand all lines: JdkLearn/src/main/java/com/learnjava/io/netty/bio/ServerBoot.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.learnjava.nettysource;
1+
package com.learnjava.io.netty.bio;
22

33

44
/**
Collapse file
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.learnjava.io.netty.demo01;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.*;
5+
import io.netty.channel.ChannelOption;
6+
import io.netty.channel.EventLoopGroup;
7+
import io.netty.channel.nio.NioEventLoopGroup;
8+
import io.netty.channel.socket.SocketChannel;
9+
import io.netty.channel.socket.nio.NioServerSocketChannel;
10+
import io.netty.util.AttributeKey;
11+
12+
/**
13+
* @author lhy
14+
* @date 2021/5/21
15+
*/
16+
public class Server {
17+
18+
public static void main(String[] args) throws Exception {
19+
20+
/**
21+
* EventLoopGroup:
22+
* NioEventLoopGruop
23+
* NioEventLoop
24+
*/
25+
26+
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
27+
EventLoopGroup workerGroup = new NioEventLoopGroup();
28+
29+
try {
30+
ServerBootstrap bootstrap = new ServerBootstrap();
31+
bootstrap.group(bossGroup, workerGroup)
32+
.channel(NioServerSocketChannel.class)
33+
.childOption(ChannelOption.TCP_NODELAY, true)
34+
.childAttr(AttributeKey.newInstance("childAttr"), "childAttrValue")
35+
.handler(new ServerHandler())
36+
.childHandler(new ChannelInitializer<SocketChannel>() {
37+
@Override
38+
public void initChannel(SocketChannel ch) {
39+
// ch.pipeline().addLast(new AuthHandler());
40+
//..
41+
42+
}
43+
});
44+
45+
ChannelFuture future = bootstrap.bind(8888).sync();
46+
47+
future.channel().closeFuture().sync();
48+
} finally {
49+
bossGroup.shutdownGracefully();
50+
workerGroup.shutdownGracefully();
51+
}
52+
}
53+
}
Collapse file
+74Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.learnjava.io.netty.demo01;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.ChannelInboundHandlerAdapter;
5+
6+
/**
7+
* @author lhy
8+
* @date 2021/5/22
9+
*/
10+
public class ServerHandler extends ChannelInboundHandlerAdapter {
11+
public ServerHandler() {
12+
super();
13+
}
14+
15+
@Override
16+
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
17+
super.channelRegistered(ctx);
18+
}
19+
20+
@Override
21+
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
22+
super.channelUnregistered(ctx);
23+
}
24+
25+
@Override
26+
public void channelActive(ChannelHandlerContext ctx) throws Exception {
27+
super.channelActive(ctx);
28+
}
29+
30+
@Override
31+
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
32+
super.channelInactive(ctx);
33+
}
34+
35+
@Override
36+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
37+
super.channelRead(ctx, msg);
38+
}
39+
40+
@Override
41+
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
42+
super.channelReadComplete(ctx);
43+
}
44+
45+
@Override
46+
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
47+
super.userEventTriggered(ctx, evt);
48+
}
49+
50+
@Override
51+
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
52+
super.channelWritabilityChanged(ctx);
53+
}
54+
55+
@Override
56+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
57+
super.exceptionCaught(ctx, cause);
58+
}
59+
60+
@Override
61+
public boolean isSharable() {
62+
return super.isSharable();
63+
}
64+
65+
@Override
66+
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
67+
super.handlerAdded(ctx);
68+
}
69+
70+
@Override
71+
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
72+
super.handlerRemoved(ctx);
73+
}
74+
}
Collapse file
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.learnjava.io.nio.demo01;
2+
3+
import java.net.InetSocketAddress;
4+
import java.nio.ByteBuffer;
5+
import java.nio.channels.SocketChannel;
6+
import java.util.Date;
7+
import java.util.Scanner;
8+
9+
/**
10+
* @author lhy
11+
* @date 2021/5/22
12+
*/
13+
public class NioClinet {
14+
15+
public static void main(String[] args) throws Exception {
16+
17+
// 获取通道
18+
SocketChannel channel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9999));
19+
// 切换至非阻塞模式
20+
channel.configureBlocking(false);
21+
// 分配缓冲区大小
22+
ByteBuffer buffer = ByteBuffer.allocate(1024);
23+
24+
Scanner scan = new Scanner(System.in);
25+
26+
while (scan.hasNext()) {
27+
String next = scan.next();
28+
// 向缓冲区里写入数据
29+
buffer.put( (new Date().toString() + "\n" + next).getBytes());
30+
buffer.flip();
31+
32+
// 向通道里写入带有数据的缓冲区对象, 表示向服务器发送数据
33+
channel.write( buffer);
34+
buffer.clear();
35+
}
36+
37+
// 关闭通道
38+
channel.close();
39+
}
40+
}
Collapse file
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.learnjava.io.nio.demo01;
2+
3+
import java.nio.ByteBuffer;
4+
import java.nio.channels.SelectionKey;
5+
import java.nio.channels.Selector;
6+
import java.nio.channels.ServerSocketChannel;
7+
import java.nio.channels.SocketChannel;
8+
import java.util.Iterator;
9+
10+
/**
11+
* @author lhy
12+
* @date 2021/5/22
13+
*/
14+
public class NioServer {
15+
public static void main(String[] args) throws Exception {
16+
// 获取服务端通道
17+
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
18+
// 切换为非阻塞模式
19+
serverSocketChannel.configureBlocking(false);
20+
// 绑定链接
21+
Selector selector = Selector.open();
22+
// 将通道注册在selector上,并绑定为读事件
23+
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
24+
// 选择器轮训,阻塞
25+
while (selector.select() > 0) {
26+
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
27+
28+
// 判断是否有事件进来
29+
while (it.hasNext()) {
30+
// 获取就绪的事件
31+
SelectionKey selectionKey = it.next();
32+
33+
// 读事件
34+
if (selectionKey.isAcceptable()) {
35+
// 就绪的客户端连接事件
36+
SocketChannel acceptChannel = serverSocketChannel.accept();
37+
acceptChannel.configureBlocking(false);
38+
acceptChannel.register(selector, SelectionKey.OP_READ);
39+
} else if (selectionKey.isReadable()) {
40+
// 读就绪事件
41+
SocketChannel readAcceptChannel = serverSocketChannel.accept();
42+
ByteBuffer allocateBuffer = ByteBuffer.allocate(1024);
43+
44+
int len = 0;
45+
while ((len = readAcceptChannel.read(allocateBuffer)) > 0) {
46+
allocateBuffer.flip();
47+
System.out.println(new String(allocateBuffer.array(), 0, len));
48+
allocateBuffer.clear();
49+
}
50+
}
51+
}
52+
53+
// 取消选择键selectionKey
54+
it.remove();
55+
}
56+
}
57+
}
Collapse file

‎JdkLearn/src/main/java/com/learnjava/lambda/LambdaMapDemo.java‎

Copy file name to clipboardExpand all lines: JdkLearn/src/main/java/com/learnjava/lambda/LambdaMapDemo.java
+10-7Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public class LambdaMapDemo {
1616
public static void main(String[] args) {
1717
// test01();
1818
// test02();
19-
// test03();
20-
test04();
19+
test03();
20+
// test04();
2121
}
2222

2323
/**
@@ -58,11 +58,14 @@ public static void test02() {
5858
*/
5959
public static void test03() {
6060
Map<String, Integer> codes = new HashMap<>();
61-
codes.put("2021-04-01", 1);
62-
codes.put("2021-04-15", 49);
63-
codes.put("2021-04-10", 33);
64-
codes.put("2021-04-05", 86);
65-
codes.put("2021-04-20", 92);
61+
codes.put("2021-03", 1);
62+
codes.put("2021-02", 49);
63+
codes.put("2021-05", 33);
64+
// codes.put("2021-04-01", 1);
65+
// codes.put("2021-04-15", 49);
66+
// codes.put("2021-04-10", 33);
67+
// codes.put("2021-04-05", 86);
68+
// codes.put("2021-04-20", 92);
6669

6770
// 先将Map转化为List,通过collect处理后再转为Map
6871
Map<String, Integer> sortedMap = codes.entrySet()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.