package com.example.mina.config; import com.example.mina.box1.AeroflexVirtualBoxHandler; import com.example.mina.property.AeroflexVirtualBoxProperties; import lombok.extern.slf4j.Slf4j; import org.apache.mina.transport.socket.nio.NioSocketAcceptor; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; import java.net.InetSocketAddress; /** * @author 杜云山 * @date 21/03/05 */ @Slf4j @Configuration(proxyBeanMethods = false) public class AeroflexVirtualBoxConfiguration { private final AeroflexVirtualBoxProperties aeroflexVirtualBoxProperties; public AeroflexVirtualBoxConfiguration(AeroflexVirtualBoxProperties aeroflexVirtualBoxProperties) { this.aeroflexVirtualBoxProperties = aeroflexVirtualBoxProperties; } @PostConstruct public void init() { if (!aeroflexVirtualBoxProperties.getEnable()) { log.info("AeroflexVirtual服务端 配置未开启"); return; } if (aeroflexVirtualBoxProperties.getPort() == null) { log.info("AeroflexVirtual服务端 端口未配置"); return; } try { NioSocketAcceptor acceptor = new NioSocketAcceptor(); acceptor.setHandler(new AeroflexVirtualBoxHandler()); acceptor.setReuseAddress(true); acceptor.bind(new InetSocketAddress(aeroflexVirtualBoxProperties.getPort())); log.info("AeroflexVirtual服务端已经启动,监听端口: {}", aeroflexVirtualBoxProperties.getPort()); } catch (Exception e) { log.error("无法启动AeroflexVirtual服务端, {}", e.getMessage(), e); } } }