В чем разница, открывающая многопоточность между сервисом и контроллером? - PullRequest
0 голосов
/ 18 декабря 2018

В чем разница открытия многопоточности между сервисом и контроллером?Когда я открываю multiThreading в контроллере, мне не нужно открывать сеанс гибернации, и я могу получить currentSession.Но когда я открываю поток в сервисе, я должен сначала получить hibernate sessionFactory, а затем открыть новый сеанс.И в конце я должен закрыть сессию.Пожалуйста, помогите или попробуйте дать некоторые идеи, как этого добиться.

@Controller

public class TestController extends BaseController {

    @Autowired
    private TestService testService;

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    private TestDao testDao;

    @ResponseBody
    @RequestMapping("/testMultiInService")
    public Object test() {
        GenericListResponse response = testService.testMulti();
        return response;
    }
}

@Service

public class TestServiceImpl extends TestService {

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    private TestDao testDao;

    private static Logger logger = Logger.getLogger(TestServiceImpl.class);

    @Override
    @Transactional
    public GenericListResponse testMulti() {
        CountDownLatch latch = new CountDownLatch(2);

        TestCallable1 callable1 = new TestCallable1(this, latch);
        Future<List> future1 = taskExecutor.submit(callable1);
        List list = new ArrayList();

        try {
            latch.await();
            if (null != future1)
                list.addAll(future1.get());
            if (null != future2)
                list.addAll(future2.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class TestCallable1 implements Callable<List> {

    private TestService testService;

    private CountDownLatch latch;

    public TestCallable1(TestService testService, CountDownLatch latch) {
        this.testService = testService;
        this.latch = latch;
    }

    @Override
    public List call() throws Exception {
        List list = null;
        try {
            list = testService.getDimArea();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.latch.countDown();
            return list;
        }
    }


}
...