2 // Copyright © 2011-2018 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"
28 // sngecomm methods are used specifically for these example clients.
29 "github.com/gmallard/stompngo_examples/sngecomm"
34 ll
= log
.New(os
.Stdout
, "GOJRCV ", log
.Ldate|log
.Lmicroseconds|log
.Lshortfile
)
39 // Connect to a STOMP broker, receive some messages and disconnect.
44 // Standard example connect sequence
46 n
, conn
, e
:= sngecomm
.CommonConnect(exampid
, tag
, ll
)
48 ll
.Fatalf("%stag:%s connsess:%s main_on_connect error:%v",
49 exampid
, tag
, sngecomm
.Lcs
,
50 e
.Error()) // Handle this ......
53 pbc
:= sngecomm
.Pbc() // Print byte count
56 id
:= stompngo
.Uuid() // Use package convenience function for unique ID
57 d
:= "/queue/allards.queue"
58 sc
:= sngecomm
.HandleSubscribe(conn
, d
, id
, "auto")
59 ll
.Printf("%stag:%s connsess:%s stomp_subscribe_complete\n",
60 exampid
, tag
, conn
.Session())
63 var md stompngo
.MessageData
64 // Read data from the returned channel
65 for i
:= 1; i
<= nmsgs
; i
++ {
68 case md
= <-conn
.MessageData
:
69 // Frames RECEIPT or ERROR not expected here
70 ll
.Fatalf("%stag:%s connsess:%s bad_frame error:%v",
71 exampid
, tag
, conn
.Session(),
72 e
.Error()) // Handle this ......
74 ll
.Printf("%stag:%s connsess:%s channel_read_complete\n",
75 exampid
, tag
, conn
.Session())
76 // MessageData has two components:
77 // a) a Message struct
78 // b) an Error value. Check the error value as usual
80 ll
.Fatalf("%stag:%s connsess:%s error_read error:%v",
81 exampid
, tag
, conn
.Session(),
82 e
.Error()) // Handle this ......
84 ll
.Printf("%stag:%s connsess:%s frame_type cmd:%s\n",
85 exampid
, tag
, conn
.Session(),
88 if md
.Message
.Command
!= stompngo
.MESSAGE
{
89 ll
.Fatalf("%stag:%s connsess:%s error_frame_type error:%v",
90 exampid
, tag
, conn
.Session(),
91 e
.Error()) // Handle this ......
93 wh
:= md
.Message
.Headers
94 for j
:= 0; j
< len(wh
)-1; j
+= 2 {
95 ll
.Printf("%stag:%s connsess:%s Header:%s:%s\n",
96 exampid
, tag
, conn
.Session(),
101 if len(md
.Message
.Body
) < maxlen
{
102 maxlen
= len(md
.Message
.Body
)
104 ss
:= string(md
.Message
.Body
[0:maxlen
])
105 ll
.Printf("%stag:%s connsess:%s payload body:%s\n",
106 exampid
, tag
, conn
.Session(),
112 sngecomm
.HandleUnsubscribe(conn
, d
, id
)
113 ll
.Printf("%stag:%s connsess:%s unsubscribe_complete\n",
114 exampid
, tag
, conn
.Session())
116 // Standard example disconnect sequence
117 e
= sngecomm
.CommonDisconnect(n
, conn
, exampid
, tag
, ll
)
119 ll
.Fatalf("%stag:%s connsess:%s main_disconnect error:%v",
120 exampid
, tag
, conn
.Session(),
121 e
.Error()) // Handle this ......
124 ll
.Printf("%stag:%s connsess:%s main_elapsed:%v\n",
125 exampid
, tag
, conn
.Session(),