Created
April 30, 2019 18:02
-
-
Save acdcjunior/8d92a361868ffa16bfab470bab72689b to your computer and use it in GitHub Desktop.
Spring: Async Parallel Limited by Thread Pool Count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tdd.caixaeletronico; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import java.util.List; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.Future; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
@Service | |
public class BobCaller { | |
@Autowired | |
private BobService bobService; | |
public void goBob() { | |
System.out.println("Invoking ALL asynchronous methods. " + Thread.currentThread().getName()); | |
List<Future<Void>> futures = IntStream.rangeClosed(1, 20).mapToObj((i) -> { | |
System.out.println("Going for " + i); | |
try { | |
return bobService.asyncMethodWithReturnType(i); | |
} catch (InterruptedException e) { | |
throw new RuntimeException(e); | |
} | |
}).collect(Collectors.toList()); | |
futures.forEach((f) -> { | |
try { | |
System.out.println("Done: " + f.get()); | |
} catch (InterruptedException | ExecutionException e) { | |
e.printStackTrace(); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tdd.caixaeletronico; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.scheduling.annotation.Async; | |
import org.springframework.stereotype.Service; | |
import org.springframework.transaction.annotation.Transactional; | |
import tdd.caixaeletronico.negocio.cliente.Cliente; | |
import tdd.caixaeletronico.negocio.cliente.ClienteRepository; | |
import java.util.List; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.concurrent.Future; | |
import java.util.concurrent.atomic.AtomicInteger; | |
@Service | |
public class BobService { | |
@Autowired | |
private ClienteRepository clienteRepository; | |
private AtomicInteger integer = new AtomicInteger(); | |
@Async | |
@Transactional | |
public CompletableFuture<String> go(int arg) { | |
int i = integer.incrementAndGet(); | |
List<Cliente> cs = clienteRepository.findAll(); | |
System.out.println(i + " Got Clients: " + cs.size()); | |
try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();} | |
return CompletableFuture.completedFuture("Arg: " + arg + " -> " + i); | |
} | |
@Async | |
public Future<Void> asyncMethodWithReturnType(int i) throws InterruptedException { | |
System.out.println(i + " has begun!"); | |
int r = marcarComoEmProcessamento(i); | |
try { | |
boolean rr = chamarServicoAssinatura(r); | |
if (rr) { | |
marcarComoSucesso(r); | |
} else { | |
marcarComoErro(r, "Falha: " + r); | |
} | |
} catch (Exception e) { | |
marcarComoErro(r, "Excecao: " + r); | |
} | |
Thread.sleep(5000); | |
System.out.println(i + " is returning now!"); | |
return CompletableFuture.completedFuture(null); | |
} | |
private void marcarComoSucesso(int r) { | |
} | |
private void marcarComoErro(int r, String s) { | |
} | |
private boolean chamarServicoAssinatura(int r) { | |
return false; | |
} | |
private int marcarComoEmProcessamento(int i) { | |
return i; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tdd.caixaeletronico; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.core.task.TaskExecutor; | |
import org.springframework.scheduling.annotation.EnableAsync; | |
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | |
import java.util.concurrent.Executor; | |
@Configuration | |
@EnableAsync | |
public class ThreadConfig { | |
@Bean | |
public TaskExecutor threadPoolTaskExecutor() { | |
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | |
executor.setCorePoolSize(3); | |
executor.setMaxPoolSize(3); | |
executor.setThreadNamePrefix("GO"); | |
executor.initialize(); | |
return executor; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment