最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

nats.io - How to provide job results form Nats JetStream worker? - Stack Overflow

programmeradmin4浏览0评论

I publish a job on JetStream and want to have a mechanism for waiting for the job result. Something like:

    public <T extends JobRequest> CompletableFuture<Void> publishJobRequestWithReply(final T jobRequest) throws Exception {
        final var subject = subjectOf(jobRequest);

        var replyTo = NUID.nextGlobal();

        final var data = mapper.writeValueAsBytes(
                new JobRequestEventData<>(jobRequest, replyTo));

        var replyToSub = nc.subscribe(replyTo); // Core NATS does not require pre-configured streams, making it perfect for temporary reply subjects.


        jetStream.publishAsync(subject, data);

        return CompletableFuture.runAsync(() -> {
            while (true) {
                try {
                    var msg = replyToSub.nextMessage(Duration.ofSeconds(1));
                    if (msg == null) {
                        continue;
                    }
                    System.out.println("Reply: " + msg);
                    break;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally {
                }
            }
            replyToSub.unsubscribe();
        });

Now, the resulting CompletableFuture is using the Core NATS, and not JetStreams (as no stream is configured for this). Above code works, but I would like to use the JetStream, to have persistent result (as I would like to be sure it comes back).

Should I create a result stream for the results? Or would above code be enough (robust enough)?

发布评论

评论列表(0)

  1. 暂无评论