繁体   English   中英

如何从Spring Controller启动CommandLineRunner

[英]How to launch CommandLineRunner from Spring Controller

我启动的顶级程序包中有一个主Spring Boot应用程序,子程序包中有不同的控制器:

ca.example.batch.MainBatchApplication
ca.example.batch.job1.Job1Controller (/batch/startJob1)
ca.example.batch.job2.Job2Controller (/batch/startJob2)

我可以通过以下URL来启动Spring Batchhttp:// localhost:8080 / batch / startJob1http:// localhost:8080 / batch / startJob2

但是,在另一个包中,我有:

ca.example.batch.job3.Job3Controller
ca.example.batch.job3.Job3Application

...这不是Spring Batch,而是Spring CommandLineRunner。 我想知道是否有一种方法可以在不自动启动CommandLineRunner的情况下启动MainBatchApplication,而是通过控制器(即http:// localhost:8080 / batch / startJob3)运行它。

我正在查看的控制器代码是:

package ca.example.batch.job3;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Job3Controller {

    @RequestMapping("/batch/startJob3")
    public String handle() throws Exception {

        Job3Application app = new Job3Application();

        Logger logger = LoggerFactory.getLogger(this.getClass());

        logger.info("app: " + app);

        String args = "";
        app.run(args);

        return "COMPLETE";
    }
}

Job3Application是:

package ca.example.batch.job3;

import static java.lang.System.exit;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Import;
import ca.example.batch.common.CommonLibraryReference;
import ca.example.batch.common.domain.WasHost;
import ca.example.batch.common.svc.WasHostService;

@SpringBootApplication
@Import(CommonLibraryReference.class)
public class Job3Application implements CommandLineRunner {

    private final Logger logger = LoggerFactory.getLogger(Job3Application.class);

    @Autowired
    public DataSource dataSource;

    @Autowired
    public WasHostService wasHostService;

    public Job3Application() {
    }

    public static void main(String[] args) throws Exception {
        new SpringApplicationBuilder(Job3Application.class)
        .web(WebApplicationType.NONE)
        .bannerMode(Banner.Mode.OFF)
        .run(args);
    }

    @Override
    public void run(String... strings) throws Exception {
        logger.info("Loading data...");

        logger.info("wasHostService: " + wasHostService);
        List<WasHost> hostList = wasHostService.findAll();
        if (!hostList.isEmpty()) {
            for (WasHost host : hostList) {
                logger.info("hostname: " + host.getHostname());
            }
        } else {
            logger.info("No hosts found in database. Aborting data collection.");
            exit(0);
        }
    }
}

我在日志中得到的错误是:

""2018-07-07 12:56:09 [http-nio-9191-exec-1] INFO  o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring FrameworkServlet 'dispatcherServlet'
""2018-07-07 12:56:09 [http-nio-9191-exec-1] INFO  c.e.b.job3.Job3Controller - app: ca.example.batch.job3.Job3Application@472d7ac
""2018-07-07 12:56:09 [http-nio-9191-exec-1] INFO  c.e.b.job3.Job3Application - Loading data...
""2018-07-07 12:56:09 [http-nio-9191-exec-1] INFO  c.e.b.job3.Job3Application - wasHostService: null
""2018-07-07 12:56:09 [http-nio-9191-exec-1] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
"java.lang.NullPointerException: null
    at ca.example.batch.job3.Job3Application.run(Job3Application.java:47)
    at ca.example.batch.job3.Job3Controller.handle(Job3Controller.java:21)

...当我启动控制器请求时。

如果这不是正确的方法,请提出建议。

基本上,我想做的是从控制器内部启动main(),但是使用MainBatchApplication运行时运行它(如果有意义)。 程序完成后,它将返回代码发送回控制器并显示在浏览器中。

谢谢乔伊

ca.example.batch.MainBatchApplication是您启动的主要应用。 因此,它将扫描软件包ca.example.batch所有组件。 这意味着它应该检测到ca.example.batch.job3.Job3Application因此您应该能够在Job3Controller @Autowire

这应该工作:

@Controller
public class Job3Controller {
  private final Job3Application job3Application;      

  public Job3Controller (Job3Application job3Application){
     this.job3Application = job3Application;
  }

  @RequestMapping("/batch/startJob3")
  public String handle() throws Exception {
    String[] args = ...
    this.job3Application.run(args);
    return "COMPLETE";
  }

  ....
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM