Clenaup after using go vet.
[stompngo_examples.git] / ack / ack.go
blob61315ff95061908e370688ff456624367430a296
1 //
2 // Copyright © 2011-2018 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.2
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 "log"
48 "os"
49 "time"
51 "github.com/gmallard/stompngo"
52 // senv methods could be used in general by stompngo clients.
53 "github.com/gmallard/stompngo/senv"
54 // sngecomm methods are used specifically for these example clients.
55 "github.com/gmallard/stompngo_examples/sngecomm"
58 var (
59 exampid = "ack: "
60 ll = log.New(os.Stdout, "EACK ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
61 tag = "ackmain"
64 // Connect to a STOMP broker, receive some messages, ACK them, and disconnect.
65 func main() {
67 st := time.Now()
69 // Standard example connect sequence
70 n, conn, e := sngecomm.CommonConnect(exampid, tag, ll)
71 if e != nil {
72 ll.Fatalf("%stag:%s connsess:%s main_on_connect error:%v",
73 exampid, tag, sngecomm.Lcs,
74 e.Error()) // Handle this ......
77 pbc := sngecomm.Pbc() // Print byte count
79 // *NOTE* your application functionaltiy goes here!
80 // With Stomp, you must SUBSCRIBE to a destination in order to receive.
81 // Subscribe returns a channel of MessageData struct.
82 // Here we use a common utility routine to handle the differing subscribe
83 // requirements of each protocol level.
84 d := senv.Dest()
85 id := stompngo.Uuid()
86 sc := sngecomm.HandleSubscribe(conn, d, id, "client")
87 ll.Printf("%stag:%s connsess:%s main_subscribe_complete\n",
88 exampid, tag, conn.Session())
89 // Read data from the returned channel
90 var md stompngo.MessageData
91 for i := 1; i <= senv.Nmsgs(); i++ {
93 select {
94 case md = <-sc:
95 case md = <-conn.MessageData:
96 // Frames RECEIPT or ERROR not expected here
97 ll.Fatalf("%stag:%s connsess:%s main_channel_frame error:%v",
98 exampid, tag, conn.Session(),
99 e.Error()) // Handle this ......
102 ll.Printf("%stag:%s connsess:%s main_channel_read_complete\n",
103 exampid, tag, conn.Session())
104 // MessageData has two components:
105 // a) a Message struct
106 // b) an Error value. Check the error value as usual
107 if md.Error != nil {
108 ll.Fatalf("%stag:%s connsess:%s main_channel_read error:%v",
109 exampid, tag, conn.Session(),
110 e.Error()) // Handle this ......
113 ll.Printf("%stag:%s connsess:%s frame_type:%v\n",
114 exampid, tag, conn.Session(),
115 md.Message.Command)
116 if md.Message.Command != stompngo.MESSAGE {
117 ll.Fatalf("%stag:%s connsess:%s bad_frame frame:%v",
118 exampid, tag, conn.Session(),
119 md.Message.Command) // Handle this ......
121 wh := md.Message.Headers
122 for j := 0; j < len(wh)-1; j += 2 {
123 ll.Printf("%stag:%s connsess:%s header:%s:%s\n",
124 exampid, tag, conn.Session(),
125 wh[j], wh[j+1])
127 if pbc > 0 {
128 maxlen := pbc
129 if len(md.Message.Body) < maxlen {
130 maxlen = len(md.Message.Body)
132 ss := string(md.Message.Body[0:maxlen])
133 ll.Printf("%stag:%s connsess:%s message_payload body:%s\n",
134 exampid, tag, conn.Session(),
138 // ACK the message just received.
139 // Agiain we use a utility routine to handle the different requirements
140 // of the protocol versions.
141 sngecomm.HandleAck(conn, md.Message.Headers, id)
142 ll.Printf("%stag:%s connsess:%s ack_complete\n",
143 exampid, tag, conn.Session())
145 // It is polite to unsubscribe, although unnecessary if a disconnect follows.
146 // Again we use a utility routine to handle the different protocol level
147 // requirements.
148 sngecomm.HandleUnsubscribe(conn, d, id)
149 ll.Printf("%stag:%s connsess:%s stomp_unsubscribe_complete\n",
150 exampid, tag, conn.Session())
152 // Standard example disconnect sequence
153 e = sngecomm.CommonDisconnect(n, conn, exampid, tag, ll)
154 if e != nil {
155 ll.Fatalf("%stag:%s connsess:%s disconnect error:%v",
156 exampid, tag, conn.Session(),
157 e.Error()) // Handle this ......
160 ll.Printf("%stag:%s connsess:%s main_elapsed:%v\n",
161 exampid, tag, conn.Session(),
162 time.Now().Sub(st))