logger.info("client already opened, skip");
return;
}
String lockPath = "/exchange/" + exchange.toString() + ".lock";
ZLock lock = new ZLock(lockPath);
brokerDetermined = determined;
try {
if(brokerDetermined) {
exchange.setBroker(sessionConfig.getConnection().getHostName());
logger.info(String.format("exchange %s uses determined broker %s", exchange.getName(), exchange.getBroker()));
connection = Daemon.getConnection(sessionConfig.getConnection());
}
else {
logger.info("getting exchange lock: " + lockPath);
lock.acquire(LockType.WRITE_LOCK);
logger.info("exchange lock: " + lockPath + " acquired");
exchange.setBroker(ExchangeFarm.getInstance().queryExchangeHost(exchange));
if(exchange.getBroker() == null)
throw new MistException(String.format("can not request broker from MFR for `%s:%s'", isQueue() ? "queue": "topic", getChannelName()));
logger.info(String.format("request exchange %s, select broker: %s", exchange.getName(), exchange.getBroker()));
connection = Daemon.getConnection(exchange.getBroker());
}
if(isResume)
oldConnection.decreaseReference();
javax.jms.Connection jms_conn = connection.getJMSConnection();
jms_session = jms_conn.createSession(false, javax.jms.Session.CLIENT_ACKNOWLEDGE);
if(isQueue())
destination = jms_session.createQueue(exchange.getName());
else
destination = jms_session.createTopic(exchange.getName());
if(isConsumer()) {
consumer = jms_session.createConsumer(destination);
}
else {
producer = jms_session.createProducer(destination);
if(getConfig().getChannel().getPersistent())
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
else
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
}
logger.info(String.format("attached to %s", getInfo()));
if(!brokerDetermined) {
if(!isResume && !isMigrate) {
String exchangeRefPath = ExchangeFarm.getInstance().incExchangeRef(exchange);
logger.info("exchangeRefPath added: " + exchangeRefPath);
}
}
try {
BrokerSpy.setExchangeFlowControl(exchange, ExchangeFarm.getDropPolicy(exchange));
}
catch(Exception e) {
logger.error(e.getMessage(), e);
}
ZooKeeperInfo.TotalLimit limit = ExchangeFarm.getTotalLimit(exchange);
try {
BrokerSpy.setExchangeTotalLimit(exchange, limit.getSizeBytes(), limit.getCount());
}
catch(Exception e) {
logger.error(e.getMessage(), e);
}
opened = true;
}
catch(JMSException e) {
logger.error("fail to create session level objects: " + e.getMessage(), e);
throw new MistException(e.getMessage());
}
catch(CODIException e) {
logger.error(e.getMessage(), e);
throw new MistException(e.getMessage());
}
catch(InterruptedException e) {
logger.error(e.getMessage(), e);
throw new MistException(e.getMessage());
}
finally {
if(!brokerDetermined) {
try {
lock.release();
}
catch(CODIException e) {
}
logger.info("lock released: " + lockPath);
}