From df8c86b69a4f2964ef92615bcc0c42b063ad0052 Mon Sep 17 00:00:00 2001 From: gmallard Date: Fri, 9 Nov 2012 13:35:20 -0500 Subject: [PATCH] Add Stomp 1.2 examples, ACK examples, make minor corrections. --- ack_10/ack_10.go | 122 +++++++++++++++++++++++++++++++++ ack_11/ack_11.go | 141 +++++++++++++++++++++++++++++++++++++++ ack_12/ack_12.go | 140 ++++++++++++++++++++++++++++++++++++++ receive_10/receive_10.go | 2 +- receive_11/receive_11.go | 9 +-- receive_12/receive_12.go | 123 ++++++++++++++++++++++++++++++++++ send_10/send_10.go | 2 +- send_11/send_11.go | 9 +-- send_12/send_12.go | 79 ++++++++++++++++++++++ sngecomm/sngecomm.go | 16 +++++ 10 files changed, 633 insertions(+), 10 deletions(-) create mode 100644 ack_10/ack_10.go create mode 100644 ack_11/ack_11.go create mode 100644 ack_12/ack_12.go create mode 100644 receive_12/receive_12.go create mode 100644 send_12/send_12.go diff --git a/ack_10/ack_10.go b/ack_10/ack_10.go new file mode 100644 index 0000000..cd065c8 --- /dev/null +++ b/ack_10/ack_10.go @@ -0,0 +1,122 @@ +// +// Copyright © 2011-2012 Guy M. Allard +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +/* +Receive messages from a STOMP 1.0 broker, and ACK them. +*/ +package main + +import ( + "fmt" + "github.com/gmallard/stompngo" + "github.com/gmallard/stompngo_examples/sngecomm" + "log" + "net" +) + +var exampid = "ack_10: " + +// Connect to a STOMP 1.0 broker, receive some messages and disconnect. +func main() { + fmt.Println(exampid + "starts ...") + + // Set up the connection. + h, p := sngecomm.HostAndPort10() + n, e := net.Dial("tcp", net.JoinHostPort(h, p)) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "dial complete ...") + eh := stompngo.Headers{} + conn, e := stompngo.Connect(n, eh) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "stomp connect complete ...", conn.Protocol()) + + // Setup Headers ... + uh := stompngo.Headers{"destination", sngecomm.Dest()} // unsubscribe headers + s := stompngo.Headers{"destination", sngecomm.Dest(), // subscribe headers + "ack", "client"} + + // *NOTE* your application functionaltiy goes here! + // With Stomp, you must SUBSCRIBE to a destination in order to receive. + // Stomp 1.0 allows subscribing without a unique subscription id, and we + // do that here. + // Subscribe returns: + // a) A channel of MessageData struct + // b) A possible error. Always check for errors. They can be logical + // errors detected by the stompngo package, or even hard network errors, for + // example the broker just crashed. + r, e := conn.Subscribe(s) + if e != nil { + log.Fatalln(e) // Handle this ... + } + fmt.Println(exampid + "stomp subscribe complete ...") + // Read data from the returned channel + for i := 1; i <= sngecomm.Nmsgs(); i++ { + m := <-r + fmt.Println(exampid + "channel read complete ...") + // MessageData has two components: + // a) a Message struct + // b) an Error value. Check the error value as usual + if m.Error != nil { + log.Fatalln(m.Error) // Handle this + } + // + fmt.Printf("Frame Type: %s\n", m.Message.Command) // Will be MESSAGE or ERROR! + h := m.Message.Headers + for j := 0; j < len(h)-1; j += 2 { + fmt.Printf("Header: %s:%s\n", h[j], h[j+1]) + } + fmt.Printf("Payload: %s\n", string(m.Message.Body)) // Data payload + // ACK the message just received. + ah := stompngo.Headers{"message-id", m.Message.Headers.Value("message-id")} + fmt.Println(exampid, "ACK Headers", ah) + e := conn.Ack(ah) + if e != nil { + log.Fatalln(e) // Handle this + } + // Spurious ERROR frame? + select { + case m = <-r: + log.Fatalln("RECEIVE not expected, got: [%v]\n", m) + default: + } + fmt.Println(exampid + "ACK complete ...") + } + // It is polite to unsubscribe, although unnecessary if a disconnect follows. + e = conn.Unsubscribe(uh) + if e != nil { + log.Fatalln(e) // Handle this ... + } + fmt.Println(exampid + "stomp unsubscribe complete ...") + + // Disconnect from the Stomp server + e = conn.Disconnect(eh) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "stomp disconnect complete ...") + // Close the network connection + e = n.Close() + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "network close complete ...") + + fmt.Println(exampid + "ends ...") +} diff --git a/ack_11/ack_11.go b/ack_11/ack_11.go new file mode 100644 index 0000000..4e36588 --- /dev/null +++ b/ack_11/ack_11.go @@ -0,0 +1,141 @@ +// +// Copyright © 2011-2012 Guy M. Allard +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +/* +Receive messages from a STOMP 1.1 broker, and ACK them. +*/ +package main + +import ( + "fmt" + "github.com/gmallard/stompngo" + "github.com/gmallard/stompngo_examples/sngecomm" + "log" + "net" +) + +var exampid = "ack_11: " + +// Connect to a STOMP 1.1 broker, receive some messages and disconnect. +func main() { + fmt.Println(exampid + "starts ...") + + // Set up the connection. + h, p := sngecomm.HostAndPort11() // A 1.1 connection + n, e := net.Dial("tcp", net.JoinHostPort(h, p)) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "dial complete ...") + ch := stompngo.Headers{"accept-version", "1.1", + "host", h} + conn, e := stompngo.Connect(n, ch) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "stomp connect complete ...", conn.Protocol()) + + // Setup Headers ... + u := stompngo.Uuid() // Use package convenience function for unique ID + uh := stompngo.Headers{"destination", sngecomm.Dest(), + "id", u} // unsubscribe headers + s := stompngo.Headers{"destination", sngecomm.Dest(), + "id", u, "ack", "client"} // subscribe headers + + // *NOTE* your application functionaltiy goes here! + // With Stomp, you must SUBSCRIBE to a destination in order to receive. + // Stomp 1.1 demands subscribing with a unique subscription id, and we + // do that here. + // Subscribe returns: + // a) A channel of MessageData struct. Note that with this package, the + // channel is unique (based on the unique subscription id). + // b) A possible error. Always check for errors. They can be logical + // errors detected by the stompngo package, or even hard network errors, for + // example the broker just crashed. + r, e := conn.Subscribe(s) + if e != nil { + log.Fatalln(e) // Handle this ... + } + fmt.Println(exampid + "stomp subscribe complete ...") + // Read data from the returned channel + for i := 1; i <= sngecomm.Nmsgs(); i++ { + m := <-r + fmt.Println(exampid + "channel read complete ...") + // MessageData has two components: + // a) a Message struct + // b) an Error value. Check the error value as usual + if m.Error != nil { + log.Fatalln(m.Error) // Handle this + } + // + fmt.Printf("Frame Type: %s\n", m.Message.Command) // Will be MESSAGE or ERROR! + h := m.Message.Headers + for j := 0; j < len(h)-1; j += 2 { + fmt.Printf("Header: %s:%s\n", h[j], h[j+1]) + } + fmt.Printf("Payload: %s\n", string(m.Message.Body)) // Data payload + // One item to note: Stomp 1.1 servers _must_ return a 'subscription' + // header in each message, where the value of the 'subscription' header + // matches the unique subscription id supplied on subscribe. With _some_ + // stomp client libraries, this allows you to categorize messages by + // 'subscription'. That is not required with this package!!! This + // because of the unique MessageData channels returned by Subscribe. + // But check that this is the case for demonstration purposes. + if h.Value("subscription") != u { + fmt.Printf("Error condition, expected [%s], received [%s]\n", u, h.Value("subscription")) + log.Fatalln("Bad subscription header") + } + // ACK the message just received. + ah := stompngo.Headers{"message-id", m.Message.Headers.Value("message-id"), + "subscription", u} // 1.1 ACK headers + fmt.Println(exampid, "ACK Headers", ah) + e := conn.Ack(ah) + if e != nil { + log.Fatalln(e) // Handle this + } + // Spurious ERROR frame? + select { + case m = <-r: + log.Fatalln("RECEIVE not expected, got: [%v]\n", m) + default: + } + fmt.Println(exampid + "ACK complete ...") + + } + // It is polite to unsubscribe, although unnecessary if a disconnect follows. + // With Stomp 1.1, the same unique ID is required on UNSUBSCRIBE. Failure + // to provide it will result in an error return. + e = conn.Unsubscribe(uh) + if e != nil { + log.Fatalln(e) // Handle this ... + } + fmt.Println(exampid + "stomp unsubscribe complete ...") + + // Disconnect from the Stomp server + e = conn.Disconnect(stompngo.Headers{}) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "stomp disconnect complete ...") + // Close the network connection + e = n.Close() + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "network close complete ...") + + fmt.Println(exampid + "ends ...") +} diff --git a/ack_12/ack_12.go b/ack_12/ack_12.go new file mode 100644 index 0000000..40f93ba --- /dev/null +++ b/ack_12/ack_12.go @@ -0,0 +1,140 @@ +// +// Copyright © 2011-2012 Guy M. Allard +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +/* +Receive messages from a STOMP 1.2 broker, and ACK them. +*/ +package main + +import ( + "fmt" + "github.com/gmallard/stompngo" + "github.com/gmallard/stompngo_examples/sngecomm" + "log" + "net" +) + +var exampid = "ack_12: " + +// Connect to a STOMP 1.2 broker, receive some messages and disconnect. +func main() { + fmt.Println(exampid + "starts ...") + + // Set up the connection. + h, p := sngecomm.HostAndPort12() // A 1.2 connection + n, e := net.Dial("tcp", net.JoinHostPort(h, p)) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "dial complete ...") + ch := stompngo.Headers{"accept-version", "1.2", + "host", h} + conn, e := stompngo.Connect(n, ch) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "stomp connect complete ...", conn.Protocol()) + + // Setup Headers ... + u := stompngo.Uuid() // Use package convenience function for unique ID + uh := stompngo.Headers{"destination", sngecomm.Dest(), + "id", u} // unsubscribe headers + s := stompngo.Headers{"destination", sngecomm.Dest(), + "id", u, "ack", "client"} // subscribe headers + + // *NOTE* your application functionaltiy goes here! + // With Stomp, you must SUBSCRIBE to a destination in order to receive. + // Stomp 1.2 demands subscribing with a unique subscription id, and we + // do that here. + // Subscribe returns: + // a) A channel of MessageData struct. Note that with this package, the + // channel is unique (based on the unique subscription id). + // b) A possible error. Always check for errors. They can be logical + // errors detected by the stompngo package, or even hard network errors, for + // example the broker just crashed. + r, e := conn.Subscribe(s) + if e != nil { + log.Fatalln(e) // Handle this ... + } + fmt.Println(exampid + "stomp subscribe complete ...") + // Read data from the returned channel + for i := 1; i <= sngecomm.Nmsgs(); i++ { + m := <-r + fmt.Println(exampid + "channel read complete ...") + // MessageData has two components: + // a) a Message struct + // b) an Error value. Check the error value as usual + if m.Error != nil { + log.Fatalln(m.Error) // Handle this + } + // + fmt.Printf("Frame Type: %s\n", m.Message.Command) // Will be MESSAGE or ERROR! + h := m.Message.Headers + for j := 0; j < len(h)-1; j += 2 { + fmt.Printf("Header: %s:%s\n", h[j], h[j+1]) + } + fmt.Printf("Payload: %s\n", string(m.Message.Body)) // Data payload + // One item to note: Stomp 1.2 servers _must_ return a 'subscription' + // header in each message, where the value of the 'subscription' header + // matches the unique subscription id supplied on subscribe. With _some_ + // stomp client libraries, this allows you to categorize messages by + // 'subscription'. That is not required with this package!!! This + // because of the unique MessageData channels returned by Subscribe. + // But check that this is the case for demonstration purposes. + if h.Value("subscription") != u { + fmt.Printf("Error condition, expected [%s], received [%s]\n", u, h.Value("subscription")) + log.Fatalln("Bad subscription header") + } + // ACK the message just received. + fmt.Println(exampid + "will ACK:", m.Message.Headers.Value("ack")) + ah := stompngo.Headers{"id", m.Message.Headers.Value("ack")} // 1.2 ACK headers + fmt.Println(exampid, "ACK Headers", ah) + e := conn.Ack(ah) + if e != nil { + log.Fatalln(e) // Handle this + } + // Spurious ERROR frame? + select { + case m = <-r: + log.Fatalln("RECEIVE not expected, got: [%v]\n", m) + default: + } + fmt.Println(exampid + "ACK complete ...") + } + // It is polite to unsubscribe, although unnecessary if a disconnect follows. + // With Stomp 1.2, the same unique ID is required on UNSUBSCRIBE. Failure + // to provide it will result in an error return. + e = conn.Unsubscribe(uh) + if e != nil { + log.Fatalln(e) // Handle this ... + } + fmt.Println(exampid + "stomp unsubscribe complete ...") + + // Disconnect from the Stomp server + e = conn.Disconnect(stompngo.Headers{}) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "stomp disconnect complete ...") + // Close the network connection + e = n.Close() + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "network close complete ...") + + fmt.Println(exampid + "ends ...") +} diff --git a/receive_10/receive_10.go b/receive_10/receive_10.go index d03f4d1..78cacf7 100644 --- a/receive_10/receive_10.go +++ b/receive_10/receive_10.go @@ -45,7 +45,7 @@ func main() { if e != nil { log.Fatalln(e) // Handle this ...... } - fmt.Println(exampid + "stomp connect complete ...") + fmt.Println(exampid + "stomp connect complete ...", conn.Protocol()) // Setup Headers ... s := stompngo.Headers{"destination", sngecomm.Dest()} // subscribe/unsubscribe headers diff --git a/receive_11/receive_11.go b/receive_11/receive_11.go index 842e6e3..90e8c4d 100644 --- a/receive_11/receive_11.go +++ b/receive_11/receive_11.go @@ -40,12 +40,13 @@ func main() { log.Fatalln(e) // Handle this ...... } fmt.Println(exampid + "dial complete ...") - eh := stompngo.Headers{} - conn, e := stompngo.Connect(n, eh) + ch := stompngo.Headers{"accept-version", "1.1", + "host", h} + conn, e := stompngo.Connect(n, ch) if e != nil { log.Fatalln(e) // Handle this ...... } - fmt.Println(exampid + "stomp connect complete ...") + fmt.Println(exampid + "stomp connect complete ...", conn.Protocol()) // Setup Headers ... u := stompngo.Uuid() // Use package convenience function for unique ID @@ -106,7 +107,7 @@ func main() { fmt.Println(exampid + "stomp unsubscribe complete ...") // Disconnect from the Stomp server - e = conn.Disconnect(eh) + e = conn.Disconnect(stompngo.Headers{}) if e != nil { log.Fatalln(e) // Handle this ...... } diff --git a/receive_12/receive_12.go b/receive_12/receive_12.go new file mode 100644 index 0000000..97f8a8d --- /dev/null +++ b/receive_12/receive_12.go @@ -0,0 +1,123 @@ +// +// Copyright © 2011-2012 Guy M. Allard +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +/* +Receive messages from a STOMP 1.2 broker. +*/ +package main + +import ( + "fmt" + "github.com/gmallard/stompngo" + "github.com/gmallard/stompngo_examples/sngecomm" + "log" + "net" +) + +var exampid = "receive_12: " + +// Connect to a STOMP 1.2 broker, receive some messages and disconnect. +func main() { + fmt.Println(exampid + "starts ...") + + // Set up the connection. + h, p := sngecomm.HostAndPort12() // A 1.2 connection + n, e := net.Dial("tcp", net.JoinHostPort(h, p)) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "dial complete ...") + ch := stompngo.Headers{"accept-version", "1.2", + "host", h} + conn, e := stompngo.Connect(n, ch) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "stomp connect complete ...", conn.Protocol()) + + // Setup Headers ... + u := stompngo.Uuid() // Use package convenience function for unique ID + s := stompngo.Headers{"destination", sngecomm.Dest(), + "id", u} // subscribe/unsubscribe headers + + // *NOTE* your application functionaltiy goes here! + // With Stomp, you must SUBSCRIBE to a destination in order to receive. + // Stomp 1.2 demands subscribing with a unique subscription id, and we + // do that here. + // Subscribe returns: + // a) A channel of MessageData struct. Note that with this package, the + // channel is unique (based on the unique subscription id). + // b) A possible error. Always check for errors. They can be logical + // errors detected by the stompngo package, or even hard network errors, for + // example the broker just crashed. + r, e := conn.Subscribe(s) + if e != nil { + log.Fatalln(e) // Handle this ... + } + fmt.Println(exampid + "stomp subscribe complete ...") + // Read data from the returned channel + for i := 1; i <= sngecomm.Nmsgs(); i++ { + m := <-r + fmt.Println(exampid + "channel read complete ...") + // MessageData has two components: + // a) a Message struct + // b) an Error value. Check the error value as usual + if m.Error != nil { + log.Fatalln(m.Error) // Handle this + } + // + fmt.Printf("Frame Type: %s\n", m.Message.Command) // Will be MESSAGE or ERROR! + h := m.Message.Headers + for j := 0; j < len(h)-1; j += 2 { + fmt.Printf("Header: %s:%s\n", h[j], h[j+1]) + } + fmt.Printf("Payload: %s\n", string(m.Message.Body)) // Data payload + // One item to note: Stomp 1.2 servers _must_ return a 'subscription' + // header in each message, where the value of the 'subscription' header + // matches the unique subscription id supplied on subscribe. With _some_ + // stomp client libraries, this allows you to categorize messages by + // 'subscription'. That is not required with this package!!! This + // because of the unique MessageData channels returned by Subscribe. + // But check that this is the case for demonstration purposes. + if h.Value("subscription") != u { + fmt.Printf("Error condition, expected [%s], received [%s]\n", u, h.Value("subscription")) + log.Fatalln("Bad subscription header") + } + } + // It is polite to unsubscribe, although unnecessary if a disconnect follows. + // With Stomp 1.2, the same unique ID is required on UNSUBSCRIBE. Failure + // to provide it will result in an error return. + e = conn.Unsubscribe(s) + if e != nil { + log.Fatalln(e) // Handle this ... + } + fmt.Println(exampid + "stomp unsubscribe complete ...") + + // Disconnect from the Stomp server + e = conn.Disconnect(stompngo.Headers{}) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "stomp disconnect complete ...") + // Close the network connection + e = n.Close() + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "network close complete ...") + + fmt.Println(exampid + "ends ...") +} diff --git a/send_10/send_10.go b/send_10/send_10.go index f4d4fc5..2444a81 100644 --- a/send_10/send_10.go +++ b/send_10/send_10.go @@ -46,7 +46,7 @@ func main() { if e != nil { log.Fatalln(e) // Handle this ...... } - fmt.Println(exampid + "stomp connect complete ...") + fmt.Println(exampid + "stomp connect complete ...", conn.Protocol()) // *NOTE* your application functionaltiy goes here! s := stompngo.Headers{"destination", sngecomm.Dest()} // send headers diff --git a/send_11/send_11.go b/send_11/send_11.go index b5b6705..8d05168 100644 --- a/send_11/send_11.go +++ b/send_11/send_11.go @@ -41,12 +41,13 @@ func main() { } fmt.Println(exampid + "dial complete ...") - eh := stompngo.Headers{} - conn, e := stompngo.Connect(n, eh) + ch := stompngo.Headers{"accept-version", "1.1", + "host", h} + conn, e := stompngo.Connect(n, ch) if e != nil { log.Fatalln(e) // Handle this ...... } - fmt.Println(exampid + "stomp connect complete ...") + fmt.Println(exampid + "stomp connect complete ...", conn.Protocol()) // *NOTE* your application functionaltiy goes here! // Sending to a 1.1 broker is usally _exactly_ like sending to a 1.0 broker. @@ -62,7 +63,7 @@ func main() { } // Disconnect from the Stomp server - e = conn.Disconnect(eh) + e = conn.Disconnect(stompngo.Headers{}) if e != nil { log.Fatalln(e) // Handle this ...... } diff --git a/send_12/send_12.go b/send_12/send_12.go new file mode 100644 index 0000000..2a7dc4e --- /dev/null +++ b/send_12/send_12.go @@ -0,0 +1,79 @@ +// +// Copyright © 2011-2012 Guy M. Allard +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +/* +Send messages to a STOMP 1.2 broker. +*/ +package main + +import ( + "fmt" + "github.com/gmallard/stompngo" + "github.com/gmallard/stompngo_examples/sngecomm" + "log" + "net" +) + +var exampid = "send_12: " + +// Connect to a STOMP 1.2 broker, send some messages and disconnect. +func main() { + fmt.Println(exampid + "starts ...") + + // Open a net connection + h, p := sngecomm.HostAndPort12() // a 1.2 connect + n, e := net.Dial("tcp", net.JoinHostPort(h, p)) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "dial complete ...") + + ch := stompngo.Headers{"accept-version", "1.2", + "host", h} + conn, e := stompngo.Connect(n, ch) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "stomp connect complete ...", conn.Protocol()) + + // *NOTE* your application functionaltiy goes here! + // Sending to a 1.2 broker is usally _exactly_ like sending to a 1.0 broker. + s := stompngo.Headers{"destination", sngecomm.Dest()} // send headers + m := exampid + " message: " + for i := 1; i <= sngecomm.Nmsgs(); i++ { + t := m + fmt.Sprintf("%d", i) + e := conn.Send(s, t) + if e != nil { + log.Fatalln(e) // Handle this ... + } + fmt.Println(exampid, "send complete:", t) + } + + // Disconnect from the Stomp server + e = conn.Disconnect(stompngo.Headers{}) + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "stomp disconnect complete ...") + // Close the network connection + e = n.Close() + if e != nil { + log.Fatalln(e) // Handle this ...... + } + fmt.Println(exampid + "network close complete ...") + + fmt.Println(exampid + "ends ...") +} diff --git a/sngecomm/sngecomm.go b/sngecomm/sngecomm.go index 9b6eace..3b37bae 100644 --- a/sngecomm/sngecomm.go +++ b/sngecomm/sngecomm.go @@ -32,6 +32,9 @@ var p10 = "61613" // default 1.0 port (ActiveMQ on the author's machine) var h11 = "localhost" // default 1.1 host var p11 = "62613" // default 1.1 port (Apollo on the author's machine) +var h12 = "localhost" // default 1.2 host +var p12 = "62613" // default 1.2 port (Apollo on the author's machine) + var nmsgs = 1 // Default number of messages to send var dest = "/queue/snge.common.queue" // Default destination var nqs = 1 // Default number of queues for multi-queue demo(s) @@ -62,6 +65,19 @@ func HostAndPort11() (string, string) { return h11, p11 } +// Override 1.2 Host and port for Dial if requested. +func HostAndPort12() (string, string) { + he := os.Getenv("STOMP_HOST") + if he != "" { + h12 = he + } + pe := os.Getenv("STOMP_PORT") + if pe != "" { + p12 = pe + } + return h12, p12 +} + // Number of messages to send func Nmsgs() int { c := os.Getenv("STOMP_NMSGS")