He estado haciendo unas prubas mínimas con las interfaz autocloseable
introducida en java 7 … y la verdad es que está muy bien y hace que
quede el código típico de try{} cath{} finally{} mucho más limpio.
Dejo aquí un ejemplo básico de uno de sus posible usos.
public class HBaseUtil implements AutoCloseable
{
private Connection connection;
public HBaseUtil() throws IOException
{
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","lug000.zylk.net,lug008.zylk.net");
conf.set("hbase.zookeeper.property.clientPort","2181");
conf.set("zookeeper.recovery.retry","5");
conf.set("zookeeper.session.timeout","5000");
conf.set("hbase.client.retries.number","3");
conf.set("zookeeper.znode.parent", "/hbase-unsecure");
this.connection = ConnectionFactory.createConnection(conf);
}
private Connection getConnection()
{
return this.connection;
}
public void add() throws IOException {
this.getConnection();
//add element code
}
@Override
public void close() throws Exception {
if(!this.connection.isClosed())
this.connection.close();
}
public static void main(String[] args) throws IOException
{
try (HBaseUtil hbu = new HBaseUtil())
{
hbu.add();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}







