在Spring MVC Controller中触发骆驼路线

时间:2019-10-19 11:10:52

标签: spring spring-mvc apache-camel spring-camel

“我的默认骆驼路线”在App Startup上工作(春季DSL)。这是移动文件的简单方法(因为我是Camel的初学者)。但是,我要做的是在启动JSP页面时仅在Controller中触发路由

例如

@RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) throws Exception {

//In here I want to execute a route not when app loads
}

我没有使用Spring Boot,我已经研究了这个主题,但是我什么都没找到

我还尝试过

@Autowired
CamelContext camelContext;

但是没有用。谢谢读者的帮助。

我意识到我们可以在xml中使用autoStartup = false,我使用了它,现在文件也不会通过我们的控制器触发。控制器的新代码是

  @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) throws Exception {

        SpringCamelContext camelContext = (SpringCamelContext) applicationContext.getBean("camelContext");

        try {
            System.out.println("Hello");
            camelContext.start();
            Thread.sleep(1500);
        } finally {
            System.out.println("Hello2");
            camelContext.stop();
        }
        return "index";
    }

在xml中我有

    <camel:camelContext  id="camelContext" trace="true" autoStartup="false">
        <camel:route >
            <camel:from uri="file:data/inbox?noop=true" />
            <camel:to uri="file:data/outbox" />
        </camel:route>
    </camel:camelContext>

1 个答案:

答案 0 :(得分:0)

通过将其添加到控制器代码中,我设法使此autoStartup正常工作

camelContext.setAutoStartup(true);