执行程序服务线程不是异步执行方法

时间:2017-04-06 20:58:16

标签: java

我使用newCachedThreadPool实现了执行器服务。 我创建了几个端点,每个端点都发送响应并异步调用另一个方法。 这是我的代码: @RequestMapping( “/通知”) 公共类NotificationController {

private static final Logger logger = LoggerFactory.getLogger(NotificationController.class);

ExecutorService executor = Executors.newCachedThreadPool();

ObjectMapper jackson = new ObjectMapper();

@Autowired
UserPartnerRepository userPartnerRepo;


@Value("${oauth.consumerkey}")
private String consumerkey;

@Value("${oauth.consumerSecret}")
private String consumerSecret;


@RequestMapping(value = "/summaryData1", method = RequestMethod.POST)
public ResponseEntity<String> handleSummaryDataNotification(@RequestBody @Valid DailySummary dailySummary)

{
    if (dailySummary != null && dailySummary.getDailies() != null && dailySummary.getDailies().size() > 0)
    {
        Runnable r1 = () -> {

            for(PingNotification daily:dailySummary.getDailies())
            {

                logger.info("calling rest api to get dailySummary data");
                String dailiesInfo = retrieveSummaryDataByUploadTime(Consts.DAILIES, daily
                    .getUserAccessToken(), daily.getCallBackUrl());
                logger.info("dailySummaryinfo for userAccessToken:{}:-{}", daily
                        .getUserAccessToken(),dailiesInfo);
            }
        };
        executor.execute(r1);
        logger.info("sending acknowledgement - 200  for dailies ping notification service");
        return new ResponseEntity<String>(HttpStatus.OK);
    }
    else
    {
        logger.info("sending acknowledgement - 204 for dailies ping notification service");
        return new ResponseEntity<String>(HttpStatus.NO_CONTENT);
    }
}



@RequestMapping(value = "/activities", method = RequestMethod.POST)
public ResponseEntity<String> handleActivityPingNotification(@RequestBody @Valid Activities activities)
{

    if (activities != null && activities.getActivities() != null && activities.getActivities().size() > 0)
    {
        Runnable r1 = () -> {
            for(PingNotification activity:activities.getActivities())
            {

                logger.info("calling rest api to upload activities data");
                String activityInfo = retrieveSummaryDataByUploadTime(Consts.ACTIVITIES, activity.getUserAccessToken(), activity.getCallBackUrl());

                logger.info("activity info for userAccessToken:{}:-{}", activity
                        .getUserAccessToken(), activityInfo);

            }
        };
        executor.execute(r1);
        logger.info("sending acknowledgement - 200 for activities ping notification service");
        return new ResponseEntity<String>(HttpStatus.OK);
    }
    else
    {
        logger.info("sending acknowledgement - 204  for activities ping notification service");
        return new ResponseEntity<String>(HttpStatus.NO_CONTENT);
    }
}


@RequestMapping(value = "/sleeps", method = RequestMethod.POST)
public ResponseEntity<String> handleSleepsPingNotification(@RequestBody @Valid Sleeps sleeps)
{
    logger.info("Sleeps ping notification");
    if (sleeps != null && sleeps.getSleeps() != null && sleeps.getSleeps().size() > 0)
    {
        Runnable r1 = () -> {
            for(PingNotification sleep:sleeps.getSleeps())
            {
                String sleepsInfo = retrieveSummaryDataByUploadTime(Consts.SLEEPS, sleep
                    .getUserAccessToken(), sleep.getCallBackUrl());
                logger.info("sleeps info for userAccessToken:{}:-{}", sleep
                        .getUserAccessToken(), sleepsInfo);
            }
        };
        executor.execute(r1);
        logger.info("sending acknowledgement - 200 for sleeps ping notification service");
        return new ResponseEntity<String>( HttpStatus.OK);
    }
    else
    {
        logger.info("sending acknowledgement - 204 for sleeps ping notification service");
        return new ResponseEntity<String>(HttpStatus.NO_CONTENT);
    }
}



/*
 * calling Rest APIs to get Summary data for each Summary Domain.
 */
public String retrieveSummaryDataByUploadTime(String summaryName, String userAccessToken, String callBackUrl)
{

    logger.info("value in url is:" + callBackUrl);
    DefaultOAuthConsumer consumer = new DefaultOAuthConsumer(consumerkey, consumerSecret);
    // Retrieve the useraccesstokensecret for a useraccesstoken from the database.
    consumer.setTokenWithSecret(userAccessToken, userPartnerRepo.getUserAccessSecret(userAccessToken));
    StringBuffer response = new StringBuffer();
    String summaryInfo = null;
    try
    {
        URL urlObj = new URL(callBackUrl);
        HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
        // add request header
        logger.info("calling signpost");
        consumer.sign(con);
        con.setRequestMethod("GET");
        int responseCode = con.getResponseCode();
        logger.info("value in responseCode is for{} summary " + summaryName + responseCode);
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null)
        {
            response.append(inputLine);

        }
        long timeStamp = System.currentTimeMillis();
         summaryInfo = prettyPrint(response.toString());
        in.close();
        con.disconnect();
    }

   catch (Exception e)
    {
        logger.error("failed to get summary data:" + e.getMessage());
    }
    return summaryInfo;

}

}

当我发送3个通知时,有三个方法正在发送acknowldegements,然后调用retrieveSummaryDataByUploadTime方法,但它没有完全执行此方法。当我看到日志时它显示“调用路标”之后线程不活动。看起来像以前一样打电话或在那个调用线程正在杀死。什么改变使方法完全执行?

0 个答案:

没有答案