在服务和控制器之间打开multiThreading有什么区别?当我在控制器中打开multiThreading时,不需要打开休眠会话,因此可以获得currentSession
。但是,当我在服务中打开线程时,必须首先获取休眠的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;
}
}
}