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 sender, to demonstrate JMS interoperability.
28 "github.com/gmallard/stompngo"
29 "github.com/gmallard/stompngo/senv"
34 ll
= log
.New(os
.Stdout
, "GOJSND ", log
.Ldate|log
.Lmicroseconds|log
.Lshortfile
)
38 // Connect to a STOMP 1.2 broker, send some messages and disconnect.
40 ll
.Printf("%s v1:%v\n", exampid
, "starts_...")
42 // Open a net connection
43 n
, e
:= net
.Dial("tcp", "localhost:61613")
45 ll
.Fatalf("%s %s\n", exampid
, e
.Error()) // Handle this ......
47 ll
.Printf("%s v1:%v\n", exampid
, "dial_complete_...")
50 ch
:= stompngo
.Headers
{"login", "userr", "passcode", "passw0rd",
51 "host", "localhost", "accept-version", "1.2"}
52 conn
, e
:= stompngo
.Connect(n
, ch
)
54 ll
.Fatalf("%s %s\n", exampid
, e
.Error()) // Handle this ......
56 ll
.Printf("%s v1:%v\n", exampid
, "stomp_connect_complete_...")
58 // Suppress content length here, so JMS will treat this as a 'text' message.
59 sh
:= stompngo
.Headers
{"destination", "/queue/allards.queue",
60 "suppress-content-length", "true"} // send headers, suppress content-length
61 if senv
.Persistent() {
62 sh
= sh
.Add("persistent", "true")
64 ms
:= exampid
+ " message: "
65 for i
:= 1; i
<= nmsgs
; i
++ {
66 mse
:= ms
+ fmt
.Sprintf("%d", i
)
67 e
:= conn
.Send(sh
, mse
)
69 ll
.Fatalf("%s %s\n", exampid
, e
.Error()) // Handle this ...
71 ll
.Printf("%s v1:%v v2:%v\n", exampid
, "send complete:", mse
)
74 // Disconnect from the Stomp server
75 dh
:= stompngo
.Headers
{}
76 e
= conn
.Disconnect(dh
)
78 ll
.Fatalf("%s %s\n", exampid
, e
.Error()) // Handle this ......
80 ll
.Printf("%s v1:%v\n", exampid
, "stomp_disconnect_complete_...")
81 // Close the network connection
84 ll
.Fatalf("%s %s\n", exampid
, e
.Error()) // Handle this ......
86 ll
.Printf("%s v1:%v\n", exampid
, "network_close_complete_...")
88 ll
.Printf("%s v1:%v\n", exampid
, "ends_...")