Client working and logging to terminal
This commit is contained in:
parent
efcce8a632
commit
3266b147ee
@ -7,6 +7,7 @@
|
|||||||
*.dll
|
*.dll
|
||||||
*.so
|
*.so
|
||||||
*.dylib
|
*.dylib
|
||||||
|
*.txt
|
||||||
|
|
||||||
# Test binary, built with `go test -c`
|
# Test binary, built with `go test -c`
|
||||||
*.test
|
*.test
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -7,6 +7,7 @@
|
|||||||
*.dll
|
*.dll
|
||||||
*.so
|
*.so
|
||||||
*.dylib
|
*.dylib
|
||||||
|
*.txt
|
||||||
|
|
||||||
# Test binary, built with `go test -c`
|
# Test binary, built with `go test -c`
|
||||||
*.test
|
*.test
|
||||||
|
@ -10,5 +10,6 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
log.Msg.Info("Initialised OwlBoard MQ Client " + helpers.Version)
|
log.Msg.Info("Initialised OwlBoard MQ Client " + helpers.Version)
|
||||||
dbAccess.PrintFromDbPackage()
|
dbAccess.PrintFromDbPackage()
|
||||||
|
defer messaging.Disconnect(messaging.Client)
|
||||||
messaging.Listen()
|
messaging.Listen()
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ type nrodCredential struct {
|
|||||||
pass string
|
pass string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fetches credentials from environment variables and exits if none provided.
|
||||||
func getCredentials() nrodCredential {
|
func getCredentials() nrodCredential {
|
||||||
var nrod_user string = os.Getenv("OWL_LDB_CORPUSUSER")
|
var nrod_user string = os.Getenv("OWL_LDB_CORPUSUSER")
|
||||||
var nrod_pass string = os.Getenv("OWL_LDB_CORPUSPASS")
|
var nrod_pass string = os.Getenv("OWL_LDB_CORPUSPASS")
|
||||||
@ -27,8 +28,9 @@ func getCredentials() nrodCredential {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var client = dial()
|
var Client = dial()
|
||||||
|
|
||||||
|
// Connects the STOMP file to the Network Rail MQ Server
|
||||||
func dial() *stomp.Conn {
|
func dial() *stomp.Conn {
|
||||||
var credentials nrodCredential = getCredentials()
|
var credentials nrodCredential = getCredentials()
|
||||||
conn, err := stomp.Dial("tcp", "publicdatafeeds.networkrail.co.uk:61618",
|
conn, err := stomp.Dial("tcp", "publicdatafeeds.networkrail.co.uk:61618",
|
||||||
@ -38,19 +40,26 @@ func dial() *stomp.Conn {
|
|||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Msg.Fatal("Unable to connect to STOMP Client", zap.String("err", err.Error()))
|
log.Msg.Fatal("Unable to connect to STOMP Client", zap.String("err", err.Error()))
|
||||||
|
conn.MustDisconnect()
|
||||||
}
|
}
|
||||||
//defer disconnect(conn) // This exits as soon as the function has returned
|
|
||||||
log.Msg.Info("Initialised STOMP Client")
|
log.Msg.Info("Initialised STOMP Client")
|
||||||
return conn
|
return conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func disconnect(conn *stomp.Conn) {
|
// Handles graceful disconnection of the STOMP client, falls back to
|
||||||
|
// a force disconnect if this fails.
|
||||||
|
func Disconnect(conn *stomp.Conn) {
|
||||||
if conn != nil {
|
if conn != nil {
|
||||||
err := conn.Disconnect()
|
err := conn.Disconnect()
|
||||||
|
log.Msg.Warn("Disconnected STOMP Client")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
conn.MustDisconnect()
|
conn.MustDisconnect()
|
||||||
|
log.Msg.Error("STOMP Disconnection failed, forced disconnect")
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
log.Msg.Error("STOMP Disconnect failed, next connection attempt may fail")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register against the MQ Server and log each message for testing purposes
|
// Register against the MQ Server and log each message for testing purposes
|
||||||
|
@ -2,6 +2,8 @@ package messaging
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.fjla.uk/owlboard/mq-client/log"
|
"git.fjla.uk/owlboard/mq-client/log"
|
||||||
|
|
||||||
@ -9,19 +11,45 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Listen() {
|
func Listen() {
|
||||||
sub, err := client.Subscribe("/topic/VSTP_ALL", stomp.AckAuto)
|
sub, err := Client.Subscribe("/topic/VSTP_ALL", stomp.AckAuto)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Msg.Fatal("Unable to start subscription: " + err.Error())
|
log.Msg.Fatal("Unable to start subscription: " + err.Error())
|
||||||
}
|
}
|
||||||
|
log.Msg.Info("Subscription to VSTP topic successful")
|
||||||
|
|
||||||
msg := <-sub.C
|
go func() {
|
||||||
if msg.Err != nil {
|
log.Msg.Debug("Message handler routine started")
|
||||||
fmt.Println(msg.Err)
|
for {
|
||||||
}
|
msg := <-sub.C
|
||||||
handle(msg)
|
if msg.Err != nil {
|
||||||
|
fmt.Println(msg.Err)
|
||||||
|
}
|
||||||
|
handle(msg)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var count uint64 = 0
|
||||||
|
|
||||||
func handle(msg *stomp.Message) {
|
func handle(msg *stomp.Message) {
|
||||||
log.Msg.Info("STOMP message received")
|
log.Msg.Info("STOMP message received")
|
||||||
fmt.Println(msg)
|
count++
|
||||||
|
log.Msg.Info("Message count: " + fmt.Sprint(count))
|
||||||
|
fmt.Println(string(msg.Body))
|
||||||
|
saveToFile(string(msg.Body))
|
||||||
|
}
|
||||||
|
|
||||||
|
func saveToFile(msg string) {
|
||||||
|
timestamp := time.Now().Format("2006-01-02T15:04:05")
|
||||||
|
path := fmt.Sprintf("%s-msg.txt", timestamp)
|
||||||
|
|
||||||
|
err := ioutil.WriteFile(path, []byte(msg), 0644)
|
||||||
|
if err != nil {
|
||||||
|
log.Msg.Error("Error saving message: " + err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Msg.Info("Saved message to: " + path)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user