Use common import formatting and placement.
[stompngo_examples.git] / ack / ack.go
blob6212c7e95f75abc5640f6bf98cf7e1d8223d9553
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 "log"
49 "net"
51 "github.com/gmallard/stompngo"
52 "github.com/gmallard/stompngo_examples/sngecomm"
55 var exampid = "ack: "
57 // Connect to a STOMP broker, receive some messages, ACK them, and disconnect.
58 func main() {
59 fmt.Println(exampid + "starts ...")
61 // Set up the connection.
62 h, p := sngecomm.HostAndPort()
63 n, e := net.Dial("tcp", net.JoinHostPort(h, p))
64 if e != nil {
65 log.Fatalln(e) // Handle this ......
67 fmt.Println(exampid + "dial complete ...")
68 ch := sngecomm.ConnectHeaders()
69 conn, e := stompngo.Connect(n, ch)
70 if e != nil {
71 log.Fatalln(e) // Handle this ......
73 fmt.Println(exampid+"stomp connect complete ...", conn.Protocol())
75 // *NOTE* your application functionaltiy goes here!
76 // With Stomp, you must SUBSCRIBE to a destination in order to receive.
77 // Subscribe returns a channel of MessageData struct.
78 // Here we use a common utility routine to handle the differing subscribe
79 // requirements of each protocol level.
80 d := sngecomm.Dest()
81 id := stompngo.Uuid()
82 r := sngecomm.Subscribe(conn, d, id, "client")
83 fmt.Println(exampid + "stomp subscribe complete ...")
84 // Read data from the returned channel
85 for i := 1; i <= sngecomm.Nmsgs(); i++ {
86 m := <-r
87 fmt.Println(exampid + "channel read complete ...")
88 // MessageData has two components:
89 // a) a Message struct
90 // b) an Error value. Check the error value as usual
91 if m.Error != nil {
92 log.Fatalln(m.Error) // Handle this
95 fmt.Printf("Frame Type: %s\n", m.Message.Command) // Will be MESSAGE or ERROR!
96 if m.Message.Command != stompngo.MESSAGE {
97 log.Fatalln(m) // Handle this ...
99 h := m.Message.Headers
100 for j := 0; j < len(h)-1; j += 2 {
101 fmt.Printf("Header: %s:%s\n", h[j], h[j+1])
103 fmt.Printf("Payload: %s\n", string(m.Message.Body)) // Data payload
104 // ACK the message just received.
105 // Agiain we use a utility routine to handle the different requirements
106 // of the protocol versions.
107 sngecomm.Ack(conn, m.Message.Headers, id)
108 fmt.Println(exampid + "ACK complete ...")
110 // It is polite to unsubscribe, although unnecessary if a disconnect follows.
111 // Again we use a utility routine to handle the different protocol level
112 // requirements.
113 sngecomm.Unsubscribe(conn, d, id)
114 fmt.Println(exampid + "stomp unsubscribe complete ...")
116 // Disconnect from the Stomp server
117 e = conn.Disconnect(stompngo.Headers{})
118 if e != nil {
119 log.Fatalln(e) // Handle this ......
121 fmt.Println(exampid + "stomp disconnect complete ...")
122 // Close the network connection
123 e = n.Close()
124 if e != nil {
125 log.Fatalln(e) // Handle this ......
127 fmt.Println(exampid + "network close complete ...")
129 fmt.Println(exampid + "ends ...")