Spring Boot + Mongo监听器,不允许tomcat启动

时间:2019-04-22 14:50:20

标签: java mongodb spring-boot

我有一个spring boot应用程序,该应用程序部署在多个节点上。

我想在我的Spring Boot应用程序中编写一个mongo侦听器来执行此操作,因为我监听db事件并突发内存缓存。

问题是当我按如下方式编写侦听器时,tomcat启动根本没有发生,只有事件侦听器启动了:

@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);
    }

}

如果我注释掉@Component注释,那么我的tomcat将正确启动。 如何确保我的tomcat和听众都已启动?

下面是我的tomcat日志,请注意没有错误,它在侦听器启动后不会继续前进

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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

0 个答案:

没有答案