【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
在上一帖的代码基础上集成spring boot,具体修改的地方:
1)myhandler.java文件中
@Component public class MyHandler extends IoHandlerAdapter
2) MinaServer.java文件中
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class MinaServer implements ApplicationRunner { static int PORT=7080; //Mina三个主要的部分之一是IoService,其中服务器端实现IoService的是IoAcceptor,客户端实现IoService的是IoConnector //IoAcceptor和IoConnector都分别有实现TCP/IP,UDP/IP及虚拟机管道通讯的子接口 static IoAcceptor accept = null; // AbstractIoService 是IoService接口的适配层,是一个abstact class // AbstractIoAcceptor 也是一个适配器,是一个 abstactr class ,只不过是继承自AbstarctIoService // IoHandler是mina的三个主要部分之一,这主要定义了session相关的接口(create,open,status,idle),异常(exceptionCaught)及数据发送和接收接口(messageReceive,messageSend) // IoHandlerAdapter是一个适配器,也是一个abstract class ,实现了IoHandler的接口 public static void main(String[] args) { SpringApplication.run(MinaServer.class, args); } @Override public void run(ApplicationArguments args) throws Exception { try { accept = new NioSocketAcceptor(); //设置过滤器 accept.getFilterChain().addLast("codec",new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"), LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue()))); //设置缓冲区 accept.getSessionConfig().setReadBufferSize(1024); accept.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,10); //10s accept.setHandler(new MyHandler());//业务 accept.bind(new InetSocketAddress(PORT)); System.out.println("server->"+PORT); //Mina主要在个部分分别是: //1:IoService:执行实际的IO操作,管理IO会话 //2:IoFilter Chain 将数据进行过滤或者转化为期望的数据结构,反之变然 //3:IoHandler 实际的业务操作 } catch(Exception e) { e.printStackTrace(); } } }
3)application.properties文件中增加
spring.main.web-application-type=NONE
网上帖子 https://www.iteye.com/blog/log-cd-2442372 中还有其它设置,我没有用。
再用手机APP通讯实验,也是成功的。
实验现象:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/joe/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/joe/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.1/log4j-slf4j-impl-2.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)
2020-01-08 15:45:29.577 INFO 11008 --- [ main] com.example.javafxminademo.MinaServer : Starting MinaServer on joe-PC with PID 11008 (D:\tmp\SpringBoot-master\javafxminademo\target\classes started by joe in D:\tmp\SpringBoot-master\javafxminademo)
2020-01-08 15:45:29.582 INFO 11008 --- [ main] com.example.javafxminademo.MinaServer : No active profile set, falling back to default profiles: default
2020-01-08 15:45:30.496 INFO 11008 --- [ main] com.example.javafxminademo.MinaServer : Started MinaServer in 1.402 seconds (JVM running for 1.926)
server->7080
sessionCreated:创建Session
sessionOpened:打开Session用于读写数据
sessionIdle:处于多长时间是空闲状态
来源:oschina
链接:https://my.oschina.net/u/2963604/blog/3154886