2 // Copyright © 2011-2013 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 "github.com/gmallard/stompngo_examples/sngecomm"
57 // Connect to a STOMP broker, receive some messages, ACK them, and disconnect.
59 fmt
.Println(exampid
+ "starts ...")
61 // Set up the connection.
62 h
, p
:= sngecomm
.HostAndPort()
63 n
, e
:= net
.Dial("tcp", net
.JoinHostPort(h
, p
))
65 log
.Fatalln(e
) // Handle this ......
67 fmt
.Println(exampid
+ "dial complete ...")
68 ch
:= sngecomm
.ConnectHeaders()
69 conn
, e
:= stompngo
.Connect(n
, ch
)
71 log
.Fatalln(e
) // Handle this ......
73 fmt
.Println(exampid
+"stomp connect complete ...", conn
.Protocol())
75 // *NOTE* your application functionaltiy goes here!
76 // With Stomp, you must SUBSCRIBE to a destination in order to receive.
77 // Subscribe returns a channel of MessageData struct.
78 // Here we use a common utility routine to handle the differing subscribe
79 // requirements of each protocol level.
82 r
:= sngecomm
.Subscribe(conn
, d
, id
, "client")
83 fmt
.Println(exampid
+ "stomp subscribe complete ...")
84 // Read data from the returned channel
85 for i
:= 1; i
<= sngecomm
.Nmsgs(); i
++ {
87 fmt
.Println(exampid
+ "channel read complete ...")
88 // MessageData has two components:
89 // a) a Message struct
90 // b) an Error value. Check the error value as usual
92 log
.Fatalln(m
.Error
) // Handle this
95 fmt
.Printf("Frame Type: %s\n", m
.Message
.Command
) // Will be MESSAGE or ERROR!
96 if m
.Message
.Command
!= stompngo
.MESSAGE
{
97 log
.Fatalln(m
) // Handle this ...
99 h
:= m
.Message
.Headers
100 for j
:= 0; j
< len(h
)-1; j
+= 2 {
101 fmt
.Printf("Header: %s:%s\n", h
[j
], h
[j
+1])
103 fmt
.Printf("Payload: %s\n", string(m
.Message
.Body
)) // Data payload
104 // ACK the message just received.
105 // Agiain we use a utility routine to handle the different requirements
106 // of the protocol versions.
107 sngecomm
.Ack(conn
, m
.Message
.Headers
, id
)
108 fmt
.Println(exampid
+ "ACK complete ...")
110 // It is polite to unsubscribe, although unnecessary if a disconnect follows.
111 // Again we use a utility routine to handle the different protocol level
113 sngecomm
.Unsubscribe(conn
, d
, id
)
114 fmt
.Println(exampid
+ "stomp unsubscribe complete ...")
116 // Disconnect from the Stomp server
117 e
= conn
.Disconnect(stompngo
.Headers
{})
119 log
.Fatalln(e
) // Handle this ......
121 fmt
.Println(exampid
+ "stomp disconnect complete ...")
122 // Close the network connection
125 log
.Fatalln(e
) // Handle this ......
127 fmt
.Println(exampid
+ "network close complete ...")
129 fmt
.Println(exampid
+ "ends ...")