|
| 1 | +package com.bruis.learnnetty.netty.connections.longconnections; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSONObject; |
| 4 | +import com.bruis.learnnetty.netty.connections.longconnections.ClientHandler; |
| 5 | +import io.netty.bootstrap.Bootstrap; |
| 6 | +import io.netty.buffer.PooledByteBufAllocator; |
| 7 | +import io.netty.channel.ChannelFuture; |
| 8 | +import io.netty.channel.ChannelInitializer; |
| 9 | +import io.netty.channel.ChannelOption; |
| 10 | +import io.netty.channel.EventLoopGroup; |
| 11 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 12 | +import io.netty.channel.socket.nio.NioSocketChannel; |
| 13 | +import io.netty.handler.codec.LengthFieldBasedFrameDecoder; |
| 14 | +import io.netty.handler.codec.LengthFieldPrepender; |
| 15 | +import io.netty.handler.codec.string.StringDecoder; |
| 16 | +import io.netty.handler.codec.string.StringEncoder; |
| 17 | +import org.springframework.util.StringUtils; |
| 18 | + |
| 19 | +import java.nio.charset.Charset; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author lhy |
| 23 | + * @date 2022/2/16 |
| 24 | + */ |
| 25 | +public class NettyClient { |
| 26 | + public static EventLoopGroup group = null; |
| 27 | + public static Bootstrap bootstrap = null; |
| 28 | + public static ChannelFuture future = null; |
| 29 | + static { |
| 30 | + bootstrap = new Bootstrap(); |
| 31 | + group = new NioEventLoopGroup(); |
| 32 | + bootstrap.channel(NioSocketChannel.class); |
| 33 | + bootstrap.group(group); |
| 34 | + bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
| 35 | + final ClientHandler clientHandler = new ClientHandler(); |
| 36 | + bootstrap.handler(new ChannelInitializer<NioSocketChannel>() { |
| 37 | + @Override |
| 38 | + protected void initChannel(NioSocketChannel ch) throws Exception { |
| 39 | + ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, |
| 40 | + 0, 4, 0, 4)); |
| 41 | + ch.pipeline().addLast(new StringDecoder()); |
| 42 | + ch.pipeline().addLast(clientHandler); |
| 43 | + ch.pipeline().addLast(new LengthFieldPrepender(4, false)); |
| 44 | + ch.pipeline().addLast(new StringEncoder(Charset.forName("utf-8"))); |
| 45 | + } |
| 46 | + }); |
| 47 | + try { |
| 48 | + future = bootstrap.connect("127.0.0.1", 8080).sync(); |
| 49 | + } catch (InterruptedException e) { |
| 50 | + e.printStackTrace(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * 说明:对于这个长连接的例子中,使用了静态化,即单链接、长连接,如果是多链接的话不可使用静态化,需使用线程池。 |
| 56 | + * @param msg |
| 57 | + * @return |
| 58 | + */ |
| 59 | + public Object sendRequest(Object msg) { |
| 60 | + try { |
| 61 | + RequestFuture request = new RequestFuture(); |
| 62 | + request.setRequest(msg); |
| 63 | + String requestStr = JSONObject.toJSONString(request); |
| 64 | + future.channel().writeAndFlush(requestStr); |
| 65 | + myselfPrint("我阻塞了", null); |
| 66 | + return request.get(); |
| 67 | + } catch (Exception e) { |
| 68 | + e.printStackTrace(); |
| 69 | + throw e; |
| 70 | + } |
| 71 | + } |
| 72 | + public static void main(String[] args) { |
| 73 | + NettyClient nettyClient = new NettyClient(); |
| 74 | + for (int i = 0; i < 10; i++) { |
| 75 | + Object result = nettyClient.sendRequest("hello"); |
| 76 | + myselfPrint("拿到结果了", result); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public static void myselfPrint(String description, Object value) { |
| 81 | + StringBuilder builder = new StringBuilder(); |
| 82 | + builder.append(Thread.currentThread().getName()); |
| 83 | + if (!StringUtils.isEmpty(description)) { |
| 84 | + builder.append("-").append(description); |
| 85 | + } |
| 86 | + if (!StringUtils.isEmpty(value)) { |
| 87 | + builder.append("-").append(value); |
| 88 | + } |
| 89 | + System.out.println(builder.toString()); |
| 90 | + } |
| 91 | +} |
0 commit comments