本文整理匯總了Java中org.apache.mina.transport.socket.nio.NioSocketAcceptor類的典型用法代碼示例。如果您正苦於以下問題:Java NioSocketAcceptor類的具體用法?Java NioSocketAcceptor怎麽用?Java NioSocketAcceptor使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
NioSocketAcceptor類屬於org.apache.mina.transport.socket.nio包,在下文中一共展示了NioSocketAcceptor類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
public static void main(String[] args) throws IOException {
IoAcceptor acceptor = new NioSocketAcceptor();
// 這個過濾器用來記錄所有的信息,比如創建session(會話),接收消息,發送消息,關閉會話等
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
// 用來轉換二進製或協議的專用數據到消息對象中
acceptor.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
// 實時處理客戶端的連接和請求
acceptor.setHandler(new TimeServerHandler());
acceptor.getSessionConfig().setReadBufferSize(2048);
// 方法將定時調用一次會話,保持空閑狀態。來設定時間間隔。
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
acceptor.bind(new InetSocketAddress(PORT));
}
示例2: start
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
/**
* start Mina serversocket
* @throws Exception
*/
public void start() throws Exception {
IoBuffer.setUseDirectBuffer(false);
IoBuffer.setAllocator(new SimpleBufferAllocator());
acceptor = new NioSocketAcceptor(pool);
acceptor.setReuseAddress(true);
acceptor.getSessionConfig().setAll(getSessionConfig());
int serverPort = GateServerConfig.serverPort;
logger.info("socket server start at port:{},正在監聽客戶端的連接...", serverPort);
DefaultIoFilterChainBuilder filterChain = acceptor.getFilterChain();
filterChain.addLast("codec", new ProtocolCodecFilter(MessageCodecFactory.getInstance()));
acceptor.setHandler( new ServerSocketIoHandler() );//指定業務邏輯處理器
acceptor.setDefaultLocalAddress(new InetSocketAddress(serverPort) );//設置端口號
acceptor.bind();//啟動監聽
}
示例3: SlaveHost
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
/**
* Create a new slave host and bind to a single TCP port
*
* @param options
* optional protocol options
* @param port
* the TCP port to bind to
*/
public SlaveHost ( final ProtocolOptions options, final int port ) throws IOException
{
this.options = makeOptions ( options );
this.connector = null;
this.processor = new SimpleIoProcessorPool<> ( NioProcessor.class );
final SocketAcceptor nioAcceptor = new NioSocketAcceptor ( this.processor );
this.acceptor = nioAcceptor;
nioAcceptor.setReuseAddress ( true );
nioAcceptor.setBacklog ( 5 );
this.disposeAcceptor = true;
setupAcceptor ( null );
this.acceptor.bind ( new InetSocketAddress ( port ) );
}
示例4: SocketServer
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
public SocketServer(int port, int threadPoolSize) {
NioSocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.setHandler(new ServerHandler());
// acceptor.getFilterChain().addLast("mychian",new ProtocolCodecFilter(new TextLineCodecFactory()));
// acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
acceptor.getFilterChain().addLast("exector", new ExecutorFilter(new OrderedThreadPoolExecutor(threadPoolSize)));
SocketSessionConfig ssc = acceptor.getSessionConfig();// 建立連接的配置文件
ssc.setReadBufferSize(4096);// 設置接收最大字節默認4096
ssc.setReceiveBufferSize(10240);// 設置輸入緩衝區的大小
ssc.setSendBufferSize(10240);// 設置輸出緩衝區的大小
ssc.setReuseAddress(true);// 設置每一個非主監聽連接的端口可以重用
try {
// 綁定端口
acceptor.bind(new InetSocketAddress(port));
System.out.println("服務器啟動正常,監聽端口 " + port);
} catch (IOException e) {
e.printStackTrace();
}
}
示例5: Channel
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
public Channel(final int world, final int channel) {
this.world = world;
this.channel = channel;
this.mapFactory = new MapleMapFactory(MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/Map.wz")), MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/String.wz")), world, channel);
try {
eventSM = new EventScriptManager(this, ServerConstants.EVENTS.split(" "));
port = 7575 + this.channel - 1;
port += (world * 100);
ip = ServerConstants.HOST + ":" + port;
IoBuffer.setUseDirectBuffer(false);
IoBuffer.setAllocator(new SimpleBufferAllocator());
acceptor = new NioSocketAcceptor();
TimerManager.getInstance().register(new respawnMaps(), 10000);
acceptor.setHandler(new MapleServerHandler(world, channel));
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30);
acceptor.getFilterChain().addLast("codec", (IoFilter) new ProtocolCodecFilter(new MapleCodecFactory()));
acceptor.bind(new InetSocketAddress(port));
((SocketSessionConfig) acceptor.getSessionConfig()).setTcpNoDelay(true);
eventSM.init();
System.out.println(" Channel " + getId() + ": Listening on port " + port);
} catch (Exception e) {
e.printStackTrace();
}
}
示例6: main
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
public static void main(String[] args) throws IOException {
// 創建服務器端的監聽器對象
IoAcceptor acceptor = new NioSocketAcceptor();
// 增加日誌過濾器:用於日誌存儲
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
// 增加消息編碼過濾器,采用UTF-8編碼
acceptor.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
// 設置具體的事物邏輯處理器
acceptor.setHandler(new TimeServerHandler());
// 設置IoSession的一些屬性
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
// 設置服務器監聽的端口
acceptor.bind(new InetSocketAddress(PORT));
}
示例7: main
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
public static void main(String[] args) throws IOException {
IoAcceptor acceptor = new NioSocketAcceptor();
// acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, IDELTIMEOUT);
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
acceptor.getFilterChain().addLast("myfliter", new MyFilter());
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new CommandCodecFactory("UTF-8")));
KeepAliveMessageFactoryImpl kamfi = new KeepAliveMessageFactoryImpl();
KeepAliveFilter kaf = new KeepAliveFilter(kamfi, IdleStatus.BOTH_IDLE);
/** 是否回發 */
kaf.setForwardEvent(true);
acceptor.getFilterChain().addLast("heart", kaf);
acceptor.setHandler(new CalculatorHandler());
acceptor.bind(new InetSocketAddress(PORT));
log.debug("socket通信服務端已啟動,端口是" + PORT);
}
示例8: serverStart
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
@Override
protected void serverStart() throws RemotingException {
acceptor = new NioSocketAcceptor(); //TCP Acceptor
// acceptor.getFilterChain().addFirst("logging", new MinaLoggingFilter());
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecFactory(getCodec())));
acceptor.getFilterChain().addLast("mdc", new MdcInjectionFilter());
acceptor.setHandler(new MinaHandler(this));
IoSessionConfig cfg = acceptor.getSessionConfig();
cfg.setReaderIdleTime(remotingServerConfig.getReaderIdleTimeSeconds());
cfg.setWriterIdleTime(remotingServerConfig.getWriterIdleTimeSeconds());
cfg.setBothIdleTime(remotingServerConfig.getServerChannelMaxIdleTimeSeconds());
bindAddress = new InetSocketAddress(remotingServerConfig.getListenPort());
try {
acceptor.bind(bindAddress);
} catch (IOException e) {
throw new RemotingException("Start Mina server error", e);
}
}
示例9: init
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
private void init(Config config) {
address = new InetSocketAddress(config.sourcePort);
acceptor = new NioSocketAcceptor();
if (acceptor.getFilterChain().get(LOGGER) == null) {
acceptor.getFilterChain().addLast(LOGGER, new LoggingFilter());
}
if (acceptor.getFilterChain().get(CODEC) == null) {
ProtocolCodecFactory codecFactory = config.codec == null ?
MinaUtil.getTextLineCodecFactory() : (ProtocolCodecFactory) config.codec;
acceptor.getFilterChain().addLast(CODEC, new ProtocolCodecFilter(codecFactory));
}
acceptor.setHandler(new MinaServerHandler());
acceptor.getSessionConfig().setReadBufferSize(config.bufferSize);
acceptor.getSessionConfig().setIdleTime(IdleStatus.WRITER_IDLE, 10);
acceptor.setReuseAddress(true); //避免重啟時提示地址被占用
//設置主服務監聽端口的監聽隊列的最大值為50,如果當前已經有50個連接,新的連接將被服務器拒絕
acceptor.setBacklog(50);
KeepAliveFilter keepAliveFilter = MinaUtil.initServerKeepAlive(config, this);
if (keepAliveFilter != null) {
acceptor.getFilterChain().addLast(HEARTBEAT, keepAliveFilter);
}
}
示例10: startServer
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
/**
* �������
* @param args
* @throws IOException
*/
public void startServer() {
server = new NioSocketAcceptor();
//�������ݹ�����
DefaultIoFilterChainBuilder filterChain = server.getFilterChain();
filterChain.addLast("myChin", new ProtocolCodecFilter(
new ObjectSerializationCodecFactory()));
//filterChain.addLast("textCode",new ProtocolCodecFilter(
// new TextLineCodecFactory(Charset.forName("UTF-8"))));
serverIOHandler = new ServerIOHandler(severFrame);
server.setHandler(serverIOHandler);
//�����������˿� --- ����������
try {
server.bind(new InetSocketAddress(port));
} catch (IOException e) {
Tools.show(severFrame, "�˿��Ѿ�ռ�ã��뻻���˿ڣ�");
}
}
示例11: start
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
protected void start() throws Exception{
buildExecutors();
IoProcessor<NioSession> processor = new SimpleIoProcessorPool<NioSession>(NioProcessor.class, ioExecutor, coreSize);
acceptor = new NioSocketAcceptor(acceptorExecutor, processor);
// acceptor.setBacklog(cfg.getBacklog());
buildFilterChain();
acceptor.setHandler(handler);
try {
List<SocketAddress> address = new ArrayList<SocketAddress>();
//可添加多個
address.add(new InetSocketAddress((String)rule.get("ip"), (Integer)rule.get("port")));
acceptor.bind(address);
} catch (IOException e) {
stop();
throw e;
}
}
示例12: startupMinaCore
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
/**
* 創建 Mina2 Core 服務核心並啟動
*
* @return
*/
private boolean startupMinaCore() {
_acceptor = new NioSocketAcceptor();
// 禁用算法
_acceptor.getSessionConfig().setTcpNoDelay(true);
// 添加過濾鏈處理項
_acceptor.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new CodecFactory()));
// 設置處理器
_acceptor.setHandler(new FrontendServerHandler(this));
try {
_acceptor.bind(new InetSocketAddress(getServerConfig()
.getFrontendPort()));
} catch (IOException e) {
logger.error("啟動 Mina Core 監聽失敗:", e);
return false;
}
logger.error("Mina Core Startup Success On Port:"
+ getServerConfig().getFrontendPort());
return true;
}
示例13: main
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
public static void main(String[] args) {
int port = U.getInt("cleaner.server.port");
int idleTime = U.getInt("cleaner.server.time");
NioSocketAcceptor clientAcceptor = new NioSocketAcceptor();
clientAcceptor.setReuseAddress(true);
DefaultIoFilterChainBuilder chain = clientAcceptor.getFilterChain();
SocketSessionConfig config = clientAcceptor.getSessionConfig();
config.setIdleTime(IdleStatus.READER_IDLE, idleTime);// 讀空閑 10秒
config.setReuseAddress(true);
config.setReadBufferSize(4096);// 默認2048
config.setKeepAlive(true);
config.setTcpNoDelay(true);// 開啟nagle算法
// 編解碼
chain.addLast("coder", new ProtocolCodecFilter(new TextLineCodecFactory(U.UTF8)));
clientAcceptor.setHandler(new CleanerHandler());
try {
clientAcceptor.bind(new InetSocketAddress(port));
log.info("心跳模塊啟動..");
} catch (IOException e) {
log.error("心跳模塊啟動失敗", e);
}
}
示例14: initialize
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
public void initialize() {
idleTime = U.getInt("chat.server.idleTime");
System.out.println(idleTime);
clientAcceptor = new NioSocketAcceptor();
clientAcceptor.setReuseAddress(true);
DefaultIoFilterChainBuilder chain = clientAcceptor.getFilterChain();
SocketSessionConfig config = clientAcceptor.getSessionConfig();
config.setReuseAddress(true);
config.setIdleTime(IdleStatus.READER_IDLE, idleTime);// 讀空閑 20秒
config.setReadBufferSize(4096);// 默認2048
config.setKeepAlive(true);
config.setTcpNoDelay(true);// 禁用/開啟nagle算法
// 編解碼
chain.addLast("coder", new ProtocolCodecFilter(new MessageCodecFactory()));
// 日誌
chain.addLast("logger", new LoggingFilter());
// 業務邏輯處理線程池
chain.addLast("threadPool", new ExecutorFilter());
}
示例15: run_startup_configurations
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //導入依賴的package包/類
public static void run_startup_configurations() {
autoPaoDian = Integer.parseInt(ServerProperties.getProperty("autoPaoDian", "1"));
port = Short.parseShort(ServerProperties.getProperty("cashshop.port", String.valueOf(DEFAULT_PORT)));
ip = ServerProperties.getProperty("world.host", ServerConstants.IP) + ":" + port;
IoBuffer.setUseDirectBuffer(false);
IoBuffer.setAllocator(new SimpleBufferAllocator());
acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MapleCodecFactory()));
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30);
players = new PlayerStorage(MapleServerHandler.CASH_SHOP_SERVER);
try {
acceptor.setHandler(new MapleServerHandler(MapleServerHandler.CASH_SHOP_SERVER));
acceptor.bind(new InetSocketAddress(port));
((SocketSessionConfig) acceptor.getSessionConfig()).setTcpNoDelay(true);
FileoutputUtil.log("完成!");
FileoutputUtil.log("商城伺服器正在監聽" + port + "端口\r\n");
} catch (IOException e) {
FileoutputUtil.log("失敗!");
System.err.println("無法綁定" + port + "端口");
throw new RuntimeException("綁定端口失敗.", e);
}
}