Why does Webflux WebSocketHandler returned as a lambda expression share the response, but not when defined as a component

The following configuration that returns WebSocket#handle as a lambda expression shares the Entity to all browser connections.


 @Bean
        public WebSocketHandler webSocketHandler (ObjectMapper objectMapper,
                ProfileCreatedEventListener eventListener) {            
            
            Flux<ProfileCreatedEvent> flux = Flux.create(eventListener).share();
            
            return session -> {
                
                Flux<WebSocketMessage> messageFlux = flux.map(evt -> {
                    
                    try {
                        
                        return objectMapper.writeValueAsString(evt.getSource());
                        
                    } catch(IOException e) {
                        throw new RuntimeException(e);                      
                    }
                    
                })                  
                .map(str -> {
                    
                    return session.textMessage(str); //returns WebSocketMessage
                });
                
                return session.send(messageFlux);
            } ; //end WebSocket#handle
            
        } //end @Bean webSocketHandler

When I code exactly the same WebSocketHandler#handle method within a component that implements WebSocketHandler only the browser that connects first receives the Json entity. What is the difference?

 @Configuration

                @Bean
                @Autowired
        public HandlerMapping handlerMapping(MyWebSocketHandler wsh) {
            
            Map<String, WebSocketHandler> map = new HashMap<>();
            
            map.put("/ws/profiles", wsh);
            
            int order = 10; // after ProfileEndpointConfiguration           
            
            return new SimpleUrlHandlerMapping(map, order);
        } 
@Component
public class MyWebSocketHandler implements WebSocketHandler {

        @Override
    public Mono<Void> handle(WebSocketSession session) {        
        
        Flux<ProfileCreatedEvent> fluxEvent = Flux.create(listener).share();
        
        Flux<WebSocketMessage> messageFlux = fluxEvent.map (
                
                     evt -> {                
                       ObjectMapper objectMapper = new ObjectMapper();  
                       String json = null;                  
                try {                       
                  json = objectMapper.writeValueAsString(evt.getSource());                          
                } catch(IOException e) {                        
                        throw new RuntimeException(e);                      
                }
                    return json;
                })                      
               .map(str -> session.textMessage(str));
        
        return session.send(messageFlux);       
        
    } //end handle  
}

General Architecture:
A Json entity is uploaded to a RouterFunction. The Router is bound to a handler
that invokes ReactiveCrudRepository#save through a Service component. At Service#create
the inserted entity is published.

//Model
public class Profile {
    @Id
    private String id;
    private String email;
}

public interface ProfileRepository extends ReactiveMongoRepository<Profile, String> {}

//Service Component
 public ProfileService(ApplicationEventPublisher publisher, ProfileRepository profileRepository) {. . .}

 public Mono<Profile> create(String email){
       return profileRepository.save( new Profile(null, email) )
               .doOnSuccess( profile -> publisher.publishEvent( new ProfileCreatedEvent( profile) ) );
   }

public class ProfileCreatedEvent extends ApplicationEvent {

    public ProfileCreatedEvent(Profile source) {        
        super( source );            
    }
}

//Handler

 public Mono<ServerResponse> create(ServerRequest request){
        final Flux<Profile> profileFlux = request.bodyToFlux( Profile.class )
                .flatMap( p -> profileService.create( p.getEmail() ) );
        return defaultWriteResponse( profileFlux );
    }

//Note: The body is not returned in the ServerResponse, so that Profile entity can be published.

  private static Mono<ServerResponse> defaultWriteResponse(Publisher<Profile> profiles) {
        return Mono
                .from(profiles)
                .flatMap(p -> ServerResponse
                        .created( URI.create( "/profiles/" + p.getId()))
                        .contentType(MediaType.APPLICATION_JSON)
                        .build()
                );
    }

onApplicationEvent is coded on a component that
implements ApplicationListener and Consumer<FluxSink>.
The component is used as a Consumer for Flux#create.

Within the WebSocketHandler:

Flux fluxEvent = Flux.create(listener).share();

Flux#create(Consumer <FluxSink>) is used to return a Flux. When the Profile entity is published, Sink#next
passes the Profile entity to React’s Subscriber.

@Component
public class ProfileCreatedEventListener implements ApplicationListener <ProfileCreatedEvent>, 
       Consumer <FluxSink<ProfileCreatedEvent>> {

 @Override
    public void onApplicationEvent(ProfileCreatedEvent event) {     
        this.queue.offer(event);        
    }

  @Override
    public void accept(FluxSink<ProfileCreatedEvent> sink) {
        
    this.executor.execute(() -> {                   
        while(true) {                       
                  try {                         
               ProfileCreatedEvent event = que.take()
                   sink.next(event) ;
            
              } catch (InterruptedException e) {}
                        
                }//end while
         } //end lambda
      ) ; //end execute     
    } //end accept
}//end class

Configurations are tested using curl to upload an entity the router. A simple HTML view includes the JavaScript.WebSocket#onMessage defines a callback that displays the Json text.

The code that works was obtained from the tutorial: Build Reactive APIs with Spring WebFlux by Matt Raible
https://developer.okta.com/blog/2018/09/24/reactive-apis-with-spring-webflux

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật