All examples demonstrate proper way to receive messages:
[stompngo_examples.git] / jinterop / gorecv / gorecv.go
blobbb7d51d864a15a095a9cb9808e7b7796b40661aa
1 //
2 // Copyright © 2011-2016 Guy M. Allard
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
20 package main
22 import (
23 "log"
24 "net"
25 "os"
27 "github.com/gmallard/stompngo"
30 var (
31 exampid = "gorecv: "
32 ll = log.New(os.Stdout, "GOJRCV ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
33 nmsgs = 1
36 // Connect to a STOMP 1.2 broker, receive some messages and disconnect.
37 func main() {
38 ll.Println(exampid + "starts ...")
40 // Set up the connection.
41 n, e := net.Dial("tcp", "localhost:61613")
42 if e != nil {
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)
49 if e != nil {
50 ll.Fatalln(e) // Handle this ......
52 ll.Println(exampid + "stomp connect complete ...")
54 // Setup Headers ...
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
59 // Subscribe
60 sc, e := conn.Subscribe(sbh)
61 if e != nil {
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++ {
69 select {
70 case md = <-sc:
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
79 if md.Error != nil {
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
94 if e != nil {
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)
102 if e != nil {
103 ll.Fatalln(e) // Handle this ......
105 ll.Println(exampid + "stomp disconnect complete ...")
106 // Close the network connection
107 e = n.Close()
108 if e != nil {
109 ll.Fatalln(e) // Handle this ......
111 ll.Println(exampid + "network close complete ...")
113 ll.Println(exampid + "ends ...")