Guava Futures와 관련 API


http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/ListeningExecutorService.html


http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/Futures.html


http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/FutureCallback.html


Guava Futures는 Java Future에 비해서

Callback Listener를 사용할 수 있고, 성공/실패 여부를 받을 수 있다. 

(Mina나 Netty를 사용하는 장점을 알 수 있다.)




import java.util.Random;
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 org.junit.Test;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;

public class GuavaTest {

@Test
public void test_ExecutorService() {
System.
out.println("====java future===");
ExecutorService
executor = Executors.newFixedThreadPool(10);

Callable<String>
asyncTask = new Callable<String>() {
@Override
public String call() throws Exception {
Random
random = new Random();
int number = random.nextInt(5);
return "async : " + number;
}
};

Future<String>
future = executor.submit(asyncTask);

try {
String
result = future.get();
System.
out.println(result);
}
catch (ExecutionException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}

executor.shutdown();
}

@Test
public void test_guava_future() {
System.
out.println("===guava future===");

ListeningExecutorService
executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));

Callable<String>
asyncTask = new Callable<String>() {
@Override
public String call() throws Exception {
Random
random = new Random();
int number = random.nextInt(5);
return "async : " + number;
}
};

ListenableFuture<String>
listenableFuture = executor.submit(asyncTask);

Futures.addCallback(
listenableFuture, new FutureCallback<String>() {
@Override
public void onSuccess(String result) {
System.
out.println("[success] : " + result);
}

@Override
public void onFailure(Throwable thrown) {
System.
out.println("[success] : " + thrown.getMessage());
}
});

try {
String
result = listenableFuture.get();
System.
out.println(result);
}
catch (ExecutionException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}

executor.shutdown();
}
}

결과

===guava future===

[success] : async : 1

async : 1

====java future===

async : 3



Futures 대신 ListenableFuture.addListener()를 활용할 수도 있다. 



@Test
public void test_guava_future_listener() {
System.
out.println("===guava future listener===");

ListeningExecutorService
executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));

Callable<String>
asyncTask = new Callable<String>() {
@Override
public String call() throws Exception {
Random
random = new Random();
int number = random.nextInt(5);
return "async : " + number;
}
};

final ListenableFuture<String> listenableFuture = executor.submit(asyncTask);

listenableFuture.addListener(new Runnable() {
     
@Override
     
public void run() {
         
try {
             
final String contents = listenableFuture.get();
              System.
out.println(contents);
          }
catch (InterruptedException e) {
             
e.printStackTrace();
          }
catch (ExecutionException e) {
         
e.printStackTrace();
          }
      }
  },
executor);

executor.shutdown();

}



참조할만한 소스 

https://gist.github.com/bbejeck/1387892

'general java' 카테고리의 다른 글

[Guava] EventBus 예제  (0) 2015.04.23
[Guava] Monitor 예제  (0) 2015.04.21
[Guava] Futures 예제  (0) 2015.04.17
[Guava] Preconditions 예제  (0) 2015.04.16
[Guava] CacheBuilder, CachLoader 예제  (0) 2015.04.15
[Guava] Collections2.filter  (0) 2015.04.14
Posted by '김용환'

댓글을 달아 주세요