Merge branch 'dev', Version 1.0.5:
[stompngo_examples.git] / publish / publish.go
blob039b2aedf0d949e0fb6cace17d961399593154e3
1 //
2 // Copyright © 2013-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 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 "os"
50 "time"
52 "github.com/gmallard/stompngo"
53 // senv methods could be used in general by stompngo clients.
54 "github.com/gmallard/stompngo/senv"
55 // sngecomm methods are used specifically for these example clients.
56 "github.com/gmallard/stompngo_examples/sngecomm"
59 var (
60 exampid = "publish: "
61 ll = log.New(os.Stdout, "EPUB ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
62 tag = "pubmain"
65 // Connect to a STOMP broker, publish some messages and disconnect.
66 func main() {
68 st := time.Now()
70 // Standard example connect sequence
71 n, conn, e := sngecomm.CommonConnect(exampid, tag, ll)
72 if e != nil {
73 ll.Fatalf("%stag:%s connsess:%s main_on_connect error:%v",
74 exampid, tag, sngecomm.Lcs,
75 e.Error()) // Handle this ......
78 // *NOTE* application specific functionaltiy starts here!
79 sh := stompngo.Headers{"destination", senv.Dest()}
80 if senv.Persistent() {
81 sh = sh.Add("persistent", "true")
83 ms := exampid + "message: "
84 for i := 1; i <= senv.Nmsgs(); i++ {
85 mse := ms + fmt.Sprintf("%d", i)
86 ll.Printf("%stag:%s connsess:%s main_sending mse:~%s~\n",
87 exampid, tag, conn.Session(),
88 mse)
89 e := conn.Send(sh, mse)
90 if e != nil {
91 ll.Fatalf("%stag:%s connsess:%s main_on_connect error:%v",
92 exampid, tag, conn.Session(),
93 e.Error()) // Handle this ......
95 ll.Printf("%stag:%s connsess:%s main_send_complete mse:~%s~\n",
96 exampid, tag, conn.Session(),
97 mse)
98 time.Sleep(100 * time.Millisecond)
100 // *NOTE* application specific functionaltiy ends here!
102 // Standard example disconnect sequence
103 e = sngecomm.CommonDisconnect(n, conn, exampid, tag, ll)
104 if e != nil {
105 ll.Fatalf("%stag:%s connsess:%s main_on_disconnect error:%v",
106 exampid, tag, conn.Session(),
107 e.Error()) // Handle this ......
110 ll.Printf("%stag:%s connsess:%s main_elapsed:%v\n",
111 exampid, tag, conn.Session(),
112 time.Now().Sub(st))