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