Add ability to print a limited part of the payload.
[stompngo_examples.git] / publish / publish.go
bloba8c53641976b8d9f8009dbb580cc56f1850ceb66
1 //
2 // Copyright © 2013-2014 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 Publish messages to a STOMP broker.
20 Examples:
22 # Publish to a broker with all defaults:
23 # Host is "localhost"
24 # Port is 61613
25 # Login is "guest"
26 # Passcode is "guest
27 # Virtual Host is "localhost"
28 # Protocol is 1.1
29 go run publish.go
31 # Publish to a broker using STOMP protocol level 1.0:
32 STOMP_PROTOCOL=1.0 go run publish.go
34 # Publish to a broker using a custom host and port:
35 STOMP_HOST=tjjackson STOMP_PORT=62613 go run publish.go
37 # Publish to a broker using a custom port and virtual host:
38 STOMP_PORT=41613 STOMP_VHOST="/" go run publish.go
40 # Publish to a broker using a custom login and passcode:
41 STOMP_LOGIN="userid" STOMP_PASSCODE="t0ps3cr3t" go run publish.go
44 package main
46 import (
47 "fmt"
48 "log"
49 "net"
50 "time"
52 "github.com/gmallard/stompngo"
53 "github.com/gmallard/stompngo_examples/sngecomm"
56 var exampid = "publish: "
58 // Connect to a STOMP broker, publish some messages and disconnect.
59 func main() {
60 fmt.Println(sngecomm.ExampIdNow(exampid) + "starts ...")
62 // Open a net connection
63 h, p := sngecomm.HostAndPort()
64 n, e := net.Dial("tcp", net.JoinHostPort(h, p))
65 if e != nil {
66 log.Fatalln(e) // Handle this ......
68 fmt.Println(sngecomm.ExampIdNow(exampid) + "dial complete ...")
70 ch := sngecomm.ConnectHeaders()
71 conn, e := stompngo.Connect(n, ch)
72 if e != nil {
73 log.Fatalln(e) // Handle this ......
75 fmt.Println(sngecomm.ExampIdNow(exampid)+"stomp connect complete ...", conn.Protocol())
77 fmt.Println(sngecomm.ExampIdNow(exampid)+"connected headers", conn.ConnectResponse.Headers)
78 // *NOTE* your application functionaltiy goes here!
79 s := stompngo.Headers{"destination", sngecomm.Dest(),
80 "persistent", "true"} // send headers
81 m := exampid + " message: "
82 for i := 1; i <= sngecomm.Nmsgs(); i++ {
83 t := m + fmt.Sprintf("%d", i)
84 fmt.Println(sngecomm.ExampIdNow(exampid), "sending now:", t)
85 e := conn.Send(s, t)
86 if e != nil {
87 log.Fatalln("bad send", e) // Handle this ...
89 fmt.Println(sngecomm.ExampIdNow(exampid), "send complete:", t)
90 // time.Sleep(16 * time.Millisecond)
91 // time.Sleep(1 * time.Millisecond) // DB Behind ~ 4 messages
92 // time.Sleep(64 * time.Millisecond) // DB OK
93 // time.Sleep(125 * time.Millisecond) // DB OK
94 // time.Sleep(250 * time.Millisecond) // DB OK
95 // time.Sleep(500 * time.Millisecond) // DB OK
96 time.Sleep(2 * time.Minute)
99 // Disconnect from the Stomp server
100 e = conn.Disconnect(stompngo.Headers{})
101 if e != nil {
102 log.Fatalln(e) // Handle this ......
104 fmt.Println(sngecomm.ExampIdNow(exampid) + "stomp disconnect complete ...")
105 // Close the network connection
106 e = n.Close()
107 if e != nil {
108 log.Fatalln(e) // Handle this ......
110 fmt.Println(sngecomm.ExampIdNow(exampid) + "network close complete ...")
112 fmt.Println(sngecomm.ExampIdNow(exampid) + "ends ...")