Hi, I am new to springboot. I created an API which run some process that takes around one hour and return the result. The problem is if I submit two process parallel, I am getting the same output. Pls check the below code and give me suggestions
Looks like ProcessService working on a same instance of the class because I am the last submitted output for both cases. What is wrong in this code? Any solution. pls advise
What I have tried:
--CONTROLLER
@RestController
@RequestMapping("/task")
public class ProcessController {
@Autowired
TaskRepository taskRepository;
@Autowired
ProcessService processService;
@RequestMapping(value = "/process", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
public Map<String, Object> process(@RequestBody String requestBody) throws Exception {
Map<String, Object> result = new LinkedHashMap<String, Object>();
long taskId=0;
try {
ParameterClass param =null ;
JSONObject requestJson = new JSONObject(requestBody);
taskId = Long.valueOf(requestJson.getString("taskid"));
List<Task> lst = taskRepository.findByTaskId(taskId);
Task task = lst.stream().filter(p -> p.getParameterName().equals("process_task"))
.findFirst().orElse(null);
if (task != null) {
String parameterArgs = task.getParameterArg();
param = setParameters(parameterArgs);
result = processService.ProcessJob(param);
}
} catch (Exception ex) {
result.put("taskid", taskId);
result.put("status", "failed");
result.put("message", ex.getMessage());
ex.printStackTrace();
} finally {
return result;
}
}
}
--SERVICE
@Service
public class ProcessServiceImpl implements ProcessService {
private dataLayer dbLayer = null;
private ProcessParameter inputParam = null;
private LinkedHashMap<String, Object> outputResult = null;
private StringBuilder sbLog = null;
@Override
public Map<String, Object> ProcessJob(ProcessParameter param) throws Exception {
try
{
outputResult.put("jobid", param.getTask_id());
outputResult.put("startdate", new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a").format(System.currentTimeMillis()));
sbLog.append("taskid= " + param.getTask_id() + ")\n");
inputParam = param;
dbLayer = new dataLayer(param.getServer_name(), param.getDatasource(), param.getUsername(),param.getPassword());
if (!dbLayer.openConnection())
throw new Exception("Unable to open oracle database connection ");
sStatus = "success";
}
} catch (Exception ex) {
ex.printStackTrace();
sStatus = "failed";
outputResult.put("message", ex.getMessage());
}
finally
{
outputResult.put("status",sStatus );
sbLog.append("returing output for " + param.getJob_id() + "\n");
outputResult.put("log", sbLog.toString() );
return outputResult;
}
}
}
--INPUT1
{ "taskid"= 201 }
--OUTPUT1
{
"taskid": 260,
"status": "success",
"log" : "taskid=260
returning output for 260"
}
--INPUT2
{ "taskid"= 260 }
--OUTPUT2
{
"taskid": 260,
"status": "success",
"log" : "taskid=260
returning output for 260"
}