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 A message receiver, to demonstrate JMS interoperability.
27 "github.com/gmallard/stompngo"
32 ll
= log
.New(os
.Stdout
, "GOJRCV ", log
.Ldate|log
.Lmicroseconds|log
.Lshortfile
)
36 // Connect to a STOMP 1.2 broker, receive some messages and disconnect.
38 ll
.Println(exampid
+ "starts ...")
40 // Set up the connection.
41 n
, e
:= net
.Dial("tcp", "localhost:61613")
43 ll
.Fatalln(e
) // Handle this ......
45 ll
.Println(exampid
+ "dial complete ...")
46 ch
:= stompngo
.Headers
{"login", "userr", "passcode", "passw0rd",
47 "host", "localhost", "accept-version", "1.2"}
48 conn
, e
:= stompngo
.Connect(n
, ch
)
50 ll
.Fatalln(e
) // Handle this ......
52 ll
.Println(exampid
+ "stomp connect complete ...")
55 id
:= stompngo
.Uuid() // Use package convenience function for unique ID
56 sbh
:= stompngo
.Headers
{"destination", "/queue/allards.queue",
57 "id", id
} // subscribe/unsubscribe headers
60 sc
, e
:= conn
.Subscribe(sbh
)
62 ll
.Fatalln(e
) // Handle this ...
64 ll
.Println(exampid
+ "stomp subscribe complete ...")
66 var md stompngo
.MessageData
67 // Read data from the returned channel
68 for i
:= 1; i
<= nmsgs
; i
++ {
71 case md
= <-conn
.MessageData
:
72 // A RECEIPT or ERROR frame is unexpected here
73 ll
.Fatalln(exampid
, md
) // Handle this
75 ll
.Println(exampid
+ "channel read complete ...")
76 // MessageData has two components:
77 // a) a Message struct
78 // b) an Error value. Check the error value as usual
80 ll
.Fatalln(md
.Error
) // Handle this
83 ll
.Printf("Frame Type: %s\n", md
.Message
.Command
) // Should be MESSAGE
84 wh
:= md
.Message
.Headers
85 for j
:= 0; j
< len(wh
)-1; j
+= 2 {
86 ll
.Printf("Header: %s:%s\n", wh
[j
], wh
[j
+1])
88 ll
.Printf("Payload: %s\n", string(md
.Message
.Body
)) // Data payload
90 // It is polite to unsubscribe, although unnecessary if a disconnect follows.
91 // With Stomp 1.1+, the same unique ID is required on UNSUBSCRIBE. Failure
92 // to provide it will result in an error return.
93 e
= conn
.Unsubscribe(sbh
) // Same headers as Subscribe
95 ll
.Fatalln(e
) // Handle this ...
97 ll
.Println(exampid
+ "stomp unsubscribe complete ...")
99 // Disconnect from the Stomp server
100 dh
:= stompngo
.Headers
{}
101 e
= conn
.Disconnect(dh
)
103 ll
.Fatalln(e
) // Handle this ......
105 ll
.Println(exampid
+ "stomp disconnect complete ...")
106 // Close the network connection
109 ll
.Fatalln(e
) // Handle this ......
111 ll
.Println(exampid
+ "network close complete ...")
113 ll
.Println(exampid
+ "ends ...")