-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolace_publisher.go
90 lines (73 loc) · 2.33 KB
/
solace_publisher.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
package main
import (
"fmt"
"os"
"os/signal"
"strconv"
"time"
"solace.dev/go/messaging"
"solace.dev/go/messaging/pkg/solace/config"
"solace.dev/go/messaging/pkg/solace/resource"
)
func main() {
// Configuration parameters
brokerConfig := config.ServicePropertyMap{
config.TransportLayerPropertyHost: "tcp://public.messaging.solace.cloud",
config.ServicePropertyVPNName: "public",
config.AuthenticationPropertySchemeBasicUserName: "conf42",
config.AuthenticationPropertySchemeBasicPassword: "public",
}
messagingService, err := messaging.NewMessagingServiceBuilder().FromConfigurationProvider(brokerConfig).Build()
if err != nil {
panic(err)
}
// Connect to the messaging serice
if err := messagingService.Connect(); err != nil {
panic(err)
}
// Build a Direct Message Publisher
directPublisher, builderErr := messagingService.CreateDirectMessagePublisherBuilder().Build()
if builderErr != nil {
panic(builderErr)
}
// Start the publisher
startErr := directPublisher.Start()
if startErr != nil {
panic(startErr)
}
msgSeqNum := 0
// Prepare outbound message payload and body
messageBody := "Hello from Conf42"
messageBuilder := messagingService.MessageBuilder().
WithProperty("application", "samples").
WithProperty("language", "go")
// Run forever until an interrupt signal is received
go func() {
for directPublisher.IsReady() {
msgSeqNum++
message, err := messageBuilder.BuildWithStringPayload(messageBody + " --> " + strconv.Itoa(msgSeqNum))
if err != nil {
panic(err)
}
topic := resource.TopicOf("conf42/solace/go/" + strconv.Itoa(msgSeqNum))
// Publish on dynamic topic with dynamic body
publishErr := directPublisher.Publish(message, topic)
if publishErr != nil {
panic(publishErr)
}
fmt.Println("Published message on topic: ", topic.GetName())
time.Sleep(1 * time.Second)
}
}()
// Handle OS interrupts
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
// Block until an OS interrupt signal is received.
<-c
// Terminate the Direct Publisher
directPublisher.Terminate(1 * time.Second)
fmt.Println("\nDirect Publisher Terminated? ", directPublisher.IsTerminated())
// Disconnect the Message Service
messagingService.Disconnect()
fmt.Println("Messaging Service Disconnected? ", !messagingService.IsConnected())
}