2017-07-18 89 views
1

我正在使用最新版本的春季啓動與駱駝cxf暴露休息/香皂網絡服務。然而,由於我有很多服務,其中一些服務並沒有很好的定義,所以我想逐一註冊它們中的每一個,如果發生任何異常,請繼續並繼續使用剩餘的有效服務。但是,當我設置camel.springboot.auto-startup = false我不能設法再次打開cxf服務。 (請注意,定時器路由可以以這種方式啓動)。任何解決方案或建議?駱駝cxf與春季啓動手動啓動路線

@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[email protected]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[email protected]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) 

回答

0

問題是如果總線沒有明確設置CamelCXF生成一個默認的彈簧不知道。所以解決方案是生成一個SpringBus bean並將它明確地連接到CxfRsEndpoint對象。

在配置類

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

在端點定義:

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

然後,它開始爲預期工作。