2 // Copyright © 2011-2016 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
47 "github.com/gmallard/stompngo"
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"
59 ll
= log
.New(os
.Stdout
, "EACK ", log
.Ldate|log
.Lmicroseconds|log
.Lshortfile
)
63 // Connect to a STOMP broker, receive some messages, ACK them, and disconnect.
68 // Standard example connect sequence
69 n
, conn
, e
:= sngecomm
.CommonConnect(exampid
, tag
, ll
)
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.
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
++ {
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
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(),
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(),
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
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
)
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(),