Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support async request() convenience API #11

Closed
mcqueary opened this issue Feb 16, 2016 · 2 comments
Closed

Support async request() convenience API #11

mcqueary opened this issue Feb 16, 2016 · 2 comments
Assignees

Comments

@mcqueary
Copy link

Support an asynchronous 1:N request call allows the user to supply their own MessageHandler callback to handle responses. Something along the lines of:

request(String subject, byte[] data, long timeout, TimeUnit unit, int maxReplies, MessageHandler cb)
request(String subject, byte[] data, int maxReplies, MessageHandler cb)
request(String subject, byte[] data, long timeout, TimeUnit unit, MessageHandler cb)

Thought will need to be given to managing the life of the subscription, probably using a ScheduledExecutorService to terminate the subscription once the timeout is reached.

Thought should also be given to whether this is just provided as example code.

@mcqueary mcqueary self-assigned this Feb 16, 2016
@pires
Copy link
Contributor

pires commented Feb 22, 2016

Here's how I do it:

private final ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();

public void request(String subject, long timeoutInMillis, MessageHandler handler) {
    // prepare inbox
    final String inbox = nats.newInbox();
    // subscribe to inbox
    final Subscription subscription = nats.subscribeAsync(inbox, handler);
    // schedule unsubscribe
    ses.schedule((Runnable) () -> subscription.close(), timeoutInMillis, TimeUnit.MILLISECONDS);
    // send request
    nats.publish(subject, inbox, null);
}

@ColinSullivan1
Copy link
Member

At this time, functionality along these lines should occur outside of the core NATS library.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants