Running Slack service and token streaming concurrently in Spring web flux

I have a function named sendSlackmessage which takes a string and sends a message to Slack. Now, I’m faced with a scenario where I need to process a large text, splitting it into batches of 10 words, and send each batch to Slack using sendSlackmessage. However, since calling sendSlackmessage is time-consuming, I’m exploring the possibility of running the Slack service and token streaming concurrently to optimize performance.

Here’s a simplified version of my code:

private Flux
<StreamData> processStreamDataFlux(Flux <Object> streamDataFlux, AtomicReference<StringBuilder> answer, ChatBotConversationRPCResponse response) { AtomicBoolean hasAnswer = new AtomicBoolean(true); return streamDataFlux .flatMap(data -> { var inputData = ((StreamData) data); return Mono.just(inputData); }) .doOnNext(streamData -> { if (!streamData.getToken().isEmpty()) { if (streamData.getIsAnswer().equals("True")) { answer.updateAndGet(sb -> { sb.append(streamData.getToken()); if (sb.length() >= 10) { sendSlackMessage(sb.toString()); sb.delete(0, 10); } return sb; }); } else { logger.info(String.format("Sources URLs: %s", streamData.getToken())); response.setUrlSources(extractLinks(streamData.getToken())); } } }) .doOnComplete(() -> { if (hasAnswer.get()) { response.setAnswer(answer.get().toString()); response.setResponse(true); } else { response.setAnswer(chatBot.getNoAnswer()); response.setResponse(false); } });
}
Enter fullscreen mode
Exit fullscreen mode

In this code, processStreamDataFlux processes incoming stream data and calls sendSlackmessage for each batch of 10 words. However, I’m concerned about the impact of calling sendSlackmessage on the token streaming process. Is there a way to run the Slack service in a separate thread while ensuring smooth token streaming?

I’d appreciate any insights or suggestions on how to achieve this concurrency in Java. Thank you!


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top