2 // Copyright © 2013-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 Publish messages to a STOMP broker.
22 # Publish to a broker with all defaults:
27 # Virtual Host is "localhost"
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
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"
61 ll
= log
.New(os
.Stdout
, "EPUB ", log
.Ldate|log
.Lmicroseconds|log
.Lshortfile
)
65 // Connect to a STOMP broker, publish some messages and disconnect.
70 // Standard example connect sequence
71 n
, conn
, e
:= sngecomm
.CommonConnect(exampid
, tag
, ll
)
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(),
89 e
:= conn
.Send(sh
, mse
)
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(),
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
)
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(),