2017-05-30 65 views
2

我遇到了Spring雲流的問題。問題是我有一個bean會在創建時立即寫入Kafka(方法用@PostConstruct註解),所以我自動裝入適當的MessageChannel,並在application.yml中設置目標和binder屬性。它是這樣的:@PostConstruct和自動裝配MessageChannel

@Component 
@RequiredArgsConstructor 
public class Sender 
{ 
    private final MessageChannel output; 

    @PostConstruct 
    public void start() 
    { 
     output.send(new GenericMessage("Hello world"); 
    } 
} 

而且application.yml

spring: 
    cloud: 
     stream: 
      bindings: 
       output: 
       destination: someData 
       binder: kafka 

我也有以下的依存關係:

- spring-cloud-stream-reactive 
- reactor-core 
- spring-cloud-starter-stream-kafka 

項目本身開始,但我發現了以下

:試圖寫入 outputstart()方法時異常

是否因爲卡夫卡粘合劑尚未設法綁定頻道或什麼?如果是這樣,那麼另一種方法是什麼呢。

在此先感謝。

回答

3

你不能這樣做,從@PostConstruct。太早了。其他組件可能已經初始化。

你有你發送邏輯移到SmartLifecycle.start()實施。

+0

這就是我的想法。將嘗試與SmartLifecycle.start()然後。謝謝! – wookie

+0

工作完美,再次感謝! – wookie