2017-06-08 84 views
0

我想要獲得springboot和駱駝工作的簡單例子,但未完成。不知道我做錯了什麼。在構建的gradle我已經包含到目前爲止春季引導和駱駝引發direct.DirectConsumerNotAvailableException

dependencies { 
    compile 'org.apache.camel:camel-spring-boot-starter:2.18.4' 
    compile 'org.apache.camel:camel-groovy:2.18.4' 
    compile 'org.apache.camel:camel-stream:2.18.4' 
    compile 'org.codehaus.groovy:groovy-all:2.4.11' 
    testCompile group: 'junit', name: 'junit', version: '4.11' 
    testCompile group: 'junit', name: 'junit', version: '4.12' 
} 

我創建這樣

@Component 
class DirectRoute extends RouteBuilder{ 
    @Override 
    void configure() throws Exception { 
     from ("direct:in") //tried stream:in also 
       .to ("stream:out") 
    } 

} 

一個DirectRoute組件然後我有一個司機的bean嘗試的調用路徑

@Component 
public class HelloImpl implements Hello { 

    @Produce(uri = "direct:in") 
    private ProducerTemplate template; 

    @Override 
    public String say(String value) throws ExecutionException, InterruptedException { 

     assert template 
     println "def endpoint is : " + template.getDefaultEndpoint() 
     return template.sendBody (template.getDefaultEndpoint(), value) 
    } 


} 

最後在springboot應用程序類中,我添加了一個像這樣的命令行參數,它從Spring上下文中獲取我的bean,並調用say方法。我正在使用groovy,所以我只是通過一個關閉命令行亞軍。

@Bean 
public CommandLineRunner commandLineRunner(ApplicationContext ctx) { 
//return closure to run on startup - just list the beans enabled 
    {args -> 
     println("Let's inspect the beans provided by Spring Boot:") 

     String[] beanNames = ctx.getBeanDefinitionNames() 
     Arrays.sort(beanNames) 
     for (String beanName : beanNames) { 
      println(beanName) 
     } 

     println("call the direct:start route via the service") 
     Hello service = ctx.getBean("helloService") 

     def result = service.say("William") 
     println "service returned : $result " 
    } 
} 

當我運行我的應用程序獲得所有打印出來的bean的名字(這是確定),但是當我調用直接:在通過製作模板,我得到這個錯誤(org.apache.camel.component.direct .DirectConsumerNotAvailableException)見下面。

我期待的路線被觸發的名稱發送,看看到達輸出流 - 但這就是我得到的。

Caused by: org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[ID-MONSTER-PC2-58911-1496920205300-0-2] 
    at org.apache.camel.util.ObjectHelper.wrapCamelExecutionException(ObjectHelper.java:1795) ~[camel-core-2.18.4.jar:2.18.4] 
    at org.apache.camel.util.ExchangeHelper.extractResultBody(ExchangeHelper.java:677) ~[camel-core-2.18.4.jar:2.18.4] 
    at org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:515) ~[camel-core-2.18.4.jar:2.18.4] 
    at org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:511) ~[camel-core-2.18.4.jar:2.18.4] 
    at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:163) ~[camel-core-2.18.4.jar:2.18.4] 
    at org.apache.camel.ProducerTemplate$sendBody$0.call(Unknown Source) ~[na:na] 
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) [groovy-all-2.4.11.jar:2.4.11] 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) [groovy-all-2.4.11.jar:2.4.11] 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133) [groovy-all-2.4.11.jar:2.4.11] 
    at services.HelloImpl.say(HelloImpl.groovy:29) ~[main/:na] 
    at services.Hello$say.call(Unknown Source) ~[na:na] 
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) [groovy-all-2.4.11.jar:2.4.11] 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) [groovy-all-2.4.11.jar:2.4.11] 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125) [groovy-all-2.4.11.jar:2.4.11] 
    at application.Application$_commandLineRunner_closure1.doCall(Application.groovy:47) ~[main/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121] 
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121] 
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93) ~[groovy-all-2.4.11.jar:2.4.11] 
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325) ~[groovy-all-2.4.11.jar:2.4.11] 
    at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294) ~[groovy-all-2.4.11.jar:2.4.11] 
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022) ~[groovy-all-2.4.11.jar:2.4.11] 
    at groovy.lang.Closure.call(Closure.java:414) ~[groovy-all-2.4.11.jar:2.4.11] 
    at org.codehaus.groovy.runtime.ConvertedClosure.invokeCustom(ConvertedClosure.java:54) ~[groovy-all-2.4.11.jar:2.4.11] 
    at org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java:124) ~[groovy-all-2.4.11.jar:2.4.11] 
    at com.sun.proxy.$Proxy44.run(Unknown Source) ~[na:na] 
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] 
    ... 10 common frames omitted 
Caused by: org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: direct://in. Exchange[ID-MONSTER-PC2-58911-1496920205300-0-2] 
    at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:55) ~[camel-core-2.18.4.jar:2.18.4] 
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) ~[camel-core-2.18.4.jar:2.18.4] 
    at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:97) ~[camel-core-2.18.4.jar:2.18.4] 
    at org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:529) ~[camel-core-2.18.4.jar:2.18.4] 
    at org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:497) ~[camel-core-2.18.4.jar:2.18.4] 
    at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:365) ~[camel-core-2.18.4.jar:2.18.4] 
    at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:497) ~[camel-core-2.18.4.jar:2.18.4] 
    at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:225) ~[camel-core-2.18.4.jar:2.18.4] 
    at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:144) ~[camel-core-2.18.4.jar:2.18.4] 
    at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:161) ~[camel-core-2.18.4.jar:2.18.4] 

我做錯了什麼?以及爲什麼生產者模板對'direct:in'的調用(也試圖用相同的問題流)不起作用?我認爲.to(「stream:out」)會成爲消費者。

任何指針或建議,在這一點感激地收到對問題

+0

凡被傳遞路線,顯示一個消息直接:在? – ndrone

+0

都有點奇怪 - 雖然我有SpringBootApplication註釋它沒有拿起組件註釋的路線。我不得不添加ScanBasepackages來提取它們。此外,camelContext不會自動啓動,所以我不得不添加一個start()和一個stop()以及一個睡眠inbetween。當我這樣做,並打電話給我的直接:在模板中似乎工作。將在早上完成投資並更新 –

+0

酷春啓動應用程序組件僅掃描子軟件包。所以如果駱駝配置在一個更高的包裏,它不會掃描它。 – ndrone

回答

0

確定更新。

  1. 我有一個應用程序類與@SpringBootApplication註釋的子包。所以,沒錯,它只能掃描子包。
  2. 您可以添加scanBasePackages=scanBaseClasses=參數,但是當我嘗試對單個類進行掃描時,它似乎以任何方式掃描整個目錄並抓取其他目錄。
  3. 我重構了應用程序以擁有包含子包的單個根包,並選擇將'scanBasePakages'設置爲新的根包。但將應用程序類保留在其自己的子包中(僅限個人偏好 - 文檔建議將應用程序保留在根包中)
  4. 現在可以添加使用@Configuration註釋的其他類來生成bean或使用基本的@Component
  5. 如果您創建了帶有@Component註釋的駱駝路線,它們將在camelContext中爲您自動配置。
  6. 它默認顯示Spring沒有爲你啓動camelContext。當我檢查上下文的狀態時,它顯示爲starting而不是started。所以在我的commandLineRunner中,我不得不開始注入camelContext,並且必須自己啓動它,並在完成時退出它。我有點驚訝,因爲我認爲SpringBootStarter會自動啓動camelContext,但似乎沒有。
  7. 一旦你有Spring組件掃描等工作,你開始camelContext,然後org.apache.camel.component.direct.DirectConsumerNotAvailableException異常的問題就消失了,事情開始奏效 - 至少我正在嘗試的寶貝示例。

所以修改後的結構現在看起來是這樣 enter image description here

修訂ApplicationClass現在看起來是這樣一些簡單的println輸出看上下文的狀態,和豆類的春天CTX。 helleoService bean仍然是我用來設置生產者模板來調用DirectRoute的代理。

package com.softwood.application 

import groovy.util.logging.Slf4j 
import org.apache.camel.CamelContext 
import org.springframework.beans.factory.annotation.Autowired 
import com.softwood.services.Hello 

/** 
* Created by willw on 07/06/2017. 
*/ 
import org.springframework.boot.CommandLineRunner; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.annotation.Bean 

@Slf4j //inject logger 
@SpringBootApplication (scanBasePackages = ["com.softwood"]) //forces scan at parent 
// same as @Configuration @EnableAutoConfiguration @ComponentScan with 'defaults' e.g. sub packages 
public class Application { 
    @Autowired 
    ApplicationContext ctx 

    @Autowired 
    CamelContext camelContext 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args) 
    } 

    @Bean 
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) { 
    //return closure to run on startup - just list the beans enabled 
     {args -> 

      println("Let's inspect the beans provided by Spring Boot:") 

      String[] beanNames = ctx.getBeanDefinitionNames() 
      Arrays.sort(beanNames) 
      for (String beanName : beanNames) { 
       println(beanName) 
      } 

      /* when component scan is working - bean routes are added 
      automatically to camel context via springBoot, however you do have to start 
      the camel context, yourself 
      */ 
      println "camelCtx has following components : " + camelContext.componentNames 
      println "camelCtx state is : " + camelContext.status 
      println "starting camel context" 
      camelContext.start() 
      println "camelCtx state now is : " + camelContext.status 


      //log.debug "wills logging call " 
      println("call the direct:start route via the service") 
      Hello service = ctx.getBean("helloService") 

      def result = service.say("William") 
      println "service returned : $result " 
      println "sleep 5 seconds " 
      sleep (5000) 
      println "stop camel context" 
      camelContext.stop() 
      println "camelCtx state now is : " + camelContext.status 

     } 
    } 

} 

該代理僅僅是註冊爲一個簡單的bean像這樣在Spring上下文

package com.softwood.services 
/** 
* Created by willw on 07/06/2017. 
*/ 
import org.apache.camel.Produce; 
import org.apache.camel.ProducerTemplate 
import org.springframework.stereotype.Component; 

import java.util.concurrent.ExecutionException 

@Component 
public class HelloImpl implements Hello { 

    @Produce(uri = "direct:in") /* ?block=true */ 
    private ProducerTemplate template 

    @Override 
    public String say(String value) throws ExecutionException, InterruptedException { 

     assert template 
     println "def endpoint is : " + template.getDefaultEndpoint() 
     //Future future = template.asyncSendBody(template.getDefaultEndpoint(), value) 
     //return future.get() 
     return template.sendBody (template.getDefaultEndpoint(), value) 
    } 


} 

的TimedRoute剛剛搞清楚了,沒有在

package com.softwood.camelRoutes 

/** 
* Created by willw on 07/06/2017. 
*/ 
import org.apache.camel.builder.RouteBuilder 
import org.springframework.stereotype.Component 


@Component 
class TimedRoute extends RouteBuilder { 

    @Override 
    void configure() throws Exception { 
     from ("timer:foo") 
     .to ("log:com.softwood.application.Application?level=WARN") 
    } 
} 

調用所需的模板,我簡單的無操作文件路徑is'nt工作(還),不知道爲什麼。懷疑我沒有得到文件的配置,不知何故,某些遊戲是必需的。

package com.softwood.camelRoutes 

import org.apache.camel.builder.RouteBuilder 
import org.springframework.stereotype.Component 


/** 
* Created by willw on 08/06/2017. 
*/ 
@Component 
class FileNoOpRoute extends RouteBuilder{ 
    @Override 
    void configure() throws Exception { 
     from ("file:../com.softwood.file-inbox?recursive=true&noop=true&idempotent=true") 
     .to ("file:../com.softwood.file-outbox") 

    } 

} 

但是基本不工作,至少駱駝做一些事情,而我只是有異常,並沒有之前。

我希望這可以幫助其他人同樣堅持當試圖走出去,這是最令人沮喪的,當你只是想看到的東西工作。

感謝@ndrone也偷看。

而這個鏈接指向另一個問題,關於Spring配置高亮顯示了上面的一些還 configuration with springboot problems

希望這將有助於其他人在陷阱獲得以及

+0

PS做了一些更多的實驗 - file://的默認相對路由是您項目的頂級根目錄,因此'file:// data/inbox'實際上是指'/data/inbox'。它不在你的課程路徑開始的地方。所以在上面的例子中,我的dev電腦上是'D:\ Intellij-projects \ camel-test \ data \ fileInbox'。一旦找出相對路徑開始的位置 - 它可以正常工作 –