springboot整合mina

Posted 健康平安的活着

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot整合mina相关的知识,希望对你有一定的参考价值。

一 配置操作

1.1  config

package com.dky.mina.config;

import com.dky.mina.handler.TimeServerHandler;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NiosocketAcceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

/**
 * @ClassName: MinaConfig
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/08/18 20:34:21 
 * @Version: V1.0
 **/
@Configuration
public class MinaConfig {
    private static final int PORT = 9123;

    @Bean
    public LoggingFilter loggingFilter() {
        return new LoggingFilter();
    }
    @Bean
    public IoHandler ioHandler() {
        return new TimeServerHandler();
    }
    @Bean
    public InetSocketAddress inetSocketAddress() {
        return new InetSocketAddress(PORT);
    }

    @Bean
    public IoAcceptor ioAcceptor() throws Exception {
        IoAcceptor acceptor=new NioSocketAcceptor();
        acceptor.getFilterChain().addLast( "logger", loggingFilter() );
        acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory(Charset.forName("UTF-8"))));
        acceptor.setHandler(ioHandler());
        acceptor.getSessionConfig().setReadBufferSize( 2048 );
        acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
        acceptor.bind(inetSocketAddress());
        System.out.println("Mina服务已启动,端口:" + PORT);
        return acceptor;
    }

}

1.2 handler

package com.dky.mina.handler;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * @ClassName: TimeServerHandler
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/08/18 20:59:39 
 * @Version: V1.0
 **/
public class TimeServerHandler extends IoHandlerAdapter {
    //@Autowired
    //private MenuService menuService;

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        cause.printStackTrace();
    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        String str = message.toString();
        System.out.println("Message -->"  + str);
        if (str.trim().equalsIgnoreCase("quit")) {
            session.close();
            return;
        }
        Date date = new Date();
        // 返回数据
        session.write(date.toString()+"||"+str);
        // 测试数据
        /**
        MenuViewModel menuViewModel = new MenuViewModel();
        menuViewModel.setName("harlan");
        menuViewModel.setUrl("/url/");
        menuService.addMenu(menuViewModel);
         **/
    }

    @Override
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        System.out.println("IDLE " + session.getIdleCount(status));
    }
}

1.3 启动类

package com.dky.mina;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {

        SpringApplication.run(App.class, args);
        System.out.println("启动完成!!!");
    }
}

1.4 测试

在cmd窗口中输入: telnet 127.0.0.1 9123 

以上是关于springboot整合mina的主要内容,如果未能解决你的问题,请参考以下文章

mina sping 整合

springboot Thymeleaf 整合

mina学习资料整合

spring boot 整合 redis

MybatisPlus的代码生成器 使用详解(整合springboot)

MybatisPlus的代码生成器 使用详解(整合springboot)