本文整理汇总了Java中org.apache.mina.transport.socket.nio.NioSocketAcceptor.setReuseAddress方法的典型用法代码示例。如果您正苦于以下问题:Java NioSocketAcceptor.setReuseAddress方法的具体用法?Java NioSocketAcceptor.setReuseAddress怎么用?Java NioSocketAcceptor.setReuseAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.mina.transport.socket.nio.NioSocketAcceptor
的用法示例。
在下文中一共展示了NioSocketAcceptor.setReuseAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();//启动监听
}
示例2: 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);
}
}
示例3: 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);
}
}
示例4: 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());
}
示例5: startListner
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //导入方法依赖的package包/类
public void startListner(IoHandler iohandler,int listenPort) throws Exception{
acceptor = new NioSocketAcceptor();
acceptor.setBacklog(100);
acceptor.setReuseAddress(true);
acceptor.setHandler(iohandler);
DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
IoFilter protocol = new ProtocolCodecFilter(new GameProtocolcodecFactory());
chain.addLast("codec", protocol);
threadpool = new OrderedThreadPoolExecutor(500);
threadpool.setThreadFactory(new ServerThreadFactory("OrderedThreadPool"));
chain.addLast("threadPool", new ExecutorFilter(threadpool));
int recsize = 5120;
int sendsize = 40480;
int timeout = 10;
SocketSessionConfig sc = acceptor.getSessionConfig();
sc.setReuseAddress(true);// 设置每一个非主监听连接的端口可以重用
sc.setReceiveBufferSize(recsize);// 设置输入缓冲区的大小
sc.setSendBufferSize(sendsize);// 设置输出缓冲区的大小
sc.setTcpNoDelay(true);// flush函数的调用 设置为非延迟发送,为true则不组装成大包发送,收到东西马上发出
sc.setSoLinger(0);
sc.setIdleTime(IdleStatus.READER_IDLE, timeout);
acceptor.bind(new InetSocketAddress(listenPort));
}
示例6: main
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("protocol", new ProtocolCodecFilter(new WebSocketCodecFactory()));
// close sessions when the acceptor is stopped
acceptor.setCloseOnDeactivation(true);
acceptor.setHandler(new WebSocketHandler());
SocketSessionConfig sessionConf = acceptor.getSessionConfig();
sessionConf.setReuseAddress(true);
acceptor.setReuseAddress(true);
// loop through the addresses and bind
Set<InetSocketAddress> socketAddresses = new HashSet<InetSocketAddress>();
socketAddresses.add(new InetSocketAddress("0.0.0.0", 8888));
//socketAddresses.add(new InetSocketAddress("localhost", 8888));
log.debug("Binding to {}", socketAddresses.toString());
acceptor.bind(socketAddresses);
System.out.println("WS server started listening");
listening = true;
while (true) {
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
System.out.println("WS server stopped listening");
}
}
}
示例7: createReceiver
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //导入方法依赖的package包/类
private static void createReceiver(IoHandler serverHandler, InetSocketAddress address) {
try {
NioSocketAcceptor acceptor = new NioSocketAcceptor();
acceptor.setReuseAddress(true);
acceptor.setHandler(serverHandler);
acceptor.bind(address);
logger.info("Receiver started on port {}", address.getPort());
receivers.put(address, acceptor);
} catch (IOException ex) {
logger.error(ex.getMessage(), ex);
fail("Failed to start listener on port " + address.getPort());
}
}
示例8: SocketServer
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //导入方法依赖的package包/类
public SocketServer(int port, MessageHandler msgHandler, int threadPoolSize) {
mServerHandler = new ServerHandler(msgHandler);
acceptor = new NioSocketAcceptor();
acceptor.setHandler(mServerHandler);// 设置接收器的处理程序
acceptor.setReuseAddress(true) ;
// acceptor.getFilterChain().addLast("exector", new ExecutorFilter(new OrderedThreadPoolExecutor(10)));//建立有序线程池
acceptor.getFilterChain().addLast("exector", new ExecutorFilter(new OrderedThreadPoolExecutor(Runtime.getRuntime().availableProcessors()+1)));//建立有序线程池
// acceptor.getFilterChain().addLast("exector", new ExecutorFilter(new OrderedThreadPoolExecutor(128)));//建立有序线程池
/* Executor threadPool = Executors.newFixedThreadPool(threadPoolSize);// 建立线程池
acceptor.getFilterChain().addLast("exector",
new ExecutorFilter(threadPool));*/
acceptor.getFilterChain().addLast("codec",//解码\转码
new ProtocolCodecFilter(new MessageCodecFactory(Charset.forName("UTF-8"))));
try {
// 绑定端口
acceptor.bind(new InetSocketAddress(port));
logger.info("服务器启动正常,监听端口 " + port);
} catch (IOException e) {
e.printStackTrace();
}
SocketSessionConfig ssc = acceptor.getSessionConfig();// 建立连接的配置文件
ssc.setReadBufferSize(4096);// 设置接收最大字节默认4096
// ssc.setReceiveBufferSize(1024);// 设置输入缓冲区的大小
// ssc.setSendBufferSize(1024);// 设置输出缓冲区的大小
// ssc.setReuseAddress(true);// 设置每一个非主监听连接的端口可以重用
ssc.setTcpNoDelay(false);
}
示例9: MinaAcceptor
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //导入方法依赖的package包/类
public MinaAcceptor(CodecFactory codecFactory, int maxIoThreads, int maxTaskThreads, TaskMapper taskMapper) {
Objects.requireNonNull(codecFactory);
log.info("Initializing acceptor {max I/O threads: {}, max task threads: {}}",
maxIoThreads,
maxTaskThreads > 0
? Integer.toString(maxTaskThreads)
: "unbounded");
acceptor = new NioSocketAcceptor(maxIoThreads);
DefaultIoFilterChainBuilder filters = acceptor.getFilterChain();
// encoding
filters.addLast("codec", new ProtocolCodecFilter(new MinaCodecFactory(codecFactory)));
// thread model
executor = newExecutor(maxTaskThreads, taskMapper);
filters.addLast("threadPool", new ExecutorFilter(executor));
// logging
filters.addLast("logger", new LoggingFilter());
// session handler
minaAdapter = new MinaAdapter();
acceptor.setHandler(minaAdapter);
// acceptor configuration
// disable disconnection on unbind
acceptor.setCloseOnDeactivation(false);
// port reuse when socket is in TIME_WAIT state
acceptor.setReuseAddress(true);
acceptor.getSessionConfig().setReuseAddress(true);
acceptor.getSessionConfig().setKeepAlive(true);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, DEFAULT_SESSION_IDLE_SECONDS);
// no Nagle's algorithm
acceptor.getSessionConfig().setTcpNoDelay(true);
}
示例10: start
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //导入方法依赖的package包/类
public void start(int port) throws IOException {
acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
acceptor.setHandler(this);
acceptor.getSessionConfig().setReadBufferSize( 2048 );
acceptor.getSessionConfig().setIdleTime( IdleStatus.WRITER_IDLE, 10);
acceptor.getSessionConfig().setIdleTime( IdleStatus.READER_IDLE, 10);
acceptor.setReuseAddress(true);
acceptor.bind(new InetSocketAddress(port));
}
示例11: buildSocketAcceptor
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //导入方法依赖的package包/类
private static NioSocketAcceptor buildSocketAcceptor()
{
// Create SocketAcceptor with correct number of processors
final int processorCount = JiveGlobals.getIntProperty( "xmpp.processor.count", Runtime.getRuntime().availableProcessors() );
final NioSocketAcceptor socketAcceptor = new NioSocketAcceptor( processorCount );
// Set that it will be possible to bind a socket if there is a connection in the timeout state.
socketAcceptor.setReuseAddress( true );
// Set the listen backlog (queue) length. Default is 50.
socketAcceptor.setBacklog( JiveGlobals.getIntProperty( "xmpp.socket.backlog", 50 ) );
// Set default (low level) settings for new socket connections
final SocketSessionConfig socketSessionConfig = socketAcceptor.getSessionConfig();
//socketSessionConfig.setKeepAlive();
final int receiveBuffer = JiveGlobals.getIntProperty( "xmpp.socket.buffer.receive", -1 );
if ( receiveBuffer > 0 )
{
socketSessionConfig.setReceiveBufferSize( receiveBuffer );
}
final int sendBuffer = JiveGlobals.getIntProperty( "xmpp.socket.buffer.send", -1 );
if ( sendBuffer > 0 )
{
socketSessionConfig.setSendBufferSize( sendBuffer );
}
final int linger = JiveGlobals.getIntProperty( "xmpp.socket.linger", -1 );
if ( linger > 0 )
{
socketSessionConfig.setSoLinger( linger );
}
socketSessionConfig.setTcpNoDelay( JiveGlobals.getBooleanProperty( "xmpp.socket.tcp-nodelay", socketSessionConfig.isTcpNoDelay() ) );
return socketAcceptor;
}
示例12: createAcceptor
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //导入方法依赖的package包/类
@Override
protected IoAcceptor createAcceptor() {
NioSocketAcceptor acceptor = new NioSocketAcceptor(executor, ioProcessor);
acceptor.setCloseOnDeactivation(false);
acceptor.setReuseAddress(reuseAddress);
acceptor.setBacklog(backlog);
// MINA itself forces our socket receive buffer to 1024 bytes
// by default, despite what the operating system defaults to.
// This limits us to about 3 MB/s incoming data transfer. By
// forcing back to the operating system default we can get a
// decent transfer rate again.
//
final Socket s = new Socket();
try {
try {
acceptor.getSessionConfig().setReceiveBufferSize(s.getReceiveBufferSize());
} finally {
s.close();
}
} catch (IOException e) {
log.warn("cannot adjust SO_RCVBUF back to system default", e);
}
if (sessionConfig != null) {
acceptor.getSessionConfig().setAll(sessionConfig);
}
return acceptor;
}
示例13: ProxyServer
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //导入方法依赖的package包/类
public ProxyServer(int proxyPort, int delegatorPort, List<Integer> securedPorts, String keystorePath, char[] password) throws Exception {
this.proxyPort = proxyPort;
this.delegatorPort = delegatorPort;
this.securedPorts = securedPorts;
certificateManager = new CertificateManager(keystorePath, password);
acceptor = new NioSocketAcceptor(IO_PROCESSOR_POOL);
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new SOCKSCodecFactory()));
acceptor.setReuseAddress(true);
acceptor.setHandler(this);
delegator = new Delegator(IO_PROCESSOR_POOL, certificateManager);
}
示例14: Delegator
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //导入方法依赖的package包/类
public Delegator(IoProcessor<NioSession> ioProcessor, CertificateManager certificateManager) {
this.ioProcessor = ioProcessor;
this.certificateManager = certificateManager;
acceptor = new NioSocketAcceptor(ioProcessor);
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new DelegatorRemoteCodecFactory()));
acceptor.setReuseAddress(true);
acceptor.setHandler(this);
}
示例15: MyServer
import org.apache.mina.transport.socket.nio.NioSocketAcceptor; //导入方法依赖的package包/类
/**
* Constructor
*/
public MyServer() {
try {
acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("threadPool",
new ExecutorFilter(new OrderedThreadPoolExecutor()));// 设置线程池,以支持多线程
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
/**
* 默认编码器,解码器,遇到\n默认消息结束 当然可以加参数指定解码字符,但解码字符会被截掉 例如:new
* TextLineCodecFactory(Charset.forName("UTF-8"),"]","]");
* 则会认为"]"为一条消息结束,遇到"]"则截取 比如服务器给你发送的消息是aaaa]aaaa] 会收到两条消息: 1、aaaa
* 2、aaaa 后面的"]"则去掉了
*/
// acceptor.getFilterChain().addLast(
// "codec",
// new ProtocolCodecFilter(new TextLineCodecFactory(Charset
// .forName("UTF-8"))));// 指定编码过滤器
DemuxingProtocolCodecFactory pcf = new DemuxingProtocolCodecFactory();
// 自定义编码器
pcf.addMessageEncoder(String.class, new MyMessageEncoder());
// 自定义解码器
pcf.addMessageDecoder(new MyMessageDecoder());
ProtocolCodecFilter codec = new ProtocolCodecFilter(pcf);
acceptor.getFilterChain().addLast("codec", codec);// 指定编码过滤器
acceptor.setReuseAddress(true);
acceptor.setHandler(new ServerIoHandler());// 指定业务逻辑处理器
acceptor.setDefaultLocalAddress(new InetSocketAddress(8888));// 设置端口号
acceptor.bind();// 启动监听
} catch (Exception e) {
e.printStackTrace();
}
}