Blogs

Java AutoCloseable interface

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();
        }
    }
}

Más entradas de blog

Share action to copy an Alfresco link in your email client

Some time ago, we published an Alfresco addon for editing online with Libreoffice in...

Change Alfresco ports in Alfresco 5

Sometimes we need to change the main ports of an Alfresco web application (i.e 8080 -->...

Añadir comentarios