-
Notifications
You must be signed in to change notification settings - Fork 1
/
stomp.go
96 lines (74 loc) · 1.89 KB
/
stomp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
Simple stomp Client to Send data to ActiveMQ
Default Stomp conf can be overwritten setting
stomp environment variables like:
STOMP_HOST
STOMP_PORT
STOMP_DEST
Author: Rafael Santos <[email protected]>
*/
package StompClient
import (
"github.com/gmallard/stompngo"
"net"
"log"
"os"
)
type StompClient struct {
Host string
Port string
Dest string
NetConnection net.Conn
Connection *stompngo.Connection
}
// Set stomp info
func (g *StompClient) getVars() {
g.Host = "localhost"
g.Port = "61613"
g.Dest = "/queue/events"
// Check stomp environment variables
stomp_host := os.Getenv("STOMP_HOST")
if stomp_host != "" {
g.Host = stomp_host
}
stomp_port := os.Getenv("STOMP_PORT")
if stomp_port != "" {
g.Port = stomp_port
}
stomp_dest := os.Getenv("STOMP_DEST")
if stomp_dest != "" {
g.Dest = stomp_dest
}
}
// Open connection
func (nt *StompClient) netConnection() {
n, err := net.Dial("tcp", net.JoinHostPort(nt.Host, nt.Port))
if err != nil {
log.Fatal(err)
}
nt.NetConnection = n
}
// Connect to stomp
func (conn *StompClient) Connect() (*stompngo.Connection) {
conn.getVars()
conn.netConnection()
stompHeaders := stompngo.Headers{}
c, err := stompngo.Connect(conn.NetConnection, stompHeaders)
if err != nil {
log.Print("Can't connect to Stomp.")
log.Fatal(err)
}
conn.Connection = c
return c
}
// Send data to ActiveMQ
func (s *StompClient) SendEvent(data, event string) {
headers := stompngo.Headers{"destination", s.Dest, "eventtype", event}
//sending message
err := s.Connection.Send(headers, data)
if err != nil {
log.Print("Can't send the message.")
log.Fatal(err)
}
log.Print("Message sent!")
}