本文主要是介绍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;@Beanpublic LoggingFilter loggingFilter() {return new LoggingFilter();}@Beanpublic IoHandler ioHandler() {return new TimeServerHandler();}@Beanpublic InetSocketAddress inetSocketAddress() {return new InetSocketAddress(PORT);}@Beanpublic 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;@Overridepublic void exceptionCaught(IoSession session, Throwable cause) throws Exception {cause.printStackTrace();}@Overridepublic 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);**/}@Overridepublic 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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!