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
)
62 // Connect to a STOMP broker, receive some messages, ACK them, and disconnect.
64 ll
.Printf("%s v1:%v\n", exampid
, "starts_...")
66 // Set up the connection.
67 h
, p
:= senv
.HostAndPort()
68 hap
:= net
.JoinHostPort(h
, p
)
69 n
, e
:= net
.Dial("tcp", hap
)
71 ll
.Fatalf("%s %s\n", exampid
, e
.Error()) // Handle this ......
73 ll
.Printf("%s v1:%v v2:%v\n", exampid
, "dial complete ...", hap
)
74 ch
:= sngecomm
.ConnectHeaders()
75 conn
, e
:= stompngo
.Connect(n
, ch
)
77 ll
.Fatalf("%s %s\n", exampid
, e
.Error()) // Handle this ......
79 ll
.Printf("%s v1:%v v2:%v\n", exampid
, "stomp connect complete ...", conn
.Protocol())
81 pbc
:= sngecomm
.Pbc() // Print byte count
83 // *NOTE* your application functionaltiy goes here!
84 // With Stomp, you must SUBSCRIBE to a destination in order to receive.
85 // Subscribe returns a channel of MessageData struct.
86 // Here we use a common utility routine to handle the differing subscribe
87 // requirements of each protocol level.
90 sc
:= sngecomm
.HandleSubscribe(conn
, d
, id
, "client")
91 ll
.Printf("%s v1:%v\n", exampid
, "stomp_subscribe_complete_...")
92 // Read data from the returned channel
93 var md stompngo
.MessageData
94 for i
:= 1; i
<= senv
.Nmsgs(); i
++ {
98 case md
= <-conn
.MessageData
:
99 // Frames RECEIPT or ERROR not expected here
100 ll
.Fatalf("%s v1:%v\n", exampid
, md
) // Handle this
103 ll
.Printf("%s v1:%v\n", exampid
, "channel_read_complete_...")
104 // MessageData has two components:
105 // a) a Message struct
106 // b) an Error value. Check the error value as usual
108 ll
.Fatalf("%s f4v:%v\n", exampid
, md
.Error
) // Handle this
111 ll
.Printf("Frame Type: %s\n", md
.Message
.Command
) // Will be MESSAGE or ERROR!
112 if md
.Message
.Command
!= stompngo
.MESSAGE
{
113 ll
.Fatalf("%s f4v:%v\n", exampid
, md
) // Handle this ...
115 wh
:= md
.Message
.Headers
116 for j
:= 0; j
< len(wh
)-1; j
+= 2 {
117 ll
.Printf("Header: %s:%s\n", wh
[j
], wh
[j
+1])
121 if len(md
.Message
.Body
) < maxlen
{
122 maxlen
= len(md
.Message
.Body
)
124 ss
:= string(md
.Message
.Body
[0:maxlen
])
125 ll
.Printf("Payload: %s\n", ss
) // Data payload
127 // ACK the message just received.
128 // Agiain we use a utility routine to handle the different requirements
129 // of the protocol versions.
130 sngecomm
.HandleAck(conn
, md
.Message
.Headers
, id
)
131 ll
.Printf("%s v1:%v\n", exampid
, "ACK_complete_...")
133 // It is polite to unsubscribe, although unnecessary if a disconnect follows.
134 // Again we use a utility routine to handle the different protocol level
136 sngecomm
.HandleUnsubscribe(conn
, d
, id
)
137 ll
.Printf("%s v1:%v\n", exampid
, "stomp_unsubscribe_complete_...")
139 // Disconnect from the Stomp server
140 e
= conn
.Disconnect(stompngo
.Headers
{})
142 ll
.Fatalf("%s %s\n", exampid
, e
.Error()) // Handle this ......
144 ll
.Printf("%s v1:%v\n", exampid
, "stomp_disconnect_complete_...")
145 // Close the network connection
148 ll
.Fatalf("%s %s\n", exampid
, e
.Error()) // Handle this ......
150 ll
.Printf("%s v1:%v\n", exampid
, "network_close_complete_...")
152 ll
.Printf("%s v1:%v\n", exampid
, "ends_...")