A scripted update to formats of log messages:
[stompngo_examples.git] / jinterop / gosend / gosend.go
blobd7b8934e40089e6d00497dbc8ae404e96093684b
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 sender, to demonstrate JMS interoperability.
20 package main
22 import (
23 "fmt"
24 "log"
25 "net"
26 "os"
28 "github.com/gmallard/stompngo"
29 "github.com/gmallard/stompngo/senv"
32 var (
33 exampid = "gosend: "
34 ll = log.New(os.Stdout, "GOJSND ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
35 nmsgs = 1
38 // Connect to a STOMP 1.2 broker, send some messages and disconnect.
39 func main() {
40 ll.Printf("%s v1:%v\n", exampid, "starts_...")
42 // Open a net connection
43 n, e := net.Dial("tcp", "localhost:61613")
44 if e != nil {
45 ll.Fatalf("%s %s\n", exampid, e.Error()) // Handle this ......
47 ll.Printf("%s v1:%v\n", exampid, "dial_complete_...")
49 // Connect to broker
50 ch := stompngo.Headers{"login", "userr", "passcode", "passw0rd",
51 "host", "localhost", "accept-version", "1.2"}
52 conn, e := stompngo.Connect(n, ch)
53 if e != nil {
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)
68 if e != nil {
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)
77 if e != nil {
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
82 e = n.Close()
83 if e != nil {
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_...")