Embellish single connection multi-goroutine example.
[stompngo_examples.git] / ack / ack.go
blob5d66f9120953662061f161b56597ea5717f7f989
1 //
2 // Copyright © 2011-2013 Guy M. Allard
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 Receive messages from a STOMP broker, and ACK them.
20 Examples:
22 # ACK messages from a broker with all defaults:
23 # Host is "localhost"
24 # Port is 61613
25 # Login is "guest"
26 # Passcode is "guest
27 # Virtual Host is "localhost"
28 # Protocol is 1.1
29 go run ack.go
31 # ACK messages from a broker using STOMP protocol level 1.0:
32 STOMP_PROTOCOL=1.0 go run ack.go
34 # ACK messages from a broker using a custom host and port:
35 STOMP_HOST=tjjackson STOMP_PORT=62613 go run ack.go
37 # ACK messages from a broker using a custom port and virtual host:
38 STOMP_PORT=41613 STOMP_VHOST="/" go run ack.go
40 # ACK messages from a broker using a custom login and passcode:
41 STOMP_LOGIN="userid" STOMP_PASSCODE="t0ps3cr3t" go run ack.go
44 package main
46 import (
47 "fmt"
48 "github.com/gmallard/stompngo"
49 "github.com/gmallard/stompngo_examples/sngecomm"
50 "log"
51 "net"
54 var exampid = "ack: "
56 // Connect to a STOMP broker, receive some messages, ACK them, and disconnect.
57 func main() {
58 fmt.Println(exampid + "starts ...")
60 // Set up the connection.
61 h, p := sngecomm.HostAndPort()
62 n, e := net.Dial("tcp", net.JoinHostPort(h, p))
63 if e != nil {
64 log.Fatalln(e) // Handle this ......
66 fmt.Println(exampid + "dial complete ...")
67 ch := sngecomm.ConnectHeaders()
68 conn, e := stompngo.Connect(n, ch)
69 if e != nil {
70 log.Fatalln(e) // Handle this ......
72 fmt.Println(exampid+"stomp connect complete ...", conn.Protocol())
74 // *NOTE* your application functionaltiy goes here!
75 // With Stomp, you must SUBSCRIBE to a destination in order to receive.
76 // Subscribe returns a channel of MessageData struct.
77 // Here we use a common utility routine to handle the differing subscribe
78 // requirements of each protocol level.
79 d := sngecomm.Dest()
80 id := stompngo.Uuid()
81 r := sngecomm.Subscribe(conn, d, id, "client")
82 fmt.Println(exampid + "stomp subscribe complete ...")
83 // Read data from the returned channel
84 for i := 1; i <= sngecomm.Nmsgs(); i++ {
85 m := <-r
86 fmt.Println(exampid + "channel read complete ...")
87 // MessageData has two components:
88 // a) a Message struct
89 // b) an Error value. Check the error value as usual
90 if m.Error != nil {
91 log.Fatalln(m.Error) // Handle this
94 fmt.Printf("Frame Type: %s\n", m.Message.Command) // Will be MESSAGE or ERROR!
95 if m.Message.Command != stompngo.MESSAGE {
96 log.Fatalln(m) // Handle this ...
98 h := m.Message.Headers
99 for j := 0; j < len(h)-1; j += 2 {
100 fmt.Printf("Header: %s:%s\n", h[j], h[j+1])
102 fmt.Printf("Payload: %s\n", string(m.Message.Body)) // Data payload
103 // ACK the message just received.
104 // Agiain we use a utility routine to handle the different requirements
105 // of the protocol versions.
106 sngecomm.Ack(conn, m.Message.Headers, id)
107 fmt.Println(exampid + "ACK complete ...")
109 // It is polite to unsubscribe, although unnecessary if a disconnect follows.
110 // Again we use a utility routine to handle the different protocol level
111 // requirements.
112 sngecomm.Unsubscribe(conn, d, id)
113 fmt.Println(exampid + "stomp unsubscribe complete ...")
115 // Disconnect from the Stomp server
116 e = conn.Disconnect(stompngo.Headers{})
117 if e != nil {
118 log.Fatalln(e) // Handle this ......
120 fmt.Println(exampid + "stomp disconnect complete ...")
121 // Close the network connection
122 e = n.Close()
123 if e != nil {
124 log.Fatalln(e) // Handle this ......
126 fmt.Println(exampid + "network close complete ...")
128 fmt.Println(exampid + "ends ...")