skip to Main Content

I have developed a Telegram bot and I wanted it to run in Tomcat environment. I converted my java program into Spring boot project as this.

My conversion is basically adding spring boot dependency to pom,and make main class as spring boot runner. In eclipse environment, my application runs as expected. When I package it into war and deploy it into tomcat, the business logic does not run. Spring boot starts, I can see the logs of it. However telegram bot does not initialized.

I also added a dummy RestController which return “hello”. When I run localhost:8080/endpoint I can see “hello”. However, Telegram bot is not active.

In some resources, they say telegrambots-spring-boot-starter should be in the project. My telegram dependency contains it. It is not the case. I do not get any error log in catalina.out. Application just starts and stays as it is. I dont understand why.

Any help is appreciated.

2

Answers


  1. It’s because you initialize your bot inside the main method of IftarVaktiApp. It doesn’t get called anymore because you’re not running IftarVaktiApp in it’s main method. You should use an EventListener or just implement a CommandLineRunner. see this EventListener or this CommandLineRunner and initialize your bot there.

    Login or Signup to reply.
  2. Working with Spring Boot as (1) an enterprise application on a Tomcat server, deployed through a .WAR is different than (2) running Java code inside your own JVM machine. This means, a main method is not started magically when you deploy the .WAR on the Tomcat server. Starting a Spring Boot application through a Tomcat webserver works differently, and you should refactor the entire main method according to Spring Boot project guidelines to execute the code from the Tomcat environment.

    I have cloned your project from GitHub and I can see this is not a real Spring Initializr project. If you want to run your code through Spring on Tomcat, I advise the following steps:

    1. Create a Spring Initializr project through https://start.spring.io/ and…
    2. …make sure you add the following dependencies to have access to full features if needed: spring-boot-starter-web, spring-boot-starter-actuator, spring-boot-starter-tomcat
    3. Copy all packages from .iftarvakti GitHub project and refactor/place them inside the new Spring project – make sure to import all dependencies as well
    4. Create a new Controller in the root package like "IftarVaktiController.java":
    @RestController
    public class IftarVaktiController {
    
        @GetMapping("/startIftar")
        public void startIftar() {
            try {
                ApiContextInitializer.init();
                IftarVaktiBot bot = new IftarVaktiBot();
                TelegramBotsApi telegramBotsApi = new TelegramBotsApi();
                telegramBotsApi.registerBot(bot);
    
            } catch (TelegramApiException | IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    1. Like you did with the HelloController, this GET request is mapped by Spring Boot. If you successfully deployed the project you can go to localhost:8080/startIftar and the Java code will be executed accordingly.

    ps. I have not functionally checked any of your code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search