Sunday, 5 April 2015

Please find below the use of java executor and Future task. This example also shows how to handle customed timed out operation. Please watch this tutorial in youtube. link: https://youtu.be/Zd4ckkD7K5E

package test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class TimeoutImpl {

private String mymethod() {
return "Time out situation";

}

public static void main(String[] args) {

ExecutorService service = Executors.newSingleThreadScheduledExecutor();
Future future = null;

Callable callable = new Callable() {

public Object call() throws Exception {
Thread.sleep(1100);
return new TimeoutImpl().mymethod();
}
};


future = service.submit(callable);

try {
System.out.println(future.get(1, TimeUnit.SECONDS));
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
System.out.println("timeout occured");
e.printStackTrace();
}finally{
service.shutdown();
}

}

}

Monday, 30 March 2015

Guava cache example

Please fine below the Google Guava example below:

You can also watch my tutorial at:
https://youtu.be/FCE_AlYjgJ8
https://youtu.be/2AN4H6yF0-Y

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;

public class GuavaTest {

    private LoadingCache<String, String> cache;
   
    private static GuavaTest gt = new GuavaTest();
    public static GuavaTest getInstance(){ return gt; }
    private GuavaTest() {
        cache = CacheBuilder.newBuilder()
                .refreshAfterWrite(2, TimeUnit.SECONDS)
                .build(new CacheLoader<String, String>(

                ) {
                    @Override
                    public String load(String arg0) throws Exception {
                        // TODO Auto-generated method stub
                        return addcache(arg0);
                    }
                   
                    @Override
                    public Map<String, String> loadAll(Iterable<? extends String> keys) throws Exception {
                        System.out.println("inside load all");
                        return addcacheAll(keys);
                    }

                });
    }

    private String addcache(String arg0) {
        System.out.println("adding cache");
        return arg0.toUpperCase();
    }
   
    private Map<String, String> addcacheAll(Iterable<? extends String> keys) {
        Map<String, String> map = new HashMap<String, String>();
        for(String s: keys){
            map.put(s, s.toUpperCase());
        }
        return map;
    }
   
    public String getEntry(String args) throws ExecutionException{
        System.out.println(cache.size());
        return cache.get(args);
    }
   
    public ImmutableMap<String, String> getEntryAll(String args) throws ExecutionException{
        List<String> list = new ArrayList<String>();
        list.add(args);
        return cache.getAll(list);
    }
   
    public static void main(String[] args) {
        GuavaTest gt = GuavaTest.getInstance();
        try {
            System.out.println(gt.getEntry("Suvendu"));
            System.out.println(gt.getEntry("Suvendu"));
            Thread.sleep(2100);
            System.out.println(gt.getEntry("Suvendu"));
            System.out.println(gt.getEntry("Suvendu"));
           
            System.out.println(gt.getEntryAll("Suvendu1"));
            System.out.println(gt.getEntry("Suvendu1"));
           
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}