在使用@Async时遇到线程问题

时间:2014-12-03 07:22:01

标签: multithreading spring asynchronous

我在webservice中有一个要求,一个数据库调用会更新表中的一些信息。 并期望在接下来的几秒钟内,需要支持将近20,000个客户端来支持这个Web服务。

为此,我在Spring 3中使用了@Async注释,以便在服务调用和下面的配置中实现此目的。

<task:annotation-driven executor="taskExecutor"/>     
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean">
      <property name="corePoolSize" value="5000" />
      <property name="maxPoolSize" value="10000" />
      <property name="keepAliveSeconds" value="5000" />
</bean>

直到2000个客户端才能使用,而不是超过4GB RAM容量测试机器。

尝试以下选项..但没有运气

<task:annotation-driven executor="taskExecutor"/>     
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean">
      <property name="corePoolSize" value="3000" />
      <property name="maxPoolSize" value="15000" />
      <property name="queueCapacity" value="9000" />
      <!-- <property name="keepAliveSeconds" value="5000" /> -->
      <property name="waitForTasksToCompleteOnShutdown" value="true"/>
</bean>

需要您帮助解决此问题。

根据要求粘贴服务类详细信息:

@Autowired
MongoTemplate mongoTemplate;

@Override
@Async
public void processAndSave(String tLine) {
    ...
    ...
    while (st.hasMoreTokens()) {
        try {
            String token =st.nextToken().trim();
            map = StringUtil.fetchMapFrom(token);
            if (isNotEmptyMap(map) && (map.size() == InfoHeaders.values().length)) {
                tList.add(prepareTInfo(map));
            } else {
                tErrorList.add(prepareTErrorInfo(token,map));
            }
        } catch (Exception e) {
            LOGGER.error("processAndSave:Unable to process and save", e);
        }
    }

    if(tList!=null && tList.size()>0){
        mongoTemplate.insert(tList,TInfo.class);
    }

    if(tErrorList!=null && tErrorList.size()>0){
        mongoTemplate.insert(tErrorList,TErrorInfo.class);
    }
}


public TInfo prepareTInfo(Map<String, String> tlMap) {
    TInfo tcInfo = null;
    try {
        if (isNotEmptyMap(tlMap)) {
            tcInfo = new TInfo();
            tcInfo.setActivityId(tlMap.get(String.valueOf(InfoHeaders.A)));
            tcInfo.setBattVal(tlMap.get(String.valueOf(InfoHeaders.B)));
            tcInfo.setLocVal(tlMap.get(String.valueOf(InfoHeaders.L)));
            tcInfo.setDateTime(tlMap.get(String.valueOf(InfoHeaders.T)));
            tcInfo.setTId(tlMap.get(String.valueOf(InfoHeaders.ID)));
            tcInfo.setTempVal(tlMap.get(String.valueOf(InfoHeaders.D)));
            tcInfo.setCreatedDate(new Date());
            tcInfo.setIsDataPushed(0);
        }
    } catch (Exception e) {
        LOGGER.error(e);
    }
    return tcInfo;
}

public TErrorInfo prepareTErrorInfo(String errorTLine,Map<String, String> map) {
    TErrorInfo tceInfo = null;
    try {
        tceInfo = new TErrorInfo();
        tceInfo.setErrorTMsg(errorTLine);
        tceInfo.setCreatedDate(new Date());
        tceInfo.setIsDataPushed(0);
        tceInfo.setTId(map.get(String.valueOf(InfoHeaders.ID)));
    } catch (Exception e) {
        LOGGER.error(e);
    }
    return tceInfo;
}

使用mongoTemplate将数据插入到数据库中。

请进一步建议我。

0 个答案:

没有答案