Я пришел к следующему решению на основе вышеуказанных предложений и spring.io
Public Async Method в своем собственном классе
@Service
public class CallAggrAsync
{
@Async
public void fireandforgetrag()
{
try
{
URL myurl = new URL("http://myurl.com");
URLConnection myconn = myurl.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
myconn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
catch(Exception e)
{
System.out.println("Error firing or forgetting: "+e);
}
}
}
Application.java, где мы включаем EnableAsync
@SpringBootApplication
@EnableAsync
public class Application
{
public static void main(final String args[])
{
SpringApplication.run(Application.class);
}
@Bean
public Executor asyncExecutor()
{
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("Async-");
executor.initialize();
return executor;
}
}
Я вызываю вышеупомянутый асинхронный метод
@Service
public class Persist
{
@Autowired private CallAggrAsync asy;
public boolean somefunc()
{
//do some work
asy.fireandforgetrag("server",hostname);
//do some more work which will continue running without waiting for above
function to finish
}
}