SpringBoot使用Mina框架进行服务端与客户端数据通信

x33g5p2x  于2022-03-01 转载在 Spring  
字(2.8k)|赞(0)|评价(0)|浏览(539)

pom.xml引入

  1. <dependency>
  2. <groupId>org.apache.mina</groupId>
  3. <artifactId>mina-core</artifactId>
  4. <version>2.1.3</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.mina</groupId>
  8. <artifactId>mina-integration-beans</artifactId>
  9. <version>2.1.3</version>
  10. </dependency>

服务端创建采集服务TCP线程

  1. @Configuration
  2. public class TCPServ {
  3. //数据采集开关。配置项中获取
  4. @Value("${monitordata.company.electricity.enableSwitch}")
  5. private boolean enableSwitch = false;
  6. @Value("${monitordata.company.electricity.tcpPort}")
  7. private int port;
  8. @Autowired
  9. ServerHandler serverHandler; //Mina事件类ServerHandler
  10. @Bean
  11. public IoAcceptor companyElectricityTCPServ() throws Exception {
  12. if (!enableSwitch) {
  13. return null;
  14. }
  15. IoAcceptor acceptor = new NioSocketAcceptor();
  16. acceptor.getSessionConfig().setReadBufferSize(1024 * 1024);//设置缓冲区
  17. acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60*5); //配置会话信息
  18. //其中需要注意的是,在服务端和客户端的代码里面,如果要传递string信息,codec编码过滤器中,要这么写:new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))。否则报错。
  19. //acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
  20. acceptor.setHandler(serverHandler); //自定义处理业务的代码:自定义的类
  21. try {
  22. acceptor.bind(new InetSocketAddress(port));//绑定端口号
  23. } catch (IOException e) {
  24. // TODO Auto-generated catch block
  25. e.printStackTrace();
  26. }
  27. System.out.println("Socket服务器在端口:" + port + "已经启动");
  28. return acceptor;
  29. }
  30. }

Mina事件类ServerHandler

  1. @Component
  2. public class ServerHandler extends IoHandlerAdapter {
  3. @Autowired
  4. MeterCollectsDataService meterCollectsDataService;
  5. @Autowired
  6. ElectricCollectorService electricCollectorService;
  7. @Override
  8. public void sessionCreated(IoSession session) throws Exception {
  9. session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 60*5);
  10. String key = session.getRemoteAddress().toString();
  11. System.out.println("设备接入:" + key);
  12. }
  13. @Override
  14. public void messageReceived(IoSession session, Object message) throws Exception {
  15. String key = session.getRemoteAddress().toString();
  16. IoBuffer ioBuffer = (IoBuffer) message;
  17. byte[] data = new byte[ioBuffer.limit()];
  18. ioBuffer.get(data);
  19. String msg = new String(data);
  20. System.out.println("收到数据:" + msg);
  21. //发送数据
  22. String sendData = "";
  23. session.write(IoBuffer.wrap(sendData.getBytes("utf-8")));
  24. }
  25. @Override
  26. public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
  27. System.out.println("exceptionCaught");
  28. session.closeNow();
  29. }
  30. @Override
  31. public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
  32. if (status == IdleStatus.BOTH_IDLE) {
  33. System.out.println("BOTH空闲");
  34. session.closeNow();
  35. }
  36. }
  37. @Override
  38. public void sessionClosed(IoSession session) throws Exception {
  39. System.out.println("sessionClosed");
  40. System.out.println("设备断开:" + session.getRemoteAddress().toString());
  41. }
  42. }

调试

使用上传文件中的网络调试工具进行测试

相关文章