search

Home  >  Q&A  >  body text

java - spring-data-mongo的mongoTemplate在spring的taskExecutor中异步执行异常

测试程序如下:

@Test
    public void taskTest() {
        System.out.println(Thread.currentThread());
        List<User> list1 = mongoTemplate.findAll(User.class);
        System.out.println(JSON.toJSONString(list1));
        taskExecutor.execute(() -> {
            System.out.println(Thread.currentThread());
            List<User> list = mongoTemplate.findAll(User.class);
            System.out.println(JSON.toJSONString(list));
        });
    }

执行结果:

2016-09-18 18:00:09.094  INFO 5044 --- [.mlab.com:47975] org.mongodb.driver.cluster               : Canonical address ds147975-a.mlab.com:47975 does not match server address.  Removing ds147975.mlab.com:47975 from client view of cluster
Thread[main,5,main]
2016-09-18 18:00:09.253  INFO 5044 --- [           main] org.mongodb.driver.cluster               : No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, all=[ServerDescription{address=ds147975-a.mlab.com:47975, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
2016-09-18 18:00:10.952  INFO 5044 --- [.mlab.com:47975] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:2, serverValue:224406}] to ds147975-a.mlab.com:47975
2016-09-18 18:00:11.184  INFO 5044 --- [.mlab.com:47975] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=ds147975-a.mlab.com:47975, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 0, 12]}, minWireVersion=0, maxWireVersion=3, maxDocumentSize=16777216, roundTripTimeNanos=231838710, setName='rs-ds147975', canonicalAddress=ds147975-a.mlab.com:47975, hosts=[ds147975-a.mlab.com:47975], passives=[], arbiters=[], primary='ds147975-a.mlab.com:47975', tagSet=TagSet{[]}, electionId=57a5d2f95684dd7f4089cb4b, setVersion=1}
2016-09-18 18:00:11.184  INFO 5044 --- [.mlab.com:47975] org.mongodb.driver.cluster               : Setting max election id to 57a5d2f95684dd7f4089cb4b from replica set primary ds147975-a.mlab.com:47975
2016-09-18 18:00:11.184  INFO 5044 --- [.mlab.com:47975] org.mongodb.driver.cluster               : Setting max set version to 1 from replica set primary ds147975-a.mlab.com:47975
2016-09-18 18:00:11.184  INFO 5044 --- [.mlab.com:47975] org.mongodb.driver.cluster               : Discovered replica set primary ds147975-a.mlab.com:47975
2016-09-18 18:00:12.796  INFO 5044 --- [           main] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:3, serverValue:224407}] to ds147975-a.mlab.com:47975
[{"boss":0,"detail":{"frontPhoto":"1232","gender":1,"nickname":"涵小涵","residence":"四川成都","reversalPhoto":false,"signature":"","worth":1000},"goldRepo":{"coin":0,"createTime":1473768462677,"updateTime":1473768462677,"updateUser":"系统","version":0},"hasBindMobile":false,"hasBindQQ":true,"hasBindWechat":false,"hasBindWeibo":false,"id":"57d7ec11097a8c464bcaadd7","lastActiveTime":1473768462677,"mapper":[{"canExpire":true,"expireTime":1476775577008,"mapperTime":1473768462677,"openId":"1234","type":2},{"canExpire":true,"expireTime":1476615519735,"mapperTime":1473768462677,"openId":"12234","type":1}],"registerTime":1473768462677,"statistic":{"currentSlaveCount":0,"maxDiscoverCount":10,"maxSlaveCount":10,"totalSellCount":0,"totalSoldCount":0},"status":true,"token":"008bb62b25cc4b69b7346fa0b849be2f","userId":1}]
Thread[taskExecutor-1,5,main]
2016-09-18 18:00:13.132  INFO 5044 --- [       Thread-3] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@480d3575: startup date [Sun Sep 18 18:00:00 CST 2016]; root of context hierarchy
2016-09-18 18:00:13.135  INFO 5044 --- [       Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'taskExecutor'

结果说明:

主线程执行时,mongoTemplate可以正常的查询出user的内容,但是在taskExecutor开启的子线程中,仅仅输出了当前的线程名,查询及后续输出操作没有完成就结束了线程,而当注释掉mongoTemplate操作部分,线程能正常走完。有木有同学遇到过这种情况,是什么原因呢?

相关库的版本:

compile 'org.springframework.boot:spring-boot-starter-data-mongodb:1.4.0.RELEASE'
compile 'org.mongodb:mongo-java-driver:3.2.2'
怪我咯怪我咯2807 days ago1257

reply all(0)I'll reply

No reply
  • Cancelreply