I am logging entries into a website form using Spring4 and Java 8 (originally Java 7). I was using the slf4j logging framework firstly to write to file as a PoC and then directly to db as a mistaken final implementation. Mistaken as it shouldn't talk to the db directly. So I now have a very simple REST API server running with a couple of POST end points. So far so good. The basic code is working with no problems.
As this is non core functionality (logging NOT auditing) I thought I should make this call asynchronous so that if there are any issues this peripheral functionality won't degrade core functionality so I thought I should make the call asynchronously.
I have a created a Service class which is injected into the controller classes and has methods like this:
public void postLog (EntryItemLog logItem){
String uri = REST_API_ENDPOINT;
try {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<EntryItemLog> response = restTemplate.postForEntity(uri, logItem, EntryItemLog class);
LOGGER.debug("Returned logging POST with status " + response.getStatusCode());
}catch (Exception ex) {
LOGGER.debug("Error Calling " + uri + ": " + ex.getMessage());
}
}
and I am calling the function in this way:
@RequestMapping(value = Controller.RESOURCE_NAME, method = RequestMethod.GET)
public ModelAndView controlMethod(HttpServletRequest request, HttpServletResponse response,
@ModelAttribute(REQUEST_MODEL_NAME) RequestBean requestBean, Model model) {
//some logging & set up
new Thread(() -> {
dbRESTClientService.postLog(searchRequestBean.getTerm(), "searchProject", "WEB", (request.getHeader("X-FORWARDED-FOR") == null) ? request.getRemoteAddr() : request.getHeader("X-FORWARDED-FOR"));
}).start();
//perform main functionality
I imagine this is very basic and unsophisticated for many people on this site, but is it broadly acceptable? Can anyone offer me any advice on doing it in a better way?
I guess I have 3 queries myself but any other (broadly constructive) criticism would gratefully accepted as the is a fairly new area to me:
Starting a new thread seems like quite an overhead for this task but I don't see an alternative (other than using a 3rd party tool like Jersey which will presumably do the same thing anyway?)
Should I be doing something more sophisticated than creating a local RestTemplate each time?
Is it generally the right approach anyway (it feels over simplistic)?