2017-09-24 1 views
0

스프링 클라우드 스트림을 사용하고 있으며 반환 유형 MessageSource를 사용하는 InboundChannelAdapter를 사용할 때 문제가 발생하면 싱글 톤 클래스처럼 동작하며 매 1 초마다 실행됩니다. 동일한 데이터를 소비자에게 보내는 것. 또한 로거는 응용 프로그램 시작시 한 번만 인쇄합니다.스프링 데이터 스트림 InboundChannelAdapter의 반환 유형이 다른 경우 동작이 다릅니다.

@InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1")) 
    public MessageSource<String> uuidSource() { 
     UuidCaller uuidCaller = new UuidCaller(atomicLong.addAndGet(1), new Date(), UUID.randomUUID().toString()); 
     logger.info("buid request:"+uuidCaller); 
     return() -> MessageBuilder.withPayload(uuidCaller.toString()).build(); 
    } 

는하지만 난 문자열에 간단한에 MessageSource를 변경 한 경우 입력 한 후 그 작업을 잘

InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1")) 
    public String uuidSource() { 
     UuidCaller uuidCaller = new UuidCaller(atomicLong.addAndGet(1), new Date(), UUID.randomUUID().toString()); 
     logger.info("build request:"+uuidCaller); 
     return uuidCaller.toString(); 
    } 

가 업데이트 된 데이터로 소비자를 전송하고 또한 매초마다의 업데이트 로그를 인쇄합니다.

내 질문은 왜 다른 반환 유형에 대한 다른 동작입니까?

답변

1

MessageSource의 경우 '@Bean'으로 주석을 추가해야합니다. 따라서 UUID는 한 번만 생성됩니다. POJO 메소드 일 때 각 폴에서 작성됩니다.

UUID를 람다로 옮기면 동일하게 작동합니다.

편집

@Bean 
@InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1")) 
public MessageSource<String> uuidSource() { 
    return() -> { 
     UuidCaller uuidCaller = new UuidCaller(atomicLong.addAndGet(1), new Date(), UUID.randomUUID().toString()); 
     logger.info("buid request:"+uuidCaller); 
     return MessageBuilder.withPayload(uuidCaller.toString()).build(); 
    }; 
} 
+0

안녕하세요 회색, 당신의 응답을 주셔서 감사합니다, 나는 MessageSource를에 @Bean 추가 한하지만 여전히 1 초에 UuidCaller의 (싱글)을 하나의 사본을 전송하지 인쇄 아무것도 (로그). UUID를 람다 (예 : 코드)로 옮길 수있는 방법을 써 주시겠습니까? 감사합니다. – Baba

+0

감사합니다. 나는 봄 팀의 큰 팬이다. 귀중한 답변에 감사드립니다. – Baba

관련 문제