Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
vetcher committed Mar 22, 2018
1 parent 07d0921 commit 3231a4b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
4 changes: 4 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func (e Exchange) declare(c *Client) {
c.exchanges = append(c.exchanges, e)
}

// TemporaryExchange is a common way to create temporary exchange with given name.
func TemporaryExchange(name string) Exchange {
return Exchange{
Name: name,
Expand All @@ -167,6 +168,7 @@ func TemporaryExchange(name string) Exchange {
}
}

// LongExchange is a common way to declare exchange with given name.
func LongExchange(name string) Exchange {
return Exchange{
Name: name,
Expand All @@ -185,6 +187,7 @@ type Queue struct {
Args amqp.Table
}

// LongExchange is a common way to declare queue with given name.
func LongQueue(name string) Queue {
return Queue{
Name: name,
Expand All @@ -200,6 +203,7 @@ func (q Queue) declare(c *Client) {
c.queues = append(c.queues, q)
}

// Binding is used for bind exchange and queue.
type Binding struct {
Exchange string
Queue string
Expand Down
28 changes: 9 additions & 19 deletions client_example_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package amqp

/*
import (
"context"
"fmt"
Expand All @@ -27,43 +26,34 @@ func Example_common() {
}()
lg := logger.NewChanLogger(ch)

// Change configuration.
queuecfg := DefaultQueueConfig()
queuecfg.AutoDelete = true
client, err := NewClient("amqp://localhost:5672",
WithOptions(
WaitConnection(true, time.Minute),
ApplicationId("example app"),
WarnLogger(lg),
ErrorLogger(lg),
),
WithConnOptions(
conn.WithLogger(lg), // We want to know connection status and errors.
),
SetQueueConfig(queuecfg),
client, err := NewClient(conn.DefaultConnector(
"amqp://localhost:5672",
conn.WithLogger(lg), // We want to know connection status and errors.
),
TemporaryExchange("example-exchange"),
)
if err != nil {
panic(err)
}
eventChan, _ := client.Subscription("example-exchange", Comment{})
subscriber := client.Subscriber()
eventChan := subscriber.SubscribeToExchange(context.Background(), "example-exchange", Comment{}, Consumer{})
go func() {
for event := range eventChan {
fmt.Println(event.Data) // do something with events
}
}()
publisher := client.Publisher()
for i := 0; i < 10; i++ {
// Prepare your data before publishing
comment := Comment{
Id: strconv.Itoa(i),
Message: "message " + strconv.Itoa(i),
}
err := client.Pub(context.Background(), "example-exchange", comment)
err := publisher.Publish(context.Background(), "example-exchange", comment, Publish{})
if err != nil {
panic(err)
}
time.Sleep(time.Millisecond * 500)
}
time.Sleep(time.Second * 5) // wait for delivering all events
}
*/

0 comments on commit 3231a4b

Please sign in to comment.