SpringNettyApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package me;import io.netty.channel.ChannelFuture;import me.server.RxServer;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class SpringNettyApplication implements CommandLineRunner { @Autowired private RxServer rxServer; public static void main (String[] args) { SpringApplication.run(SpringNettyApplication.class, args); } @Override public void run (String... args) throws Exception { ChannelFuture future = rxServer.run(); Runtime.getRuntime().addShutdownHook(new Thread(){ @Override public void run () { rxServer.destroy(); } }); future.channel().closeFuture().syncUninterruptibly(); } }
server/RxServer.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 package me.server;import io.netty.bootstrap.Bootstrap;import io.netty.channel.ChannelFactory;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.oio.OioEventLoopGroup;import io.netty.channel.rxtx.RxtxChannel;import io.netty.channel.rxtx.RxtxChannelConfig;import io.netty.channel.rxtx.RxtxDeviceAddress;import lombok.extern.slf4j.Slf4j;import lombok.var ;import me.handler.RxtxClientHandler;import org.springframework.stereotype.Component;@Slf4j @Component public class RxServer { static OioEventLoopGroup group = new OioEventLoopGroup(); private static RxtxChannel channel; public ChannelFuture run () { ChannelFuture future = null ; var bootstrap = new Bootstrap(); try { bootstrap.group(group).channelFactory(new ChannelFactory<RxtxChannel>() { public RxtxChannel newChannel () { return channel; } }) .handler(new ChannelInitializer<RxtxChannel>() { @Override protected void initChannel (RxtxChannel ch) throws Exception { ch.pipeline().addLast( new RxtxClientHandler() ); } }); channel = new RxtxChannel(); channel.config().setBaudrate(9600 ) .setDatabits(RxtxChannelConfig.Databits.DATABITS_8) .setParitybit(RxtxChannelConfig.Paritybit.NONE) .setStopbits(RxtxChannelConfig.Stopbits.STOPBITS_1); future = bootstrap.connect(new RxtxDeviceAddress("COM8" )).sync(); future.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { group.shutdownGracefully(); } return future; } public void destroy () { log.info("Shutdown Netty rx..." ); if (channel != null ) { channel.close();} group.shutdownGracefully(); log.info("Shutdown Netty rx Success!" ); } }
handler/RxtxClientHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 package me.handler;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import lombok.extern.slf4j.Slf4j;@Slf4j public class RxtxClientHandler extends SimpleChannelInboundHandler <ByteBuf > { @Override protected void channelRead0 (ChannelHandlerContext ctx, ByteBuf o) throws Exception { byte [] req = new byte [o.readableBytes()]; o.readBytes(req); log.info(req.toString()); } }
GitHub地址:https://github.com/zzzzzzzs/SpringbootIntegration