0

我正在尝试使用 executor 对象使用 jdbcTemplate 执行查询,但由于某种原因,该程序没有进入 jdbcTemplate 内部。

        ExecutorService executor = Executors.newFixedThreadPool(NUMBER_OF_CONCURRENT_THREADS);
        executor.execute(new Runnable() {
        @Override
        public void run() {
            inboundJdbcTemplate.query(selectQuery, new RowCallbackHandler() {
                @Override
                public void processRow(ResultSet rs) throws SQLException {//<-instruction pointer never goes to this line
                    try {
                        //buffer.put(buildDataPoint(rs, testPermutationId));
                        System.out.println(rs.getString(0));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        Thread.currentThread().interrupt();
                    }
                }
            });
            try {
                buffer.put(STOPPING_TOKEN);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

谁能帮我解决这个愚蠢的错误?

4

1 回答 1

0

我找到了解决问题的方法。

我需要一个 CompletionService 以确保我知道 JdbcTemplate 的执行何时完成。

{...
 ExecutorService executor = Executors.newFixedThreadPool(NUMBER_OF_CONCURRENT_THREADS);
 CompletionService<String> completionService = new ExecutorCompletionService (executor);

 completionService.submit(new Runnable() {
    @Override
    public void run() {
        inboundJdbcTemplate.query(selectQuery, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                try {
                    buffer.put(buildDP(rs, Id));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
 }, "Success");

 try{
      Future<String> take1 = completionService.take();
      String s = take1.get();
      if(!"Success".equals(s)) throw new RuntimeException("Error Occured");
 catch (InterruptedException | ExecutionException e) {
        LOG.error(" Could not execute DataExtraction",e);}
 executor.shutdown();
...}
于 2013-05-17T15:04:02.847 回答