Use existing http server in spring boot as camel endpoint

女生的网名这么多〃 提交于 2019-12-05 07:00:30

问题


I have a spring boot application that uses the spring boot starter web. This creates a running Tomcat instance and sets up the http server running on a port. Within my camel route, I want to use this http server as the component for http requests, but I can't figure out how to utilize it. I see many many examples of configuring a jetty instance and consuming from it, but then wouldn't I in effect have two http servers running? I only want to have one. I assume the http server is already autowired up since I can consume from it with other spring code (such as a RestController) and I can see it started in my spring boot logs as well.

@Component
public class ExampleRoute extends RouteBuilder
{
    @Override
    public void configure() throws Exception
    {

        //@formatter:off

        from( <want to take in an http request here> )
            .log( LoggingLevel.INFO, log, "Hello World!" );

        //@formatter:on

    }
}

回答1:


There is an example here: https://github.com/camelinaction/camelinaction2/tree/master/chapter7/springboot-camel

You can to register a ServletRegistrationBean that setup the Camel Servlet with Spring Boot.

@Bean
ServletRegistrationBean camelServlet() {
    // use a @Bean to register the Camel servlet which we need to do
    // because we want to use the camel-servlet component for the Camel REST service
    ServletRegistrationBean mapping = new ServletRegistrationBean();
    mapping.setName("CamelServlet");
    mapping.setLoadOnStartup(1);
    // CamelHttpTransportServlet is the name of the Camel servlet to use
    mapping.setServlet(new CamelHttpTransportServlet());
    mapping.addUrlMappings("/camel/*");
    return mapping;
}

However for Camel 2.19 we plan on make this simpler and OOTB: https://issues.apache.org/jira/browse/CAMEL-10416

And then you can do

from("servlet:foo")
  .to("bean:foo");

Where the HTTP url to call that Camel route will be http:localhost:8080/camel/foo



来源:https://stackoverflow.com/questions/40492458/use-existing-http-server-in-spring-boot-as-camel-endpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!