2 // Copyright © 2013-2014 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 "github.com/gmallard/stompngo_examples/sngecomm"
56 var exampid
= "publish: "
58 // Connect to a STOMP broker, publish some messages and disconnect.
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
))
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
)
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
)
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
{})
102 log
.Fatalln(e
) // Handle this ......
104 fmt
.Println(sngecomm
.ExampIdNow(exampid
) + "stomp disconnect complete ...")
105 // Close the network connection
108 log
.Fatalln(e
) // Handle this ......
110 fmt
.Println(sngecomm
.ExampIdNow(exampid
) + "network close complete ...")
112 fmt
.Println(sngecomm
.ExampIdNow(exampid
) + "ends ...")