我使用Spring Cloud场景为生产者-处理器-消费者场景创建了一个示例应用程序。在这里,我使用了基于传统注释的方法。
在单元测试中,我想测试一种简单的方案,即在转换后生成一条消息并声明消耗的消息。但是我没有在消费者绑定端收到消息。请让我知道这里可能缺少什么。
Producer.java
library(dplyr)
cols <- c('i', 'j', 'k')
dat %>%
group_by(Categories) %>%
summarise(across(starts_with('Item_'), max)) %>%
#In old dplyr
#summarise_at(vars(starts_with('Item_')), max)
mutate(FactorCol = purrr::pmap_chr(select(., starts_with('Item_')),
~toString(cols[c(...) == 1]))) %>%
select(Categories, FactorCol) %>%
left_join(dat, by = 'Categories')
# Categories FactorCol Items Item_i Item_j Item_k
# <chr> <chr> <chr> <dbl> <dbl> <dbl>
#1 A i, j x 1 0 0
#2 A i, j y 0 1 0
#3 B i, j, k y 0 1 0
#4 B i, j, k z 1 0 1
#5 C i, k x 1 0 0
#6 C i, k z 0 0 1
#7 D j, k y 0 1 0
#8 D j, k z 0 0 1
TransformProcessor.java
@EnableBinding(MyProcessor.class)
public class Producer {
@Bean
@InboundChannelAdapter(value = MyProcessor.OUTPUT, poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1"))
public MessageSource<String> produceMessage() {
return () -> new GenericMessage<>("Hello Spring Cloud World >>> " + Instant.now());
}
}
Consumer.java
@EnableBinding(MyProcessor.class)
public class TransformProcessor {
@Transformer(inputChannel = MyProcessor.OUTPUT, outputChannel = MyProcessor.INPUT)
public String transform(String message) {
System.out.println("Transforming the message: " + message);
return message.toUpperCase();
}
}
MyProcessor.java
@EnableBinding(MyProcessor.class)
public class Consumer {
@StreamListener(MyProcessor.INPUT)
public void consume(String message) {
System.out.println("Consuming transformed message: " + message);
}
}
SpringCloudStreamLegacyApplicationTests.java
public interface MyProcessor {
String INPUT = "my-input";
final static String OUTPUT = "my-output";
@Input(INPUT)
SubscribableChannel anInput();
@Output(OUTPUT)
MessageChannel anOutput();
}
在这里,我希望在 handleMessage 方法中收到一条消息。
注意,我正在使用以下依赖项进行测试:
@SpringBootTest
class SpringCloudStreamLegacyApplicationTests {
@Autowired
private MyProcessor myProcessor;
@Autowired
private MessageCollector messageCollector;
@Test
public void testConsumer() {
myProcessor.anOutput().send(new GenericMessage<byte[]>("hello".getBytes()));
Message<?> poll = messageCollector.forChannel(myProcessor.anInput()).poll();
System.out.println("Received: " + poll.getPayload());
}
}