com.xlrit.gears.server.tmp.MessageEventService Maven / Gradle / Ivy
package com.xlrit.gears.server.tmp;
import com.xlrit.gears.base.event.ElementPublishedEvent;
import com.xlrit.gears.base.model.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Sinks;
@Service
public class MessageEventService {
private static final Logger LOG = LoggerFactory.getLogger(MessageEventService.class);
private final Sinks.Many messagePublisher;
public MessageEventService() {
this.messagePublisher = Sinks.many().multicast().directBestEffort();
}
public Flux subscribe(String userId) {
return messagePublisher.asFlux().filter(message -> message.forUser(userId));
}
@EventListener
public void onPublish(ElementPublishedEvent publishedEvent) {
Message message = publishedEvent.getElement();
LOG.info("onPublish: message={}", message);
messagePublisher.emitNext(message, Sinks.EmitFailureHandler.FAIL_FAST);
}
}