每个executorservice线程

时间:2018-01-22 11:15:44

标签: java mysql multithreading executorservice

我正在批量提取数据(chunkSize = 1000),因为我已经实现了执行程序服务。在下面的循环中,我正在计算firstResult并发送到executorService以获取数据并插入mongodb

for(int i = 1; i <= numOfBatches; i++){
   int firstResult = (i -1) * chunkSize;
   explicitAudienceCreationExecutorService.fetchFromMartAndInsertIntoMongo(firstResult,chunkSize,query,promotion,
       filterKeywords,audienceFilterName,programId,counttoReturn.get(0).intValue());
 }

这是我的可运行任务,在执行查询时会产生意外结果

  

例如:当我执行没有任何循环的代码并直接将firstResult传递为302000时,它会在日志中打印

     

firstResult 302000 queryResultSize 1000

但是当我在循环中执行此操作时,我在日志中看到了这一点。这种情况发生在几个值上。

  

firstResult 302000 queryResultSize 899

package com.loylty.campaign.com.loylty.campaign.service;


import javax.persistence.TypedQuery;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.loylty.campaign.com.loylty.campaign.config.MongoTenantTemplate.tenantTemplates;

@Service
public class ExplicitAudienceCreationExecutorService {

static int classCount = 0;

@Autowired
CampaignExecutorService campaignExecutorService;

@Autowired
MongoTemplate mongoTemplate;

@Autowired
AudienceWithCriteria audienceWithCriteria;

private final Logger logger = LoggerFactory.getLogger(this.getClass());


public void  fetchFromMartAndInsertIntoMongo(int fr, int cs, TypedQuery<Object[]> qr, Promotion promotion,
                                             FilterKeywords filterKeywords, String audienceFilterName, String programId, int queryrernCont) {

    final int firstResult = fr;
    final int chunkSize = cs;
    final TypedQuery<Object[]> query = qr;
    campaignExecutorService.dotask(new Runnable() {

            @Override
            public void run() {

                mongoTemplate = tenantTemplates.get(programId);
                final List<Object[]> toReturn  = query.setFirstResult(firstResult).setMaxResults(chunkSize).getResultList();
                classCount++;
                System.out.println("classCount "+ classCount);
                logger.info("firstResult "+ firstResult + " queryResultSize " + toReturn.size() );
                if (toReturn != null || toReturn.size() > 0) {
                    List<TGAudience> tgAudienceList = new ArrayList<>();
                    for (Object[] tuple : toReturn) {
                        HashMap<String, Object> queryResponseTuple = new HashMap<>();
                        int index = 0;
                        for (RequiredKeys selectProperty : promotion.getRequiredKeys()) {
                            queryResponseTuple.put(filterKeywords.matcher(selectProperty.getKeyName()).iterator().next(), tuple[index++]);
                        }
                        if (null != promotion.getAggregation() && promotion.getAggregation().size() > 0) {
                            for (Aggregation aggregations : promotion.getAggregation()) {
                                queryResponseTuple.put(filterKeywords.matcher(aggregations.getAggregateOn()).iterator().next() + "_" + aggregations.getAggregateStrategy().name(), tuple[index++]);
                            }
                        }
                        TGAudience tgAudience1 = new TGAudience();
                        String stringToConvert = String.valueOf(queryResponseTuple.get("CUSTOMER_MOBILE"));
                        tgAudience1.setMobile(stringToConvert);
                        tgAudience1.setCustomerId(String.valueOf(queryResponseTuple.get("CUSTOMER_CUSTOMER_ID")));
                        tgAudienceList.add(tgAudience1);
                    }
                    System.out.println("tgAudienceList "+ tgAudienceList.size());
                    mongoTemplate.insert(tgAudienceList, audienceFilterName);
                }
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    );
}

}

CampaignExecutorService

package com.loylty.campaign.com.loylty.campaign.service;

import org.springframework.stereotype.Service;

import java.util.concurrent.*;


@Service
public class CampaignExecutorService {

private ExecutorService executorService = Executors.newFixedThreadPool(100);

public void dotask(Runnable  runnable){
    executorService.submit(runnable);
}
}

0 个答案:

没有答案