本文整理匯總了Java中org.apache.mina.transport.socket.nio.NioSocketConnector.getFilterChain方法的典型用法代碼示例。如果您正苦於以下問題:Java NioSocketConnector.getFilterChain方法的具體用法?Java NioSocketConnector.getFilterChain怎麽用?Java NioSocketConnector.getFilterChain使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.mina.transport.socket.nio.NioSocketConnector
的用法示例。
在下文中一共展示了NioSocketConnector.getFilterChain方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initConnector
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
private void initConnector(InetSocketAddress isa, TransportConfig config) {
// create connector
ioConnector = new NioSocketConnector(1);
ioConnector.setConnectTimeoutMillis(config.getConnectTimeout());
SocketSessionConfig sessionConfig = ioConnector.getSessionConfig();
sessionConfig.setTcpNoDelay(true);
sessionConfig.setReceiveBufferSize(8 * 1024);
sessionConfig.setSendBufferSize(8 * 1024);
DefaultIoFilterChainBuilder chainBuilder = ioConnector.getFilterChain();
final TransportProtocolEncoder encoder = new TransportProtocolEncoder();
final TransportProtocolDecoder decoder = new TransportProtocolDecoder();
encoder.setMaxObjectSize(config.getMaxSize());
decoder.setMaxObjectSize(config.getMaxSize());
// add filter
chainBuilder.addLast("codec", new ProtocolCodecFilter(encoder, decoder));
// add keep alive filter
ActiveKeepAliveMessageFactory messageFactory = new ActiveKeepAliveMessageFactory(config.getSerializeType());
KeepAliveFilter kaFilter = new KeepAliveFilter(messageFactory, IdleStatus.BOTH_IDLE);
kaFilter.setRequestInterval(config.getHeartbeatIntervalSeconds());
kaFilter.setForwardEvent(true);
chainBuilder.addLast("keepAlive", kaFilter);
// set handler
ioConnector.setHandler(new MinaIoHandler());
ioConnector.setDefaultRemoteAddress(isa);
}
示例2: connect
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
/**
* �����ͻ�������
*/
public boolean connect() {
// ʵ���� �������� Socket����
client = new NioSocketConnector();
// �������ݹ�����
DefaultIoFilterChainBuilder filterChain = client.getFilterChain();
//filterChain.addLast("textCode", new ProtocolCodecFilter(
// new TextLineCodecFactory(Charset.forName("UTF-8"))));
filterChain.addLast("myChin", new ProtocolCodecFilter(
new ObjectSerializationCodecFactory()));
// �ͻ��˴������
ClientIoHandler clientIoHandler = new ClientIoHandler(loginFrame,client);
client.setHandler(clientIoHandler);
clientIoHandler.setRegisterFrame(registerFrame);
// ���ӷ�����
ConnectFuture future = client.connect(new InetSocketAddress(
IP, Port));
// �ȴ�
future.awaitUninterruptibly();
// �õ��Ự����
try {
session = future.getSession();
return true;
} catch (Exception e) {
Tools.show(loginFrame, "�����ӷ�������������û������");
client.dispose();
if(registerFrame!=null)
registerFrame.dispose();
return false;
}
// session.getCloseFuture().awaitUninterruptibly();
}
示例3: start
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
public void start() throws Exception {
buildExecutors();
IoProcessor<NioSession> processor = new SimpleIoProcessorPool<NioSession>(
NioProcessor.class, ioExecutor, coreSize);
connector = new NioSocketConnector(connectorExecutor, processor);
connector.setConnectTimeoutMillis((Integer) rule.get("timeout")); // 設置連接超時。見AbstractPollingIoConnector.processTimedOutSessions()與ConnectionRequest類
// connector.getSessionConfig().setUseReadOperation(true); //
// 亦可使用該方式實現同步發送並接收數據,這樣無須設置Handler,通過session.read()獲取
handler = new ShortConnectorHandler();
connector.setHandler(handler);
DefaultIoFilterChainBuilder filterChain = connector.getFilterChain();
filterChain.addLast("codec", new ProtocolCodecFilter(
ProtocolCodecFactoryFactory.getInstance(rule)));
}
示例4: main
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
public static void main(String[] args) {
NioSocketConnector connector = new NioSocketConnector();
DefaultIoFilterChainBuilder chain = connector.getFilterChain();
SocketSessionConfig config = connector.getSessionConfig();
config.setReuseAddress(true);
config.setIdleTime(IdleStatus.READER_IDLE, 20);// 讀空閑 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());
connector.setHandler(new MinaMessageHandler());
connector.setConnectTimeoutMillis(15 * 1000); // 設置連接超時 ,15秒
connector.setConnectTimeoutCheckInterval(300);
// 建立連接
ConnectFuture cf = connector.connect(new InetSocketAddress("localhost", 8000));
// 等待連接創建完成
cf.awaitUninterruptibly();
Packet packet = Packet.newBuilder().setCmdId(0).setChatMsgAck(ChatMsgAck.newBuilder().setResult(123)).build();
cf.getSession().write(packet.toByteArray());
packet = Packet.newBuilder().setCmdId(1).setChatMsgAck(ChatMsgAck.newBuilder().setResult(123)).build();
cf.getSession().write(packet.toByteArray());
// 等待連接斷開
cf.getSession().getCloseFuture().awaitUninterruptibly();
// 釋放連接
connector.dispose();
}
示例5: MinaConnector
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
public MinaConnector(CodecFactory codecFactory, int maxIoThreads, int maxTaskThreads, TaskMapper taskMapper) {
Objects.requireNonNull(codecFactory);
log.info("Initializing connector {max I/O threads: {}, max task threads: {}}",
Integer.toString(maxIoThreads),
maxTaskThreads > 0
? Integer.toString(maxTaskThreads)
: "unbounded");
connector = new NioSocketConnector(maxIoThreads);
DefaultIoFilterChainBuilder filters = connector.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();
connector.setHandler(minaAdapter);
// connector configuration
connector.getSessionConfig().setReuseAddress(true);
connector.getSessionConfig().setKeepAlive(true);
connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, DEFAULT_SESSION_IDLE_SECONDS);
// no Nagle's algorithm
connector.getSessionConfig().setTcpNoDelay(true);
}
示例6: start
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
private void start() {
Config conf = ConfigFactory.load();
String check_server_beat_ip = conf.getString("check_server_beat_ip");
int check_server_beat_port = conf.getInt("check_server_beat_port");
if (U.isEmpty(check_server_beat_ip)) {
log.error("BeatClient 連接心跳服務器 沒有配置!");
return;
}
NioSocketConnector connector = new NioSocketConnector();
DefaultIoFilterChainBuilder chain = connector.getFilterChain();
chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
connector.setHandler(new BeatHanlder());
connector.setConnectTimeoutMillis(15 * 1000); // 設置連接超時 ,15秒
connector.setConnectTimeoutCheckInterval(300);
long waitTimeConnect = 2 * 1000;
final long waitTimeMAX = 30 * 1000;
final long waitTimeSpace = 2 * 1000;
for (;;) {
try {
ConnectFuture cf = connector
.connect(new InetSocketAddress(check_server_beat_ip, check_server_beat_port));
cf.awaitUninterruptibly();
IoSession session = cf.getSession();// 獲取會話
if (session.isConnected()) {
log.info("BeatClient 連接心跳服務器OK IP:" + check_server_beat_ip + ",PORT:" + check_server_beat_port);
break;
}
Thread.sleep(waitTimeConnect);
} catch (Exception e) {
log.error("ERROR_connect :" + e.getMessage());
try {
Thread.sleep(waitTimeConnect);
} catch (InterruptedException e1) {
}
} finally {
waitTimeConnect += waitTimeSpace;
if (waitTimeConnect > waitTimeMAX) {
waitTimeConnect = waitTimeSpace;
}
}
}
}
示例7: MyClient
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
/**
* Constructor
*/
public MyClient() {
connector = new NioSocketConnector();
/**
* 設置信息交換的IoHandler,負責接收和發送信息的處理
*/
connector.setHandler(new ClientIoHandler());
//配置過濾器
DefaultIoFilterChainBuilder chain = connector.getFilterChain();
//增加日誌過濾器
chain.addLast("logger", new LoggingFilter());
//增加字符編碼過濾器以及設置編碼器和解碼器
//chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
/**
* 默認編碼器,解碼器,遇到\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);
chain.addLast("codec",codec);// 指定編碼過濾器
//設置默認連接的地址和端口
connector.setDefaultRemoteAddress(new InetSocketAddress("localhost", 8888));
/**
* 重連機製
* 如果沒有連接,則過30 * 1000毫秒客戶端會嘗試重新連接服務器
* 如果連接,則下麵的代碼不會執行
*/
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if (null != connector && !connector.isActive()) {
try {
//嘗試連接默認的地址和端口
ConnectFuture connFuture = connector.connect();
connFuture.awaitUninterruptibly();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}, new Date(), 30 * 1000);
}