本文整理匯總了Java中org.apache.mina.core.future.ConnectFuture類的典型用法代碼示例。如果您正苦於以下問題:Java ConnectFuture類的具體用法?Java ConnectFuture怎麽用?Java ConnectFuture使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ConnectFuture類屬於org.apache.mina.core.future包,在下文中一共展示了ConnectFuture類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: callNextExceptionCaught
import org.apache.mina.core.future.ConnectFuture; //導入依賴的package包/類
private void callNextExceptionCaught(Entry entry, IoSession session, Throwable cause) {
// Notify the related future.
ConnectFuture future = (ConnectFuture) session.removeAttribute(SESSION_CREATED_FUTURE);
if (future == null) {
try {
IoFilter filter = entry.getFilter();
NextFilter nextFilter = entry.getNextFilter();
filter.exceptionCaught(nextFilter, session, cause);
} catch (Throwable e) {
LOGGER.warn("Unexpected exception from exceptionCaught handler.", e);
}
} else {
// Please note that this place is not the only place that
// calls ConnectFuture.setException().
session.close(true);
future.setException(cause);
}
}
示例2: startConnect
import org.apache.mina.core.future.ConnectFuture; //導入依賴的package包/類
private synchronized void startConnect ( final InetAddress address )
{
logger.debug ( "Start connection to {}", address );
setState ( ConnectionState.CONNECTING, null );
this.connectFuture = this.connector.connect ( new InetSocketAddress ( address, this.connectionInformation.getSecondaryTarget () ) );
logger.trace ( "Returned from connect call" );
this.connectFuture.addListener ( new IoFutureListener<ConnectFuture> () {
@Override
public void operationComplete ( final ConnectFuture future )
{
handleConnectComplete ( future );
}
} );
logger.trace ( "Future listener registered" );
}
示例3: start
import org.apache.mina.core.future.ConnectFuture; //導入依賴的package包/類
@Override
public boolean start() {
super.start();
isAutoReConn = true;
if (isRunning()) {
return false;
}
init(config);
ConnectFuture future = connector.connect();
future.awaitUninterruptibly();
try {
mSession = future.getSession();
} catch (Exception e) {
onStartFailed(e);
return false;
}
onStartSuccess();
return true;
//return mSession == null ? false : true;
}
示例4: openConnection
import org.apache.mina.core.future.ConnectFuture; //導入依賴的package包/類
private void openConnection() {
if (this.address == null || !this.configuration.isCachedAddress()) {
setSocketAddress(this.configuration.getProtocol());
}
if (LOG.isDebugEnabled()) {
LOG.debug("Creating connector to address: {} using connector: {} timeout: {} millis.", new Object[]{address, connector, timeout});
}
// connect and wait until the connection is established
if (connectorConfig != null) {
connector.getSessionConfig().setAll(connectorConfig);
}
connector.setHandler(new ResponseHandler());
ConnectFuture future = connector.connect(address);
future.awaitUninterruptibly();
session = future.getSession();
}
示例5: getSession
import org.apache.mina.core.future.ConnectFuture; //導入依賴的package包/類
@Override
public IoSession getSession() throws SyslogSenderException {
if (session == null || !session.isConnected()) {
synchronized (this) {
LOG.debug("Lazily open connection to address: {} using connector: {}", socketAddress, connector);
// connect and wait until the connection is established
if (connectorConfig != null) {
connector.getSessionConfig().setAll(connectorConfig);
}
connector.setHandler(new ResponseHandler());
ConnectFuture future = connector.connect(socketAddress);
future.awaitUninterruptibly();
try {
session = future.getSession();
if (session == null) {
throw new SyslogSenderException("Could not establish connection to " + socketAddress);
}
} catch (RuntimeException e) {
throw new SyslogSenderException("Could not establish connection to " + socketAddress, e);
}
}
}
return session;
}
示例6: Work
import org.apache.mina.core.future.ConnectFuture; //導入依賴的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();
}
示例7: connect
import org.apache.mina.core.future.ConnectFuture; //導入依賴的package包/類
/**
* Attempt to connect to the info server.
*/
public void connect()
{
try
{
ConnectFuture future = connector.connect(new InetSocketAddress(IP, PORT));
future.awaitUninterruptibly(AWAIT_TIME);
IoSession session = future.getSession();
session.getFilterChain().addFirst("protocol", new ProtocolCodecFilter(factory));
state = State.READY;
this.onConnect(session);
}
catch(RuntimeIoException e)
{
this.onFailure(e);
}
}
示例8: main
import org.apache.mina.core.future.ConnectFuture; //導入依賴的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();
}
示例9: connect
import org.apache.mina.core.future.ConnectFuture; //導入依賴的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);
}
示例10: retryAttemptListener
import org.apache.mina.core.future.ConnectFuture; //導入依賴的package包/類
/**
* Handles the retry attempts. Listens to see when the ConnectFuture is finished and checks if there was
* an exception thrown. If so, it then attempts a retry if there are more retries.
*
* @param connector
* @param detectFuture
* @param address
* @param retryAttempt
* @return IoFutureListener<ConnectFuture>
*/
private final IoFutureListener<ConnectFuture> retryAttemptListener(final DetectFutureMinaImpl detectFuture, final InetSocketAddress address, final IoSessionInitializer<ConnectFuture> init, final int retryAttempt) {
return new IoFutureListener<ConnectFuture>() {
@Override
public void operationComplete(ConnectFuture future) {
final Throwable cause = future.getException();
if (cause instanceof IOException) {
if(retryAttempt == 0) {
LogUtils.infof(this, "Service %s detected false: %s: %s",getServiceName(), cause.getClass().getName(), cause.getMessage());
detectFuture.setServiceDetected(false);
}else {
LogUtils.infof(this, "Connection exception occurred: %s for service %s, retrying attempt %d", cause, getServiceName(), retryAttempt);
future = m_connectionFactory.reConnect(address, init, createDetectorHandler(detectFuture));
future.addListener(retryAttemptListener(detectFuture, address, init, retryAttempt - 1));
}
}else if(cause instanceof Throwable) {
LogUtils.infof(this, cause, "Threw a Throwable and detection is false for service %s", getServiceName());
detectFuture.setServiceDetected(false);
}
}
};
}
示例11: connect
import org.apache.mina.core.future.ConnectFuture; //導入依賴的package包/類
@Override
public final ConnectFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) {
if (isDisposing()) {
throw new IllegalStateException("The connector is being disposed.");
}
if (remoteAddress == null) {
throw new IllegalArgumentException("null remoteAddress");
}
if (!InetSocketAddress.class.isAssignableFrom(remoteAddress.getClass())) {
throw new IllegalArgumentException("remoteAddress type: " + remoteAddress.getClass() + " (expected: InetSocketAddress)");
}
if (localAddress != null && !InetSocketAddress.class.isAssignableFrom(localAddress.getClass())) {
throw new IllegalArgumentException("localAddress type: " + localAddress.getClass() + " (expected: InetSocketAddress)");
}
if (getHandler() == null) {
throw new IllegalStateException("The handler is not set.");
}
return connect0(remoteAddress, localAddress);
}
示例12: callNextExceptionCaught
import org.apache.mina.core.future.ConnectFuture; //導入依賴的package包/類
private void callNextExceptionCaught(Entry entry, Throwable cause) {
// Notify the related future.
ConnectFuture future = (ConnectFuture) session.removeAttribute(SESSION_CREATED_FUTURE);
if (future == null) {
try {
if (entry != null) {
entry.getFilter().exceptionCaught(entry.getNextFilter(), session, cause);
} else {
session.getHandler().exceptionCaught(session, cause);
}
} catch (Throwable e) {
ExceptionMonitor.getInstance().exceptionCaught(e);
}
} else {
// Please note that this place is not the only place that
// calls ConnectFuture.setException().
if (!session.isClosing()) {
// Call the closeNow method only if needed
session.closeNow();
}
future.setException(cause);
}
}
示例13: connect
import org.apache.mina.core.future.ConnectFuture; //導入依賴的package包/類
/**
* <p>Connect to a remote socket. If org.opennms.netmgt.provision.maxConcurrentConnections
* is set, this may block until a connection slot is available.</p>
*
* <p>You must dispose both the {@link ConnectionFactoryNewConnectorImpl} and {@link ConnectFuture} when done
* by calling {@link #dispose(ConnectionFactoryNewConnectorImpl, ConnectFuture)}.</p>
*
* @param remoteAddress
* Destination address
* @param init
* Initialiser for the IoSession
* @return
* ConnectFuture from a Mina connect call
*/
@Override
public ConnectFuture connect(SocketAddress remoteAddress, IoSessionInitializer<? extends ConnectFuture> init, IoHandler handler) {
SocketConnector connector = getSocketConnector(getTimeout(), handler);
InetSocketAddress localAddress = null;
synchronized (m_portMutex) {
if (m_port.get() == null) {
// Fetch a new ephemeral port
localAddress = new InetSocketAddress(0);
m_port.set(localAddress.getPort());
} else {
localAddress = new InetSocketAddress(m_port.get());
}
}
final ConnectFuture cf = connector.connect(remoteAddress, localAddress, init);
cf.addListener(portSwitcher(connector, remoteAddress, init, handler));
cf.addListener(connectorDisposer(connector));
return cf;
}
示例14: connect0
import org.apache.mina.core.future.ConnectFuture; //導入依賴的package包/類
/**
* Connects to the specified <code>address</code>. If communication starts
* successfully, events are fired to the connector's <code>handler</code>.
*
* @param remoteAddress the remote address to connect to
* @param localAddress the local address
* @param sessionInitializer the session initializer
* @return {@link ConnectFuture} that will tell the result of the connection attempt
*/
@SuppressWarnings("unchecked")
@Override
protected ConnectFuture connect0(final SocketAddress remoteAddress, final SocketAddress localAddress,
final IoSessionInitializer<? extends ConnectFuture> sessionInitializer) {
if (!proxyIoSession.isReconnectionNeeded()) {
// First connection
IoHandler handler = getHandler();
if (!(handler instanceof AbstractProxyIoHandler)) {
throw new IllegalArgumentException("IoHandler must be an instance of AbstractProxyIoHandler");
}
connector.setHandler(handler);
future = new DefaultConnectFuture();
}
ConnectFuture conFuture = connector.connect(proxyIoSession.getProxyAddress(), new ProxyIoSessionInitializer(
sessionInitializer, proxyIoSession));
// If proxy does not use reconnection like socks the connector's
// future is returned. If we're in the middle of a reconnection
// then we send back the connector's future which is only used
// internally while <code>future</code> will be used to notify
// the user of the connection state.
if (proxyIoSession.getRequest() instanceof SocksProxyRequest || proxyIoSession.isReconnectionNeeded()) {
return conFuture;
}
return future;
}
示例15: buildConnection
import org.apache.mina.core.future.ConnectFuture; //導入依賴的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;
}