2
votes

I am using latest release version of spring boot with camel-cxf to expose rest/soap web services. However since I have many services and some of them are not well defined, I want to register each of them one by one, catch if any exception occurs and continue with remaining valid services. But when I set camel.springboot.auto-startup=false I can not manage to open cxf service again. (Please note that timer route can be started this way). Any solution or suggestion ?

@Component
public class TestRoutes {

    @Autowired CamelContext camelContext;
    @Autowired Swagger2Feature swagger2Feature;

    @PostConstruct
    public void init(){
        try {
            camelContext.start();
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        HelloResponse response = new HelloResponse();
        response.setCevap("response");
        List<Object> providers = new ArrayList<>();
        CxfRsComponent cxfComponent = new CxfRsComponent(camelContext);

        CxfRsEndpoint serviceEndpoint = new CxfRsEndpoint("/rest2", cxfComponent);
        serviceEndpoint.addResourceClass(Service1.class);
        serviceEndpoint.setProviders(providers);
        serviceEndpoint.setSynchronous(true);
        serviceEndpoint.setAddress("/rest2");
        serviceEndpoint.getFeatures().add(swagger2Feature);

        RouteBuilder builder = new RouteBuilder(camelContext) {
            @Override
            public void configure() throws Exception {
                from(serviceEndpoint)
                .id(Service1.class.getCanonicalName()+"rest")   
                .log("${header.operationName} message here")
                .setBody(constant(response));

                from("timer:mytimer?period=5000")
                    .id("timer")
                    .log("hi");

            }
        };
        try {
            camelContext.startRoute(Service1.class.getCanonicalName()+"rest");
            camelContext.startRoute("timer");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



2017-07-18 10:50:49.076  INFO 12052 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5149d738: startup date [Tue Jul 18 10:50:49 EEST 2017]; root of context hierarchy
2017-07-18 10:50:52.413  INFO 12052 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.apache.camel.spring.boot.CamelAutoConfiguration' of type [org.apache.camel.spring.boot.CamelAutoConfiguration$$EnhancerBySpringCGLIB$$5d14135] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-07-18 10:50:53.063  INFO 12052 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-07-18 10:50:53.085  INFO 12052 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-07-18 10:50:53.086  INFO 12052 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache 

    Tomcat/8.5.11
    2017-07-18 10:50:53.341  INFO 12052 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2017-07-18 10:50:53.342  INFO 12052 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4269 ms
    2017-07-18 10:50:53.974  INFO 12052 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
    2017-07-18 10:50:53.976  INFO 12052 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'CXFServlet' to [/services/*]
    2017-07-18 10:50:53.981  INFO 12052 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
    2017-07-18 10:50:53.982  INFO 12052 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    2017-07-18 10:50:53.982  INFO 12052 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
    2017-07-18 10:50:53.982  INFO 12052 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
    2017-07-18 10:50:54.682  INFO 12052 --- [           main] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.19.1 (CamelContext: camel-1) is starting
    2017-07-18 10:50:54.685  INFO 12052 --- [           main] o.a.c.m.ManagedManagementStrategy        : JMX is enabled
    2017-07-18 10:50:54.999  INFO 12052 --- [           main] o.a.c.i.converter.DefaultTypeConverter   : Loaded 214 type converters
    2017-07-18 10:50:55.064  INFO 12052 --- [           main] o.a.c.i.DefaultRuntimeEndpointRegistry   : Runtime endpoint registry is in extended mode gathering usage statistics of all incoming and outgoing endpoints (cache limit: 1000)
    2017-07-18 10:50:55.094  INFO 12052 --- [           main] o.a.camel.spring.SpringCamelContext      : StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
    2017-07-18 10:50:55.095  INFO 12052 --- [           main] o.a.camel.spring.SpringCamelContext      : Total 0 routes, of which 0 are started.
    2017-07-18 10:50:55.097  INFO 12052 --- [           main] o.a.camel.spring.SpringCamelContext      : Apache Camel 2.19.1 (CamelContext: camel-1) started in 0.415 seconds
    2017-07-18 10:50:55.301  WARN 12052 --- [           main] o.apache.cxf.jaxrs.utils.ResourceUtils   : No resource methods have been found for resource class java.lang.Class
    2017-07-18 10:50:55.433  INFO 12052 --- [           main] org.apache.cxf.endpoint.ServerImpl       : Setting the server's publish address to be /rest1
    2017-07-18 10:50:55.636  INFO 12052 --- [           main] org.reflections.Reflections              : Reflections took 60 ms to scan 1 urls, producing 21 keys and 25 values 
    2017-07-18 10:50:56.106  INFO 12052 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5149d738: startup date [Tue Jul 18 10:50:49 EEST 2017]; root of context hierarchy
    2017-07-18 10:50:56.206  INFO 12052 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
    2017-07-18 10:50:56.208  INFO 12052 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
    2017-07-18 10:50:56.267  INFO 12052 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2017-07-18 10:50:56.267  INFO 12052 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2017-07-18 10:50:56.330  INFO 12052 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2017-07-18 10:50:57.591  INFO 12052 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2017-07-18 10:50:57.686  INFO 12052 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
    2017-07-18 10:50:57.694  INFO 12052 --- [           main] c.e.demo.AutoStartupFalseApplication     : Started AutoStartupFalseApplication in 9.057 seconds (JVM running for 9.764)
1

1 Answers

1
votes

The problem was if bus not set explicitly CamelCXF generates a default one which is not spring aware. So solution was generatin a SpringBus bean and wiring it to explicitly to the CxfRsEndpoint object.

In the configuration class

@Bean(destroyMethod = "shutdown")
public SpringBus cxf() {
    return new SpringBus();
}

In the endpoint definition :

@Autowired Bus bus;
.
.
CxfRsEndpoint serviceEndpoint = new CxfRsEndpoint("/rest2", cxfComponent);
.
.
serviceEndpoint.setBus(bus);

Then it starts working as expected.