mirror of
https://github.com/taigrr/nats.docs
synced 2025-01-18 04:03:23 -08:00
217 lines
7.8 KiB
HTML
217 lines
7.8 KiB
HTML
|
|
<div class="tab-wrap">
|
|
|
|
|
|
<input type="radio" id="drain_conn_go" name="drain_conn" class="tab" checked>
|
|
|
|
<label for="drain_conn_go" class="api-lang" data-language="go">Go</label>
|
|
|
|
|
|
<input type="radio" id="drain_conn_java" name="drain_conn" class="tab">
|
|
|
|
<label for="drain_conn_java" class="api-lang" data-language="java">Java</label>
|
|
|
|
|
|
<input type="radio" id="drain_conn_js" name="drain_conn" class="tab">
|
|
|
|
<label for="drain_conn_js" class="api-lang" data-language="js">JavaScript</label>
|
|
|
|
|
|
<input type="radio" id="drain_conn_py" name="drain_conn" class="tab">
|
|
|
|
<label for="drain_conn_py" class="api-lang" data-language="py">Python</label>
|
|
|
|
|
|
<input type="radio" id="drain_conn_ruby" name="drain_conn" class="tab">
|
|
|
|
<label for="drain_conn_ruby" class="api-lang" data-language="ruby">Ruby</label>
|
|
|
|
|
|
<input type="radio" id="drain_conn_ts" name="drain_conn" class="tab">
|
|
|
|
<label for="drain_conn_ts" class="api-lang" data-language="ts">TypeScript</label>
|
|
|
|
|
|
|
|
<div class="tab__content">
|
|
<pre id="drain_conn_go_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/go-nats-examples/blob/master/api-examples/drain_conn/main.go#L12-64"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-go">wg := sync.WaitGroup{}
|
|
wg.Add(1)
|
|
|
|
errCh := make(chan error, 1)
|
|
|
|
// To simulate a timeout, you would set the DrainTimeout()
|
|
// to a value less than the time spent in the message callback,
|
|
// so say: nats.DrainTimeout(10*time.Millisecond).
|
|
|
|
nc, err := nats.Connect("demo.nats.io",
|
|
nats.DrainTimeout(10*time.Second),
|
|
nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, err error) {
|
|
errCh <- err
|
|
}),
|
|
nats.ClosedHandler(func(_ *nats.Conn) {
|
|
wg.Done()
|
|
}))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Just to not collide using the demo server with other users.
|
|
subject := nats.NewInbox()
|
|
|
|
// Subscribe, but add some delay while processing.
|
|
if _, err := nc.Subscribe(subject, func(_ *nats.Msg) {
|
|
time.Sleep(200 * time.Millisecond)
|
|
}); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Publish a message
|
|
if err := nc.Publish(subject, []byte("hello")); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Drain the connection, which will close it when done.
|
|
if err := nc.Drain(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Wait for the connection to be closed.
|
|
wg.Wait()
|
|
|
|
// Check if there was an error
|
|
select {
|
|
case e := <-errCh:
|
|
log.Fatal(e)
|
|
default:
|
|
}
|
|
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="drain_conn_java_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/java-nats-examples/blob/master/src/main/java/io/nats/examples/DrainConn.java#L16-40"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-java">Connection nc = Nats.connect("nats://demo.nats.io:4222");
|
|
|
|
// Use a latch to wait for a message to arrive
|
|
CountDownLatch latch = new CountDownLatch(1);
|
|
|
|
// Create a dispatcher and inline message handler
|
|
Dispatcher d = nc.createDispatcher((msg) -> {
|
|
String str = new String(msg.getData(), StandardCharsets.UTF_8);
|
|
System.out.println(str);
|
|
latch.countDown();
|
|
});
|
|
|
|
// Subscribe
|
|
d.subscribe("updates");
|
|
|
|
// Wait for a message to come in
|
|
latch.await();
|
|
|
|
// Drain the connection, which will close it
|
|
CompletableFuture<Boolean> drained = nc.drain(Duration.ofSeconds(10));
|
|
|
|
// Wait for the drain to complete
|
|
drained.get();
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="drain_conn_js_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/node-nats-examples/blob/master/src/connection_samples.js#L91-111"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-javascript">let nc = NATS.connect({url: "nats://demo.nats.io:4222"});
|
|
let inbox = createInbox();
|
|
let counter = 0;
|
|
nc.subscribe(inbox, () => {
|
|
counter++;
|
|
});
|
|
|
|
nc.publish(inbox);
|
|
nc.drain((err)=> {
|
|
if(err) {
|
|
t.log(err);
|
|
}
|
|
t.log('connection is closed:', nc.closed);
|
|
t.log('processed', counter, 'messages');
|
|
t.pass();
|
|
// the snippet is running as a promise in a test
|
|
// and calls resolve to pass the test
|
|
resolve();
|
|
});
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="drain_conn_py_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/asyncio-nats-examples/blob/master/drain_conn.py#L1-34"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-python">import asyncio
|
|
from nats.aio.client import Client as NATS
|
|
|
|
async def example(loop):
|
|
nc = NATS()
|
|
|
|
await nc.connect("nats://127.0.0.1:4222", loop=loop)
|
|
|
|
async def handler(msg):
|
|
print("[Received] ", msg)
|
|
await nc.publish(msg.reply, b'I can help')
|
|
|
|
# Can check whether client is in draining state
|
|
if nc.is_draining:
|
|
print("Connection is draining")
|
|
|
|
await nc.subscribe("help", "workers", cb=handler)
|
|
await nc.flush()
|
|
|
|
requests = []
|
|
for i in range(0, 10):
|
|
request = nc.request("help", b'help!', timeout=1)
|
|
requests.append(request)
|
|
|
|
# Wait for all the responses
|
|
responses = []
|
|
responses = await asyncio.gather(*requests)
|
|
|
|
# Gracefully close the connection.
|
|
await nc.drain()
|
|
|
|
print("Received {} responses".format(len(responses)))
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="drain_conn_ruby_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ruby-nats-examples/blob/master/drain_conn.rb#L3-26"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-ruby">NATS.start(drain_timeout: 1) do |nc|
|
|
NATS.subscribe('foo', queue: "workers") do |msg, reply, sub|
|
|
nc.publish(reply, "ACK:#{msg}")
|
|
end
|
|
|
|
NATS.subscribe('bar', queue: "workers") do |msg, reply, sub|
|
|
nc.publish(reply, "ACK:#{msg}")
|
|
end
|
|
|
|
NATS.subscribe('quux', queue: "workers") do |msg, reply, sub|
|
|
nc.publish(reply, "ACK:#{msg}")
|
|
end
|
|
|
|
EM.add_timer(2) do
|
|
next if NATS.draining?
|
|
|
|
# Drain gracefully closes the connection.
|
|
NATS.drain do
|
|
puts "Done draining. Connection is closed."
|
|
end
|
|
end
|
|
end
|
|
</code></pre>
|
|
</div>
|
|
|
|
<div class="tab__content">
|
|
<pre id="drain_conn_ts_content"><a class="toolbar-icons pull-right" target="_blank" href="https://github.com/nats-io/ts-nats-examples/blob/master/src/connection_samples.ts#L73-83"><i class="mdi mdi-github-circle" title="View on GitHub"></i></a><a class="toolbar-icons pull-right"><i class="mdi mdi-content-copy js-copy" title="Copy to Clipboard"></i></a><span class="copy-msg pull-right"></span><code class="language-javascript">let sub = await nc.subscribe('updates', (err, msg) => {
|
|
t.log('worker got message', msg.data);
|
|
}, {queue: "workers"});
|
|
// [end drain_sub]
|
|
nc.flush();
|
|
|
|
await nc.drain();
|
|
// client must close when the connection drain resolves
|
|
nc.close();
|
|
</code></pre>
|
|
</div>
|
|
|
|
</div>
|