How to create a Spring Reactor Flux from a ActiveMQ queue?

前端 未结 1 505
感情败类
感情败类 2021-01-08 01:32

I am experimenting with the Spring Reactor 3 components and Spring Integration to create a reactive stream (Flux) from a JMS queue.

I am attempting to create a reac

相关标签:
1条回答
  • 2021-01-08 01:37

    Works well for me:

    @SpringBootApplication
    @RestController
    public class SpringIntegrationSseDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringIntegrationSseDemoApplication.class, args);
        }
    
        @Autowired
        private ConnectionFactory connectionFactory;
    
        @Autowired
        private JmsTemplate jmsTemplate;
    
        @Bean
        public Publisher<Message<String>> jmsReactiveSource() {
            return IntegrationFlows
                    .from(Jms.messageDrivenChannelAdapter(this.connectionFactory)
                            .destination("testQueue"))
                    .channel(MessageChannels.queue())
                    .log(LoggingHandler.Level.DEBUG)
                    .log()
                    .toReactivePublisher();
        }
    
        @GetMapping(value = "/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
        public Flux<String> getPatientAlerts() {
            return Flux.from(jmsReactiveSource())
                    .map(Message::getPayload);
        }
    
        @GetMapping(value = "/generate")
        public void generateJmsMessage() {
            for (int i = 0; i < 100; i++) {
                this.jmsTemplate.convertAndSend("testQueue", "testMessage #" + (i + 1));
            }
        }
    
    }
    

    In one terminal I have curl http://localhost:8080/events which waits for SSEs from that Flux.

    In other terminal I perform curl http://localhost:8080/generate and see in the first one:

    data:testMessage #1
    
    data:testMessage #2
    
    data:testMessage #3
    
    data:testMessage #4
    

    I use Spring Boot 2.0.0.BUILD-SNAPSHOT.

    Also see here: https://spring.io/blog/2017/03/08/spring-tips-server-sent-events-sse

    0 讨论(0)
提交回复
热议问题