2 // Copyright © 2011-2018 Guy M. Allard
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
22 # ACK messages from a broker with all defaults:
27 # Virtual Host is "localhost"
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
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"
60 ll
= log
.New(os
.Stdout
, "EACK ", log
.Ldate|log
.Lmicroseconds|log
.Lshortfile
)
64 // Connect to a STOMP broker, receive some messages, ACK them, and disconnect.
69 // Standard example connect sequence
70 n
, conn
, e
:= sngecomm
.CommonConnect(exampid
, tag
, ll
)
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.
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
++ {
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
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(),
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(),
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
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
)
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(),