Spring boot + Mongo listener , does not allow tomcat to start

吃可爱长大的小学妹 提交于 2021-01-29 11:41:02

问题


I have a spring boot application , where is deployed on multiple nodes.

I want to write a mongo listener inside my spring boot application to do this, as I listen to db events and burst my inmemory cache.

The problem is when I write a listener as below, my tomcat startup does not happen at all, only event listener starts:

@Component
public class MongoListener {


    @PostConstruct
    public void listenProduct() {

        MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
        MongoDatabase database = mongoClient.getDatabase("test");
        MongoCollection<Document> collection = database.getCollection("products");

        Block<ChangeStreamDocument<Document>> printBlock = new Block<ChangeStreamDocument<Document>>() {
            @Override
            public void apply(final ChangeStreamDocument<Document> changeStreamDocument) {
                System.out.println(" primary key is : " + changeStreamDocument.getDocumentKey() + " operation name:"
                        + changeStreamDocument.getOperationType() + "change document" + changeStreamDocument);
            }
        };

        collection
                .watch(Arrays.asList(Aggregates
                        .match(Filters.in("operationType", Arrays.asList("update","delete")))))
                .fullDocument(FullDocument.UPDATE_LOOKUP).forEach(printBlock);
    }

}

If I comment out @Component annotation then my tomcat starts up properly. How do I ensure my tomcat is started as well as my listener?

Below are my tomcat logs, notice there are no errors , it just doesnt move forward after listener starts

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.13.RELEASE)

[2019-04-22T20:35:28:660Z] INFO  --- [main] com.MyProject : Starting MyProject on abclocal with PID 87098 (/Users/abclocal/MyProject/target/classes started by abc in /Users/abclocal/MyProject)
[2019-04-22T20:35:28:662Z] DEBUG --- [main] com.MyProject : Running with Spring Boot v1.5.13.RELEASE, Spring v4.3.17.RELEASE
[2019-04-22T20:35:28:663Z] INFO  --- [main] com.MyProject : The following profiles are active: dev
[2019-04-22T20:35:28:711Z] INFO  --- [main] o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@70b0b186: startup date [Mon Apr 22 20:35:28 IST 2019]; root of context hierarchy
[2019-04-22T20:35:29:518Z] INFO  --- [main] o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cache.annotation.ProxyCachingConfiguration' of type [org.springframework.cache.annotation.ProxyCachingConfiguration$$EnhancerBySpringCGLIB$$2024e1d6] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[2019-04-22T20:35:29:951Z] INFO  --- [main] o.s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
[2019-04-22T20:35:29:976Z] INFO  --- [main] o.a.catalina.core.StandardService : Starting service [Tomcat]
[2019-04-22T20:35:29:977Z] INFO  --- [main] o.a.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.31
[2019-04-22T20:35:30:073Z] INFO  --- [localhost-startStop-1] o.a.c.c.C.[.[localhost].[/v1] : Initializing Spring embedded WebApplicationContext
[2019-04-22T20:35:30:073Z] INFO  --- [localhost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1365 ms
[2019-04-22T20:35:30:230Z] INFO  --- [localhost-startStop-1] o.s.b.w.s.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
[2019-04-22T20:35:30:234Z] INFO  --- [localhost-startStop-1] o.s.b.w.s.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
[2019-04-22T20:35:30:235Z] INFO  --- [localhost-startStop-1] o.s.b.w.s.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
[2019-04-22T20:35:30:235Z] INFO  --- [localhost-startStop-1] o.s.b.w.s.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
[2019-04-22T20:35:30:235Z] INFO  --- [localhost-startStop-1] o.s.b.w.s.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
[2019-04-22T20:35:30:771Z] INFO  --- [main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
[2019-04-22T20:35:30:892Z] INFO  --- [cluster-ClusterId{value='5cbdd83a72f7790491c14c9c', description='null'}-127.0.0.1:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:26}] to 127.0.0.1:27017
[2019-04-22T20:35:30:897Z] INFO  --- [cluster-ClusterId{value='5cbdd83a72f7790491c14c9c', description='null'}-127.0.0.1:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=127.0.0.1:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 6]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=3905086, setName='rs0', canonicalAddress=localhost:27017, hosts=[localhost:27017], passives=[], arbiters=[], primary='localhost:27017', tagSet=TagSet{[]}, electionId=7fffffff0000000000000003, setVersion=1, lastWriteDate=Mon Apr 22 20:35:24 IST 2019, lastUpdateTimeNanos=45738116405786}
[2019-04-22T20:35:31:262Z] INFO  --- [main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
[2019-04-22T20:35:31:268Z] INFO  --- [cluster-ClusterId{value='5cbdd83b72f7790491c14c9d', description='null'}-localhost:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:27}] to localhost:27017
[2019-04-22T20:35:31:270Z] INFO  --- [cluster-ClusterId{value='5cbdd83b72f7790491c14c9d', description='null'}-localhost:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 6]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=1316621, setName='rs0', canonicalAddress=localhost:27017, hosts=[localhost:27017], passives=[], arbiters=[], primary='localhost:27017', tagSet=TagSet{[]}, electionId=7fffffff0000000000000003, setVersion=1, lastWriteDate=Mon Apr 22 20:35:24 IST 2019, lastUpdateTimeNanos=45738489844647}
[2019-04-22T20:35:31:324Z] INFO  --- [main] org.mongodb.driver.connection : Opened connection [connectionId{localValue:3, serverValue:28}] to localhost:27017

来源:https://stackoverflow.com/questions/55796310/spring-boot-mongo-listener-does-not-allow-tomcat-to-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!