Skip to content

Instantly share code, notes, and snippets.

@bsideup
Created April 7, 2015 19:17
Show Gist options
  • Save bsideup/a08c4f0b10dbacde82c9 to your computer and use it in GitHub Desktop.
Save bsideup/a08c4f0b10dbacde82c9 to your computer and use it in GitHub Desktop.
import com.example.calculator.protocol.TOperation;
+import com.google.common.util.concurrent.*;
import org.springframework.stereotype.Component;
import com.example.calculator.service.CalculatorService;
import org.springframework.beans.factory.annotation.Autowired;
+import java.util.concurrent.*;
+
@Component
public class CalculatorServiceHandler implements TCalculatorService {
@Autowired
CalculatorService calculatorService;
+ ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
+
@Override
- public int calculate(int num1, int num2, TOperation op) throws TDivisionByZeroException {
+ public ListenableFuture<Integer> calculate(int num1, int num2, TOperation op) throws TDivisionByZeroException {
+ return JdkFutureAdapters.listenInPoolThread(
+ scheduledExecutorService.schedule(() -> {
switch(op) {
case ADD:
return calculatorService.add(num1, num2);
@@ -32,5 +40,7 @@
default:
throw new IllegalArgumentException("Unknown operation " + op);
}
+ }, 2, TimeUnit.SECONDS)
+ );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment