本文整理匯總了Java中org.apache.mina.transport.socket.nio.NioSocketConnector.setHandler方法的典型用法代碼示例。如果您正苦於以下問題:Java NioSocketConnector.setHandler方法的具體用法?Java NioSocketConnector.setHandler怎麽用?Java NioSocketConnector.setHandler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.mina.transport.socket.nio.NioSocketConnector
的用法示例。
在下文中一共展示了NioSocketConnector.setHandler方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: connect
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
public void connect() {
synchronized(mutex) {
if (connected.getFlag()) return;
if (connecting.getFlag()) return;
log.warning("Connecting to TC at " + getHost() + ":" + getPort() + " ...");
connecting.setFlag(true);
}
try {
ioConnector = new NioSocketConnector();
ioConnector.setHandler(this);
ioConnector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
connectFuture = ioConnector.connect(address);
connectFuture.addListener(connectionListener);
} catch (Exception e1) {
try {
connecting.setFlag(false);
} catch (Exception e2) {
}
}
}
示例2: clientStart
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
@Override
protected void clientStart() throws RemotingException {
try {
connector = new NioSocketConnector(); //TCP Connector
// connector.getFilterChain().addFirst("logging", new MinaLoggingFilter());
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecFactory(getCodec())));
connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
connector.setHandler(new MinaHandler(this));
IoSessionConfig cfg = connector.getSessionConfig();
cfg.setReaderIdleTime(remotingClientConfig.getReaderIdleTimeSeconds());
cfg.setWriterIdleTime(remotingClientConfig.getWriterIdleTimeSeconds());
cfg.setBothIdleTime(remotingClientConfig.getClientChannelMaxIdleTimeSeconds());
} catch (Exception e) {
throw new RemotingException("Mina Client start error", e);
}
}
示例3: init
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
private void init(Config config) {
address = new InetSocketAddress(config.targetIP, config.targetPort);
connector = new NioSocketConnector();
connector.setDefaultRemoteAddress(address);
if (connector.getFilterChain().get(LOGGER) == null) {
connector.getFilterChain().addLast(LOGGER, new LoggingFilter());
}
if (connector.getFilterChain().get(CODEC) == null) {
ProtocolCodecFactory codecFactory = config.codec == null ?
MinaUtil.getTextLineCodecFactory() : (ProtocolCodecFactory) config.codec;
connector.getFilterChain().addLast(CODEC, new ProtocolCodecFilter(codecFactory));
}
connector.setHandler(new MinaClientHandler());
connector.getSessionConfig().setReadBufferSize(config.bufferSize);
connector.getSessionConfig().setIdleTime(IdleStatus.WRITER_IDLE, 10);
KeepAliveFilter keepAliveFilter = MinaUtil.initClientKeepAlive(config, this);
if (keepAliveFilter != null) {
connector.getFilterChain().addLast(HEARTBEAT, keepAliveFilter);
}
}
示例4: connect
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
/**
* 執行連接操作的方法
*
* @throws RedisProtocolException
* 當連接出現問題時拋出該異常
*/
private void connect() throws RedisProtocolException {
connector = new NioSocketConnector();
connector.setConnectTimeoutMillis(connectionTimeOut);
connector.getFilterChain().addFirst("redis-protocol", new ProtocolCodecFilter(new RedisProtocolCodecFactory()));
connector.setHandler(this);
connector.connect(new InetSocketAddress(address, port));
}
示例5: Work
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
public Work(){
NioSocketConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast( "logger", new LoggingFilter() );
connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new MessageCodecFactory())); //設置編碼過濾器
connector.setConnectTimeoutMillis(30000);
connector.setHandler(new ClientMessageHandler());//設置事件處理器
ConnectFuture cf = connector.connect(
new InetSocketAddress("127.0.0.1", 50000));//建立連接
cf.awaitUninterruptibly();//等待連接創建完成
ByteArray ba = new ByteArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("parseId", 1);
jsonObject.put("command", 1);
jsonObject.put("text", "塗鴉");
ba.writeUTF(jsonObject.toJSONString());
cf.getSession().write(ba.toArray());
// cf.getSession().write("塗鴉");//發送消息
// cf.getSession().write("quit");//發送消息
// cf.getSession().getCloseFuture().awaitUninterruptibly();//等待連接斷開
// connector.dispose();
}
示例6: main
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
public static void main(String[] args) {
NioSocketConnector connector = new NioSocketConnector(); //TCP Connector
connector.getFilterChain().addLast("logging", new LoggingFilter());
connector.getFilterChain().addLast("codec",new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
connector.setHandler(new HelloClientHandler());
IoSession session;
for (;;) {
try {
ConnectFuture future = connector.connect(new InetSocketAddress(HOSTNAME, PORT));
future.awaitUninterruptibly();
session = future.getSession();
break;
} catch (RuntimeIoException e) {
System.err.println("Failed to connect.");
e.printStackTrace();
}
}
session.getCloseFuture().awaitUninterruptibly();
connector.dispose();
}
示例7: connect
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
/**
* connects to a LLRP device at the host address and port specified. the connect method waits
* for the timeperiod specified (in ms) for a response. If the READER_NOTIFICATION does not arrive
* or the ConnectionAttemptEventStatus
* is not set to 'Success', a LLRPConnectionAttemptFailedException is thrown.
*
* @param timeout time in ms
* @throws LLRPConnectionAttemptFailedException
*/
public void connect(long timeout) throws LLRPConnectionAttemptFailedException{
connector = new NioSocketConnector();
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new LLRPProtocolCodecFactory(LLRPProtocolCodecFactory.BINARY_ENCODING)));
// MINA 2.0 method
connector.setHandler(handler);
remoteAddress = new InetSocketAddress(host, port);
ConnectFuture future = connector.connect(remoteAddress);//.connect(remoteAddress,handler);
future.join();// Wait until the connection attempt is finished.
if(future.isConnected()){
session = future.getSession();
}else{
String msg = "failed to connect";
throw new LLRPConnectionAttemptFailedException(msg);
}
// MINA 2.0
//future.awaitUninterruptibly();
//check if llrp reader reply with a status report to indicate connection success.
//the client shall not send any information to the reader until this status report message is received
checkLLRPConnectionAttemptStatus(timeout);
}
示例8: init
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
protected void init() {
DemuxingProtocolDecoder decoder = new DemuxingProtocolDecoder();
decoder.addMessageDecoder(new ConnAckDecoder());
decoder.addMessageDecoder(new SubAckDecoder());
decoder.addMessageDecoder(new UnsubAckDecoder());
decoder.addMessageDecoder(new PublishDecoder());
decoder.addMessageDecoder(new PubAckDecoder());
decoder.addMessageDecoder(new PingRespDecoder());
DemuxingProtocolEncoder encoder = new DemuxingProtocolEncoder();
encoder.addMessageEncoder(ConnectMessage.class, new ConnectEncoder());
encoder.addMessageEncoder(PublishMessage.class, new PublishEncoder());
encoder.addMessageEncoder(SubscribeMessage.class, new SubscribeEncoder());
encoder.addMessageEncoder(UnsubscribeMessage.class, new UnsubscribeEncoder());
encoder.addMessageEncoder(DisconnectMessage.class, new DisconnectEncoder());
encoder.addMessageEncoder(PingReqMessage.class, new PingReqEncoder());
m_connector = new NioSocketConnector();
// m_connector.getFilterChain().addLast("logger", new LoggingFilter());
m_connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(encoder, decoder));
m_connector.setHandler(new DummyClientHandler());
m_connector.getSessionConfig().setReadBufferSize(2048);
m_connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, Constants.DEFAULT_CONNECT_TIMEOUT);
}
示例9: buildConnection
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
public void buildConnection() {
NioSocketConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(MessageCodecFactory.getInstance()));
connector.setHandler(new ClientHandler());
System.out.println("開始連接socket服務端");
int serverPort = ServerConfig.getInstance().getServerPort();
ConnectFuture future = connector.connect(new InetSocketAddress(serverPort));
future.awaitUninterruptibly();
IoSession session = future.getSession();
this.session = session;
}
示例10: createConnector
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
/**
* Create the connector
*/
private void createConnector() throws LdapException
{
// Use only one thread inside the connector
connector = new NioSocketConnector( 1 );
if ( connectionConfig != null )
{
( ( SocketSessionConfig ) connector.getSessionConfig() ).setAll( connectionConfig );
}
else
{
( ( SocketSessionConfig ) connector.getSessionConfig() ).setReuseAddress( true );
}
// Add the codec to the chain
connector.getFilterChain().addLast( "ldapCodec", ldapProtocolFilter );
// If we use SSL, we have to add the SslFilter to the chain
if ( config.isUseSsl() )
{
addSslFilter();
}
// Inject the protocolHandler
connector.setHandler( this );
}
示例11: start
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
@Override
public Object start ( final IApplicationContext context ) throws Exception
{
final NioSocketConnector connector = new NioSocketConnector ();
connector.setHandler ( new SingleSessionIoHandlerDelegate ( new SingleSessionIoHandlerFactory () {
@Override
public SingleSessionIoHandler getHandler ( final IoSession session ) throws Exception
{
return new DaveHandler ( session );
}
} ) );
connector.getFilterChain ().addLast ( "logger", new LoggingFilter ( this.getClass ().getName () ) );
connector.getFilterChain ().addLast ( "tpkt", new TPKTFilter ( 3 ) );
connector.getFilterChain ().addLast ( "cotp", new COTPFilter ( 0, (byte)3 ) );
connector.getFilterChain ().addLast ( "dave", new DaveFilter () );
connector.connect ( new InetSocketAddress ( "192.168.1.83", 102 ) );
while ( this.running )
{
Thread.sleep ( 1000 );
}
return null;
}
示例12: connect
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
private synchronized void connect (boolean bIsReconnect, boolean bUseReadOperation) {
try {
if (mFuture != null && mFuture.isConnected()) {
return;
}
if (mPort == 0) {
return;
}
mConnector = new NioSocketConnector();
//設置連接超時時間
mConnector.setConnectTimeoutMillis(2000);
mConnector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MessageCodecFactory(Charset.forName("UTF-8"))));
mConnector.setHandler(mClientHandler);
// logger.info("嘗試連接 Server " + mAddr + ":" + mPort+"--------------------------------------------");
mFuture = mConnector.connect(new InetSocketAddress(mAddr, mPort));
//設置IDLE時間
mConnector.getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, 40);
mConnector.getSessionConfig().setTcpNoDelay(false);
mConnector.getSessionConfig().setUseReadOperation(bUseReadOperation);
mFuture.awaitUninterruptibly();
boolean connected=mFuture.isConnected();
if (!connected && bIsReconnect) {
// logger.info("connect fail mFuture=[ " + mFuture + "],mAddr =[" + mAddr + ":" + mPort);
reset();
mConnector.dispose(true);
//如果鏈接失敗,則重新啟動鏈接重試線程
(new Thread(this)).start();
} else {
//如果連接成功,則發送所有隊列中的消息
logger.info("connect success " + mAddr + ":" + mPort);
sendAllMsgInQueue();
}
} catch (Exception e) {
logger.fatal("SocketClient ["+mAddr+" : "+ mPort+"] connect()失敗 Exception: " + e + " " + Arrays.toString(e.getStackTrace()));
//如果鏈接失敗,則重新啟動鏈接重試線程
(new Thread(this)).start();
}
}
示例13: MinaCubeClient
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
public MinaCubeClient(String hostname, int port) {
this.hostname = hostname;
this.port = port;
connector = new NioSocketConnector(1);
connector.setConnectTimeoutMillis(2000);
connector.getFilterChain().addLast("logger", new LoggingFilter());
TextLineCodecFactory codecFactory = new TextLineCodecFactory(UTF_8, LineDelimiter.CRLF, LineDelimiter.AUTO);
codecFactory.setDecoderMaxLineLength(4096);
codecFactory.setEncoderMaxLineLength(4096);
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(codecFactory));
handler = new ConnectHandler();
connector.setHandler(handler);
}
示例14: main
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
public static void main(String[] args) {
// 創建客戶端連接器.
NioSocketConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast("logger", new LoggingFilter());
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); // 設置編碼過濾器
connector.setConnectTimeout(30);
connector.setHandler(new TimeClientHandler());// 設置事件處理器
ConnectFuture cf = connector.connect(new InetSocketAddress("127.0.0.1", 9123));// 建立連接
cf.awaitUninterruptibly();// 等待連接創建完成
cf.getSession().write("hello");// 發送消息
cf.getSession().write("quit");// 發送消息
cf.getSession().getCloseFuture().awaitUninterruptibly();// 等待連接斷開
connector.dispose();
}
示例15: 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);
}