0

My requirement is to create the multiple threads and execute the query and give the final output like Map<String,List<Object>>;

Map contains table name string and List<Object> is the query output that contains list of tables record.

The requirement:

  • I have one table that contains the list of field like TableName and Query

    Eg.

    employ | select * from employ; that query have more than 100000 record employ_detail| select * from employ_detail; that query have more than 300000 record employ_salary| select * from employ_salary; that query have more than 600000 record

Above table may have 10 000 queries

I want to create one API for that above query using the spring boot + hibernate.

My problem:

I want to create one solution with multiple threading using JAVA 8.

 @RestController
 public class ApiQueries {
   @RequestMapping(value = "/getAllQueries", method = RequestMethod.GET)     

     public CommonDTO getAllQuery(){

          list=apiQueryService.findAll();
          if(null!=list){
             objectMap= apiQueryService.executeQueryData(list);   //here apiQueryService have one method named is executeQuery()
          }
     } 
 }

I wrote the below logic in that method.

@Override
public Map<String,List<Object>> executeQueryData(List<ApiQueries> 
apiQuerylist, String fromDate, String toDate) {
    addExecutor = new ThreadPoolExecutor(3, 5, 10, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
    List<Object> obj=null;

    Map<String,List<Object>> returnMap=new HashMap<String,List<Object>>();
        try {

            if(session==null) {
                session = sessionFactory.openSession();
            }
            apiQuerylist.forEach(list-> addExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    apiQueryObject = session.createSQLQuery(list.getQuery()).list();    
                    returnMap.put(list.getTableName(), apiQueryObject);
                }

            }));
        }catch(Exception ex) {
            System.out.println("Inside [B] Exception "+ex);
            ex.printStackTrace();
        }finally {
            if(session !=null) {
                session.close();
            }
        }   
        return returnMap;
}

Issue is when i call that api the below code will run in background and that method is return the null object, But in background i will see the list of queries which executes one by one

apiQuerylist.forEach(list-> addExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    apiQueryObject = session.createSQLQuery(list.getQuery()).list();    
                    returnMap.put(list.getTableName(), apiQueryObject);
                }

            }));
2
  • Thanks and i will try with ConcurrentHashMap. no creatSqlQuery will not return the null value actually problem is when i start the debuging that and cursor is on that line apiQuerylist.forEach(list-> addExecutor.execute(new Runnable() and it will execute in background and cursor will transfer to foreach loop and thats why it return the null map. Commented Oct 25, 2019 at 9:46
  • not resolve, i will try with ConcurrentHashMap but issue is still persist Commented Oct 25, 2019 at 9:54

1 Answer 1

1

You need to wait for thread pool completion. Something like below after apiQuerylist.forEach should work:

addExecutor.shutdown();
// waiting for executors to finish their jobs
while (!addExecutor.awaitTermination(50, TimeUnit.MILLISECONDS));
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.