Spring Java project not starting Tomcat when importing jar with infinite loop

AnonymousAlias :

I have a java app that is just takes a jar from another project, the jar is using an infinite loop to poll for messages on AWS SQS topic.

The loop looks for methods annotated with @subscriber in my client application.

I have a bean that will return a class which has a method annotated. When I remove the bean that is calling a class with an annotation method my tomcat is started on a port. When i add it in again there is no port hosted.

In the pom file I have these 2 dependencies added (along with some others but these should be the relevant ones)

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

My main class looks like this

@SpringBootApplication
@ComponentScan(basePackages = {"com.mypackage.reuse", "com.mypackage.sample.subscriber"})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

The logs I get when I start my app

2020-03-12 14:32:56.228  INFO 2429 --- [           main] c.l.g.e.f.sample.subscriber.Application  : The following profiles are active: development
2020-03-12 14:32:57.580  INFO 2429 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 1339 (http)
2020-03-12 14:32:57.586  INFO 2429 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-03-12 14:32:57.587  INFO 2429 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.29]
2020-03-12 14:32:57.650  INFO 2429 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-03-12 14:32:57.651  INFO 2429 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1363 ms
2020-03-12 14:32:58.038  INFO 2429 --- [           main] org.reflections.Reflections              : Reflections took 111 ms to scan 2 urls, producing 23 keys and 53 values 

there is no log after that I would expect something like this which is what I get if I remove the loop. When i remove the loop i get something like below but the problem is i need the loop to continuously poll.

Exposing 2 endpoint(s) beneath base path '/actuator'
Tomcat started on port(s): 1339 (http) with context path ''
Mark Bramnik :

Since there is no error,

Spring boot probably does something, and it might take time... When the initialization of the application context is done spring boot finally writes "tomcat started on port 1339 message"

I don't know what exactly happens when "reflections API to find methods annotated with @subscirber" but if implemented inefficiently - this can definitely take some time.

So I suggest taking a thread dump and trying to analyze the running threads when you reach the point in log where you feel that the application gets stuck.

Another suspicious point is what you wrote:

When I remove the bean that is calling a class with an annotation method my tomcat is started on a port.

What is this bean exactly? what is the class with an annotation method? Does it try to connect to AWS? If so what if the definitions are wrong and it gets stuck trying to connect? Again the stack trace will clarify a lot of things here.

Update

Threas dump (you should read it from the bottom to the top - it shows the current calls execution chsin) shows that in the SubscriberFactory's constructor you're trying to connect to SQS of Aws and it gets stuck...

This is abnormal situation. Probably you did something wrong with connection parameters.

From the point of view of spring initialization it stucks the whole initialization process, because spring creates the beans one by one in one thread

Anyway, proper connection to sqs will probably resolve the issue.

Update 2

Answering the question of running the bean in different thread.

You dont run the bean but rather the method in the bean.

It should not be a constructor or method annotated with @PostConstruct because these are methods called by spring during the initialization

You can use @Async method in listener that will run when the application is ready.

You can read about asynchronous methods here, in this tutorial (note, you also need @EnableAsync)

The method should be called/implemented im the listener and be public void

The concept of listeners is easy to grasp - these are hooks to application context initialization lifecycle. You'll need to follow thos thread

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=238753&siteId=1