Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Site Bugs / Suggestions
  4. Springboot API returns overlapping results for parallel run

Springboot API returns overlapping results for parallel run

Scheduled Pinned Locked Moved Site Bugs / Suggestions
jsondatabaseoraclehelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leoiser
    wrote on last edited by
    #1

    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 --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 process(@RequestBody String requestBody) throws Exception {
    	Map result = new LinkedHashMap();
    	long taskId=0;
    	
    	try {
    
    		ParameterClass param =null ;
    		JSONObject requestJson = new JSONObject(requestBody);
    		taskId = Long.valueOf(requestJson.getString("taskid"));
    		List 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

    @Service
    public class ProcessServiceImpl implements ProcessService {

    //Oracle DB connection
    private dataLayer dbLayer 						= null;
    
    //Setting the parameters
    private  ProcessParameter inputParam				= null;
    
    //Storing output 
    private LinkedHashMap outputResult 		= null;
    
    //storing Log details
    private StringBuilder sbLog						= null;
    
    @Override
    public Map 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 (!dbLaye
    
    W 1 Reply Last reply
    0
    • L leoiser

      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 --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 process(@RequestBody String requestBody) throws Exception {
      	Map result = new LinkedHashMap();
      	long taskId=0;
      	
      	try {
      
      		ParameterClass param =null ;
      		JSONObject requestJson = new JSONObject(requestBody);
      		taskId = Long.valueOf(requestJson.getString("taskid"));
      		List 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

      @Service
      public class ProcessServiceImpl implements ProcessService {

      //Oracle DB connection
      private dataLayer dbLayer 						= null;
      
      //Setting the parameters
      private  ProcessParameter inputParam				= null;
      
      //Storing output 
      private LinkedHashMap outputResult 		= null;
      
      //storing Log details
      private StringBuilder sbLog						= null;
      
      @Override
      public Map 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 (!dbLaye
      
      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      This forum is only for bugs and suggestions related to the site. Try asking the question for example in Q&A: [Ask a Question](https://www.codeproject.com/Questions/ask.aspx)

      L 1 Reply Last reply
      0
      • W Wendelius

        This forum is only for bugs and suggestions related to the site. Try asking the question for example in Q&A: [Ask a Question](https://www.codeproject.com/Questions/ask.aspx)

        L Offline
        L Offline
        leoiser
        wrote on last edited by
        #3

        thanks I deleted and moved to Ask a Question. Thanks

        W 1 Reply Last reply
        0
        • L leoiser

          thanks I deleted and moved to Ask a Question. Thanks

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          :thumbsup: Hope you get the problem solved!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups