本文整理匯總了Java中org.apache.mina.transport.socket.nio.NioSocketConnector.connect方法的典型用法代碼示例。如果您正苦於以下問題:Java NioSocketConnector.connect方法的具體用法?Java NioSocketConnector.connect怎麽用?Java NioSocketConnector.connect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.mina.transport.socket.nio.NioSocketConnector
的用法示例。
在下文中一共展示了NioSocketConnector.connect方法的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: 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));
}
示例3: 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();
}
示例4: 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();
}
示例5: 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);
}
示例6: 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;
}
示例7: 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;
}
示例8: 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();
}
}
示例9: 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();
}
示例10: 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();
}
示例11: main
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
// if (args.length != 2) {
// System.out.println(Main.class.getName() + " <hostname> <port>");
// return;
// }
DateTime endTime = DateTime.now().withTimeAtStartOfDay();
DateTime startTime = endTime.minusMonths(1);
List<DateTime> dayStarts=dayStarts(startTime, endTime);
for (DateTime dateTime : dayStarts) {
System.out.println(dateTime.toString("yyyy-MM-dd HH:mm:ss"));
}
// Create TCP/IP connector.
NioSocketConnector connector = new NioSocketConnector();
// Set connect timeout.
connector.setConnectTimeoutMillis(30 * 1000L);
// Start communication.
connector.setHandler(new NetCatProtocolHandler());
ConnectFuture cf = connector.connect(new InetSocketAddress(args[0], Integer.parseInt(args[1])));
// Wait for the connection attempt to be finished.
cf.awaitUninterruptibly();
cf.getSession().getCloseFuture().awaitUninterruptibly();
connector.dispose();
}
示例12: 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();
}
示例13: connect
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
public boolean connect(NioSocketConnector connector, SocketAddress address) {
if(!isSetChain){
throw new IllegalStateException(
"please set ConservationChain first !");
}
if (session != null && session.isConnected()) {
throw new IllegalStateException(
"Already connected. Disconnect first.");
}
try {
IoFilter CODEC_FILTER = new ProtocolCodecFilter(
new GameProtocolcodecFactory());
connector.getFilterChain().addLast("codec", CODEC_FILTER);
connector.setHandler(handler);
ConnectFuture future1 = connector.connect(address);
future1.awaitUninterruptibly();
if (!future1.isConnected()) {
return false;
}
session = future1.getSession();
return true;
} catch (Exception e) {
return false;
}
}
示例14: connect
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
public void connect(String host, int port) throws IOException, ExecutionException, InterruptedException {
this.host = host;
this.port = port;
connector = new NioSocketConnector();
connector.setConnectTimeoutMillis(4000);
connector.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
connector.getSessionConfig().setIdleTime(IdleStatus.WRITER_IDLE, 10);
connector.getSessionConfig().setIdleTime(IdleStatus.READER_IDLE, 10);
connector.getSessionConfig().setReceiveBufferSize(2048);
connector.setHandler(this);
System.out.println("Connecting.. " + host + ":" + port + DateTime.now().toString());
future = connector.connect(new InetSocketAddress(host, port));
}
示例15: connectToServer
import org.apache.mina.transport.socket.nio.NioSocketConnector; //導入方法依賴的package包/類
/**
* Connect to remote message server.
* @return
*/
public boolean connectToServer() {
try {
resourceLock.lock();
if ( log.isInfoEnabled() ) {
log.info("Connect to message server : " + remoteHost + ":" + remotePort);
}
connector = new NioSocketConnector();
connector.getFilterChain().addLast("codec", new GameProtocolCodecFilter());
connector.setHandler(this.handler);
int heartBeatSecond = GlobalConfig.getInstance().getIntProperty("message.heartbeat.second");
if ( log.isDebugEnabled() ) {
log.debug("heartBeatSecond : " + heartBeatSecond);
}
connector.getSessionConfig().setBothIdleTime(heartBeatSecond);
// Make a new connection
ConnectFuture connectFuture = connector.connect(new InetSocketAddress(remoteHost, remotePort));
// Wait until the connection is make successfully.
connectFuture.awaitUninterruptibly(CONNECT_TIMEOUT);
try {
session = connectFuture.getSession();
//Tell caller we can write message.
// connectedCond.signal();
if ( log.isInfoEnabled() ) {
log.info("connect to " + remoteHost + ":" + remotePort);
}
return true;
}
catch (Throwable e) {
disconnectFromServer();
return false;
}
} finally {
resourceLock.unlock();
}
}