spring boot 集成 netty出现java.lang.NullPointerException: childHandler错误
java.lang.NullPointerException: childHandler
at io.netty.bootstrap.ServerBootstrap.childHandler(ServerBootstrap.java:134)
at com.example.demo.server.NettyServer.startServer(NettyServer.java:44)
at com.example.demo.server.NettyServer.run(NettyServer.java:72)
at java.lang.Thread.run(Thread.java:748)
下面是demo全部源码
NettyServer 源码
package com.example.demo.server;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
@Component(value = "nettyServer")
public class NettyServer implements Runnable {
@Resource
private NettyServerInitializer nettyServerInitializer;
@Resource
private NettyServerHandler nettyServerHandler;
private int port = 7000;
private EventLoopGroup boss = new NioEventLoopGroup();
private EventLoopGroup worker = new NioEventLoopGroup();
private ChannelFuture cf = null;
/**
* 启动server
*/
public void startServer() {
try {
System.out.println("go!");
ServerBootstrap b = new ServerBootstrap();
b.group(boss, worker);
b.channel(NioServerSocketChannel.class);
b.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(64, 2048, 65536));
b.childHandler(nettyServerInitializer);
cf = b.bind(port).sync();
cf.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
@PreDestroy
public void stop() {
try {
// 监听服务器关闭监听
if (cf != null)
cf.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
@Override
public void run() {
try {
this.startServer();
} catch (Exception e) {
e.printStackTrace();
}
}
}
NettyClient源码
package com.example.demo.server;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class NettyServerInitializer extends ChannelInitializer<SocketChannel> {
@Resource
private NettyServerHandler nettyServerHandler;
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new ByteArrayDecoder());
pipeline.addLast(new ByteArrayEncoder());
pipeline.addLast(nettyServerHandler);
}
}
NettyHandler源码
package com.example.demo.server;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.springframework.stereotype.Component;
@Component
@ChannelHandler.Sharable
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("server channel active... ");
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String body = (String)msg;
System.out.println("Server 接收到 :" + body );
String response = "返回给客户端的响应:" + body ;
ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes()));
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("server: 读完了");
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
启动app程序源码
package com.example.demo;
import com.example.demo.client.NettyClient;
import com.example.demo.server.NettyServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void runNettyServer(){
NettyServer nettyServer = new NettyServer();
Thread thread = new Thread(nettyServer);
thread.start();
System.out.println("Netty Server Start Success!");
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
try {
runNettyServer();
}catch (Exception e){
e.printStackTrace();
}
}
}
相关推荐
大模型+数字仓储解决方案.pptx
大模型+数字仓储解决方案.pptx
中小企业在引进外部技术进行产业化时,标准的技术转移操作流程是什么?.docx
科易网基于行业知识库+行业大数据+科技资源要素+AI模型,以AI,赋能技术转移、成果转化、技术经理人培训、知识产权管理、区域产业分析与布局、地方科技招商、创新创业大赛等场景,推出科技创新领域的AI工具、场景化服务和数智化解决方案,推动科技创新与产业创新智能化发展。
MES车间级工业4.0智能制造管理系统.pptx
MES车间级工业4.0智能制造管理系统.pptx
5G 工业互联网规划方案.pptx
5G 工业互联网规划方案.pptx
考虑电动汽车移动储能特性的多区域电网功率波动平抑优化调控研究(Python代码实现)
内容概要:本文研究了考虑电动汽车移动储能特性的多区域电网功率波动平抑优化调控方法,并提供了相应的Python代码实现。通过将电动汽车视为具有时空灵活性的可移动储能单元,构建了综合考虑电网负荷、可再生能源出力不确定性及用户出行需求的优化模型。该模型利用电动汽车集群的协同调度能力,在满足出行约束的前提下实现削峰填谷,有效平抑多区域电网功率波动,降低系统运行成本并提升新能源消纳能力。采用数学优化方法求解该模型,实现了对多区域电网功率波动的协同优化调控,增强了高比例新能源接入背景下电网运行的稳定性与经济性。; 适合人群:具备一定电力系统基础知识和Python编程能力的研究生、科研人员及从事智能电网、电动汽车与能源互联网相关领域的工程技术人员。; 使用场景及目标:①研究电动汽车作为分布式储能参与电网调峰调频的可行性与优化调度策略;②实现多区域电网功率波动的协同优化控制;③为高比例可再生能源接入背景下的电网稳定运行提供技术支持与决策参考。; 阅读建议:此资源以Python代码实现为核心,结合理论模型与仿真验证,建议读者在学习过程中重点关注优化模型的构建逻辑、约束条件设置及目标函数设计,并结合实际算例进行代码调试与结果分析,以深入理解电动汽车参与电网调控的机制与效果。


