Skip to content

Commit

Permalink
Revert "Remove examples"
Browse files Browse the repository at this point in the history
This reverts commit 6a93438.
  • Loading branch information
krobertson committed May 31, 2013
1 parent 7214a7e commit 21172c9
Show file tree
Hide file tree
Showing 3 changed files with 314 additions and 0 deletions.
212 changes: 212 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package nats_test

import (
"fmt"
"github.com/apcera/nats"
"time"
)

// Shows 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 for 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)
}

func ExampleConn_Close() {
nc, _ := nats.Connect(nats.DefaultURL)
nc.Close()
}

// Shows how to wrap a Conn into an EncodedConn
func ExampleNewEncodedConn() {
nc, _ := nats.Connect(nats.DefaultURL)
c, _ := nats.NewEncodedConn(nc, "json")
c.Close()
}

// EncodedConn can publish virtually anything just
// by passing it in. The encoder will be used to properly
// encode the raw Go type
func ExampleEncodedConn_Publish() {
nc, _ := nats.Connect(nats.DefaultURL)
c, _ := nats.NewEncodedConn(nc, "json")
defer c.Close()

type person struct {
Name string
Address string
Age int
}

me := &person{Name: "derek", Age: 22, Address: "85 Second St"}
c.Publish("hello", me)
}

// EncodedConn's subscribers will automatically decode the
// wire data into the requested Go type using the Decode()
// method of the registered Encoder. The callback signature
// can also vary to include additional data, such as subject
// and reply subjects.
func ExampleEncodedConn_Subscribe() {
nc, _ := nats.Connect(nats.DefaultURL)
c, _ := nats.NewEncodedConn(nc, "json")
defer c.Close()

type person struct {
Name string
Address string
Age int
}

c.Subscribe("hello", func(p *person) {
fmt.Printf("Received a person! %+v\n", p)
})

c.Subscribe("hello", func(subj, reply string, p *person) {
fmt.Printf("Received a person on subject %s! %+v\n", subj, p)
})

me := &person{Name: "derek", Age: 22, Address: "85 Second St"}
c.Publish("hello", me)
}
44 changes: 44 additions & 0 deletions examples/nats-pub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2012 Apcera Inc. All rights reserved.
// +build ignore

package main

import (
"flag"
"github.com/apcera/nats"
"log"
)

func usage() {
log.Fatalf("Usage: nats-pub [-s server] [--ssl] [-t] <subject> <msg> \n")
}

func main() {
var url = flag.String("s", nats.DefaultURL, "The nats server URL")
var ssl = flag.Bool("ssl", false, "Use Secure Connection")

log.SetFlags(0)
flag.Usage = usage
flag.Parse()

args := flag.Args()
if len(args) < 1 {
usage()
}

opts := nats.DefaultOptions
opts.Url = *url
opts.Secure = *ssl

nc, err := opts.Connect()
if err != nil {
log.Fatalf("Can't connect: %v\n", err)
}

subj, msg := args[0], []byte(args[1])

nc.Publish(subj, msg)
nc.Close()

log.Printf("Published [%s] : '%s'\n", subj, msg)
}
58 changes: 58 additions & 0 deletions examples/nats-sub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2012 Apcera Inc. All rights reserved.
// +build ignore

package main

import (
"flag"
"github.com/apcera/nats"
"log"
"runtime"
)

func usage() {
log.Fatalf("Usage: nats-sub [-s server] [--ssl] [-t] <subject> \n")
}

var index = 0

func printMsg(m *nats.Msg, i int) {
index += 1
log.Printf("[#%d] Received on [%s]: '%s'\n", i, m.Subject, string(m.Data))
}

func main() {
var url = flag.String("s", nats.DefaultURL, "The nats server URL")
var showTime = flag.Bool("t", false, "Display timestamps")
var ssl = flag.Bool("ssl", false, "Use Secure Connection")

log.SetFlags(0)
flag.Usage = usage
flag.Parse()

args := flag.Args()
if len(args) < 1 {
usage()
}

opts := nats.DefaultOptions
opts.Url = *url
opts.Secure = *ssl

nc, err := opts.Connect()
if err != nil {
log.Fatalf("Can't connect: %v\n", err)
}

subj, i := args[0], 0

nc.Subscribe(subj, func(msg *nats.Msg) {
i += 1
printMsg(msg, i)
})

log.Printf("Listening on [%s]\n", subj)
if *showTime { log.SetFlags(log.LstdFlags) }

runtime.Goexit()
}

0 comments on commit 21172c9

Please sign in to comment.