¿RestTemplate debería ser estático globalmente declarado?
Estoy usando Java Callable Future en mi código. Abajo está mi código principal que usa el futuro y se puede llamar:
public class TimeoutThread {
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(5);
Future<String> future = executor.submit(new Task());
try {
System.out.println("Started..");
System.out.println(future.get(3, TimeUnit.SECONDS));
System.out.println("Finished!");
} catch (TimeoutException e) {
System.out.println("Terminated!");
}
executor.shutdownNow();
}
}
Abajo esta miTask
clase que implementa la interfaz invocable y necesito generar una URL según el nombre de host que tengamos y luego realizar una llamada a los SERVIDORES usandoRestTemplate
. Si hay alguna excepción en el primer nombre de host, generaré una URL para otro nombre de host e intentaré hacer una llamada.
class Task implements Callable<String> {
private static RestTemplate restTemplate = new RestTemplate();
@Override
public String call() throws Exception {
//.. some code
for(String hostname : hostnames) {
if(hostname == null) {
continue;
}
try {
String url = generateURL(hostname);
response = restTemplate.getForObject(url, String.class);
// make a response and then break
break;
} catch (Exception ex) {
ex.printStackTrace(); // use logger
}
}
}
}
Así que mi pregunta debería declararRestTemplate
como variable global estática? ¿O no debería ser estático en este escenario?