Copyright date updates.
[stompngo_examples.git] / jinterop / activemq / gorecv / gorecv.go
blob24334bacb30d39dc5aebd166b96fffa60f4ac485
1 //
2 // Copyright © 2011-2018 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 A message receiver, to demonstrate JMS interoperability.
20 package main
22 import (
23 "log"
24 "os"
25 "time"
27 "github.com/gmallard/stompngo"
28 // sngecomm methods are used specifically for these example clients.
29 "github.com/gmallard/stompngo_examples/sngecomm"
32 var (
33 exampid = "gorecv: "
34 ll = log.New(os.Stdout, "GOJRCV ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
35 nmsgs = 1
36 tag = "jintamqgr"
39 // Connect to a STOMP broker, receive some messages and disconnect.
40 func main() {
42 st := time.Now()
44 // Standard example connect sequence
45 // Use AMQ port here
46 n, conn, e := sngecomm.CommonConnect(exampid, tag, ll)
47 if e != nil {
48 ll.Fatalf("%stag:%s connsess:%s main_on_connect error:%v",
49 exampid, tag, sngecomm.Lcs,
50 e.Error()) // Handle this ......
53 pbc := sngecomm.Pbc() // Print byte count
55 // Setup Headers ...
56 id := stompngo.Uuid() // Use package convenience function for unique ID
57 d := "/queue/allards.queue"
58 sc := sngecomm.HandleSubscribe(conn, d, id, "auto")
59 ll.Printf("%stag:%s connsess:%s stomp_subscribe_complete\n",
60 exampid, tag, conn.Session())
63 var md stompngo.MessageData
64 // Read data from the returned channel
65 for i := 1; i <= nmsgs; i++ {
66 select {
67 case md = <-sc:
68 case md = <-conn.MessageData:
69 // Frames RECEIPT or ERROR not expected here
70 ll.Fatalf("%stag:%s connsess:%s bad_frame error:%v",
71 exampid, tag, conn.Session(),
72 e.Error()) // Handle this ......
74 ll.Printf("%stag:%s connsess:%s channel_read_complete\n",
75 exampid, tag, conn.Session())
76 // MessageData has two components:
77 // a) a Message struct
78 // b) an Error value. Check the error value as usual
79 if md.Error != nil {
80 ll.Fatalf("%stag:%s connsess:%s error_read error:%v",
81 exampid, tag, conn.Session(),
82 e.Error()) // Handle this ......
84 ll.Printf("%stag:%s connsess:%s frame_type cmd:%s\n",
85 exampid, tag, conn.Session(),
86 md.Message.Command)
88 if md.Message.Command != stompngo.MESSAGE {
89 ll.Fatalf("%stag:%s connsess:%s error_frame_type error:%v",
90 exampid, tag, conn.Session(),
91 e.Error()) // Handle this ......
93 wh := md.Message.Headers
94 for j := 0; j < len(wh)-1; j += 2 {
95 ll.Printf("%stag:%s connsess:%s Header:%s:%s\n",
96 exampid, tag, conn.Session(),
97 wh[j], wh[j+1])
99 if pbc > 0 {
100 maxlen := pbc
101 if len(md.Message.Body) < maxlen {
102 maxlen = len(md.Message.Body)
104 ss := string(md.Message.Body[0:maxlen])
105 ll.Printf("%stag:%s connsess:%s payload body:%s\n",
106 exampid, tag, conn.Session(),
112 sngecomm.HandleUnsubscribe(conn, d, id)
113 ll.Printf("%stag:%s connsess:%s unsubscribe_complete\n",
114 exampid, tag, conn.Session())
116 // Standard example disconnect sequence
117 e = sngecomm.CommonDisconnect(n, conn, exampid, tag, ll)
118 if e != nil {
119 ll.Fatalf("%stag:%s connsess:%s main_disconnect error:%v",
120 exampid, tag, conn.Session(),
121 e.Error()) // Handle this ......
124 ll.Printf("%stag:%s connsess:%s main_elapsed:%v\n",
125 exampid, tag, conn.Session(),
126 time.Now().Sub(st))