return null;
synchronized (connectionLock)
{
if (connection == null)
{
Connection tc = null;
try
{
DelegatingBufferHandler handler = new DelegatingBufferHandler();
connector = connectorFactory.createConnector(connectorConfig.getParams(),
handler,
this,
closeExecutor,
threadPool,
scheduledThreadPool);
if (HornetQClientLogger.LOGGER.isDebugEnabled())
{
HornetQClientLogger.LOGGER.debug("Trying to connect with connector = " + connectorFactory +
", parameters = " +
connectorConfig.getParams() +
" connector = " +
connector);
}
if (connector != null)
{
connector.start();
if (ClientSessionFactoryImpl.isDebug)
{
HornetQClientLogger.LOGGER.debug("Trying to connect at the main server using connector :" + connectorConfig);
}
tc = connector.createConnection();
if (tc == null)
{
if (ClientSessionFactoryImpl.isDebug)
{
HornetQClientLogger.LOGGER.debug("Main server is not up. Hopefully there's a backup configured now!");
}
try
{
connector.close();
}
catch (Throwable t)
{
}
connector = null;
}
}
// if connection fails we can try the backup in case it has come live
if (connector == null)
{
if (backupConfig != null)
{
if (ClientSessionFactoryImpl.isDebug)
{
HornetQClientLogger.LOGGER.debug("Trying backup config = " + backupConfig);
}
ConnectorFactory backupConnectorFactory = instantiateConnectorFactory(backupConfig.getFactoryClassName());
connector = backupConnectorFactory.createConnector(backupConfig.getParams(),
handler,
this,
closeExecutor,
threadPool,
scheduledThreadPool);
if (connector != null)
{
connector.start();
tc = connector.createConnection();
if (tc == null)
{
if (ClientSessionFactoryImpl.isDebug)
{
HornetQClientLogger.LOGGER.debug("Backup is not active yet");
}
try
{
connector.close();
}
catch (Throwable t)
{
}
connector = null;
}
else
{
/*looks like the backup is now live, let's use that*/
if (ClientSessionFactoryImpl.isDebug)
{
HornetQClientLogger.LOGGER.debug("Connected to the backup at " + backupConfig);
}
connectorConfig = backupConfig;
backupConfig = null;
connectorFactory = backupConnectorFactory;
}
}
}
else
{
if (ClientSessionFactoryImpl.isTrace)
{
HornetQClientLogger.LOGGER.trace("No Backup configured!", new Exception("trace"));
}
}
}
}
catch (Exception cause)
{
// Sanity catch for badly behaved remoting plugins
HornetQClientLogger.LOGGER.createConnectorException(cause);
if (tc != null)
{
try
{
tc.close();
}
catch (Throwable t)
{
}
}