mirror of
https://github.com/nats-io/nats.go.git
synced 2025-09-27 04:46:01 +08:00
161 lines
3.3 KiB
Go
161 lines
3.3 KiB
Go
package nats_test
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
"github.com/apcera/nats"
|
|
)
|
|
|
|
// Show different ways to create a Conn
|
|
func ExampleConnect() {
|
|
|
|
nats.Connect(nats.DefaultURL)
|
|
nats.Connect("nats://derek:secretpassword@nats.apcera.com:421")
|
|
|
|
opts := nats.Options{
|
|
AllowReconnect : true,
|
|
MaxReconnect : 10,
|
|
ReconnectWait : 5 * time.Second,
|
|
Timeout : 1 * time.Second,
|
|
}
|
|
|
|
nc, _ := opts.Connect()
|
|
nc.Close()
|
|
}
|
|
|
|
// This Example shows an asynchronous subscriber.
|
|
func ExampleConn_Subscribe() {
|
|
nc, _ := nats.Connect(nats.DefaultURL)
|
|
defer nc.Close()
|
|
|
|
nc.Subscribe("foo", func(m *nats.Msg) {
|
|
fmt.Printf("Received a message: %s\n", string(m.Data))
|
|
})
|
|
}
|
|
|
|
// This Example shows a synchronous subscriber.
|
|
func ExampleConn_SubscribeSync() {
|
|
nc, _ := nats.Connect(nats.DefaultURL)
|
|
defer nc.Close()
|
|
|
|
sub, _ := nc.SubscribeSync("foo")
|
|
m, err := sub.NextMsg(1*time.Second)
|
|
if err == nil {
|
|
fmt.Printf("Received a message: %s\n", string(m.Data))
|
|
} else {
|
|
fmt.Println("NextMsg timed out.")
|
|
}
|
|
}
|
|
|
|
func ExampleSubscription_NextMsg() {
|
|
nc, _ := nats.Connect(nats.DefaultURL)
|
|
defer nc.Close()
|
|
|
|
sub, _ := nc.SubscribeSync("foo")
|
|
m, err := sub.NextMsg(1*time.Second)
|
|
if err == nil {
|
|
fmt.Printf("Received a message: %s\n", string(m.Data))
|
|
} else {
|
|
fmt.Println("NextMsg timed out.")
|
|
}
|
|
}
|
|
|
|
func ExampleSubscription_Unsubscribe() {
|
|
nc, _ := nats.Connect(nats.DefaultURL)
|
|
defer nc.Close()
|
|
|
|
sub, _ := nc.SubscribeSync("foo")
|
|
// ...
|
|
sub.Unsubscribe()
|
|
}
|
|
|
|
func ExampleConn_Publish() {
|
|
nc, _ := nats.Connect(nats.DefaultURL)
|
|
defer nc.Close()
|
|
|
|
nc.Publish("foo", []byte("Hello World!"))
|
|
}
|
|
|
|
func ExampleConn_PublishMsg() {
|
|
nc, _ := nats.Connect(nats.DefaultURL)
|
|
defer nc.Close()
|
|
|
|
msg := &nats.Msg{Subject:"foo", Reply:"bar", Data:[]byte("Hello World!")}
|
|
nc.PublishMsg(msg)
|
|
}
|
|
|
|
func ExampleConn_Flush() {
|
|
nc, _ := nats.Connect(nats.DefaultURL)
|
|
defer nc.Close()
|
|
|
|
msg := &nats.Msg{Subject:"foo", Reply:"bar", Data:[]byte("Hello World!")}
|
|
for i := 0 ; i < 1000 ; i++ {
|
|
nc.PublishMsg(msg)
|
|
}
|
|
err := nc.flush()
|
|
if err == nil {
|
|
// Everything has been processed by the server for nc *Conn.
|
|
}
|
|
}
|
|
|
|
func ExampleConn_FlushTimeout() {
|
|
nc, _ := nats.Connect(nats.DefaultURL)
|
|
defer nc.Close()
|
|
|
|
msg := &nats.Msg{Subject:"foo", Reply:"bar", Data:[]byte("Hello World!")}
|
|
for i := 0 ; i < 1000 ; i++ {
|
|
nc.PublishMsg(msg)
|
|
}
|
|
// Only wait up to 1 second for Flush
|
|
err := nc.flushTimeout(1*time.Second)
|
|
if err == nil {
|
|
// Everything has been processed by the server for nc *Conn.
|
|
}
|
|
}
|
|
|
|
func ExampleConn_Request() {
|
|
nc, _ := nats.Connect(nats.DefaultURL)
|
|
defer nc.Close()
|
|
|
|
nc.Subscribe("foo", func(m *nats.Msg) {
|
|
nc.Publish(m.Reply, []byte("I will help you"))
|
|
})
|
|
nc.Request("foo", []byte("help"), 50*time.Millisecond)
|
|
}
|
|
|
|
func ExampleConn_QueueSubscribe() {
|
|
nc, _ := nats.Connect(nats.DefaultURL)
|
|
defer nc.Close()
|
|
|
|
received := 0
|
|
|
|
nc.QueueSubscribe("foo", "worker_group", func(_ *nats.Msg) {
|
|
received += 1
|
|
})
|
|
}
|
|
|
|
func ExampleSubscription_AutoUnsubscribe() {
|
|
nc, _ := nats.Connect(nats.DefaultURL)
|
|
defer nc.Close()
|
|
|
|
received, wanted, total := 0, 10, 100
|
|
|
|
sub, _ := nc.Subscribe("foo", func(_ *nats.Msg) {
|
|
received += 1
|
|
})
|
|
sub.AutoUnsubscribe(wanted)
|
|
|
|
for i := 0; i < total; i++ {
|
|
nc.Publish("foo", []byte("Hello"))
|
|
}
|
|
nc.Flush()
|
|
|
|
fmt.Printf("Received = %d", received)
|
|
// Output:
|
|
// Received = 10
|
|
}
|
|
|
|
func ExampleConn_Close() {
|
|
nc, _ := nats.Connect(nats.DefaultURL)
|
|
nc.Close()
|
|
} |