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