Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Java
@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);
				
				//Log process take around one hour
				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
Java
@Service
public class ProcessServiceImpl implements ProcessService {

	//Oracle DB connection
	private dataLayer dbLayer 						= null;
	
	//Setting the parameters
	private  ProcessParameter inputParam				= null;
	
	//Storing output 
	private LinkedHashMap<String, Object> outputResult 		= null;
	
	//storing Log details
	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;
		
			// Initialize the dabaLayer object
			dbLayer = new dataLayer(param.getServer_name(), param.getDatasource(), param.getUsername(),param.getPassword());
					
		
			if (!dbLayer.openConnection())
				throw new Exception("Unable to open oracle database connection ");

			//Here do the process 
			
			//Loop through the job details and get the details from oracle db
		
			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"
     }
Posted
Comments
Afzaal Ahmad Zeeshan 3-Aug-19 9:26am    
Interesting, perhaps the problems is that a new job overrides the previous one.
leoiser 3-Aug-19 11:25am    
Yes its something like that but some cases both job failed after sometime because there is a condition based on the job could not find in DB. Anything wrong in my code?
wseng 4-Aug-19 12:36pm    
Have you tried using postman ?
leoiser 5-Aug-19 7:32am    
Yes use postman only

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900