-
Notifications
You must be signed in to change notification settings - Fork 9
EBP-30: Added Howtos to demonstrate how to use the Go PubSub+ API with the PubSub+ cache #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
oodigie
wants to merge
8
commits into
SolaceSamples:main
Choose a base branch
from
oodigie:EBP-30
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fb10f79
EBP-30: Added howtos to demonstrate how to use the Go PubSub+ API wit…
oodigie c482cef
EBP-30: Added howtos to demonstrate how to use the Go PubSub+ API wit…
oodigie 31d59a1
Update howtos/how_to_use_pubsub_cache.go
Mrc0113 bafdb5c
Update howtos/how_to_use_pubsub_cache.go
Mrc0113 fe8f440
Update howtos/how_to_use_pubsub_cache.go
Mrc0113 1745df2
Update howtos/how_to_use_pubsub_cache.go
Mrc0113 085f55e
Update howtos/how_to_use_pubsub_cache.go
Mrc0113 8016b4c
Update howtos/how_to_use_pubsub_cache.go
Mrc0113 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/signal" | ||
| "time" | ||
|
|
||
| "solace.dev/go/messaging" | ||
| "solace.dev/go/messaging/pkg/solace" | ||
| "solace.dev/go/messaging/pkg/solace/config" | ||
| "solace.dev/go/messaging/pkg/solace/message" | ||
| "solace.dev/go/messaging/pkg/solace/resource" | ||
| ) | ||
|
|
||
| const ( | ||
| ValidCachedMessageAge int32 = 0 | ||
| ValidMaxCachedMessages int32 = 0 | ||
| ValidCacheAccessTimeout int32 = 5000 | ||
| ) | ||
|
|
||
| func getEnv(key, def string) string { | ||
| if val, ok := os.LookupEnv(key); ok { | ||
| return val | ||
| } | ||
| return def | ||
| } | ||
|
|
||
| // HowToGetCacheRequestConfiguration - example of how to retrieve the cache request | ||
| // based on the different supported strategies | ||
| func HowToGetCacheRequestConfiguration(cacheRequestStrategy resource.CachedMessageSubscriptionStrategy) resource.CachedMessageSubscriptionRequest { | ||
| // specific the topic name | ||
| topic := "MaxMsgs3/default/notcached" | ||
| // specific the cache name | ||
Mrc0113 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| cacheName := "CacheMessages" | ||
| var cacheRequestConfig resource.CachedMessageSubscriptionRequest | ||
|
|
||
| switch cacheRequestStrategy { | ||
| // For cache request with strategy AsAvailable | ||
| case resource.CacheRequestStrategyAsAvailable: | ||
| cacheRequestConfig = resource.NewCachedMessageSubscriptionRequest( | ||
| resource.CacheRequestStrategyAsAvailable, | ||
| cacheName, | ||
| resource.TopicSubscriptionOf(topic), | ||
| ValidCacheAccessTimeout, // specific cache access timeout | ||
| ValidMaxCachedMessages, // specific max cached messages | ||
| ValidCachedMessageAge, // specific max cached message age | ||
Mrc0113 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Mrc0113 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Mrc0113 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| // For cache request with strategy CachedFirst | ||
| case resource.CacheRequestStrategyCachedFirst: | ||
| cacheRequestConfig = resource.NewCachedMessageSubscriptionRequest( | ||
| resource.CacheRequestStrategyCachedFirst, | ||
| cacheName, | ||
| resource.TopicSubscriptionOf(topic), | ||
| ValidCacheAccessTimeout, // specific cache access timeout | ||
| ValidMaxCachedMessages, // specific max cached messages | ||
| ValidCachedMessageAge, // specific max cached message age | ||
| ) | ||
| // For cache request with strategy CachedOnly | ||
| case resource.CacheRequestStrategyCachedOnly: | ||
| cacheRequestConfig = resource.NewCachedMessageSubscriptionRequest( | ||
| resource.CacheRequestStrategyCachedOnly, | ||
| cacheName, | ||
| resource.TopicSubscriptionOf(topic), | ||
| ValidCacheAccessTimeout, // specific cache access timeout | ||
| ValidMaxCachedMessages, // specific max cached messages | ||
| ValidCachedMessageAge, // specific max cached message age | ||
| ) | ||
| // For cache request with strategy LiveCancelsCached | ||
| case resource.CacheRequestStrategyLiveCancelsCached: | ||
| cacheRequestConfig = resource.NewCachedMessageSubscriptionRequest( | ||
| resource.CacheRequestStrategyLiveCancelsCached, | ||
| cacheName, | ||
| resource.TopicSubscriptionOf(topic), | ||
| ValidCacheAccessTimeout, // specific cache access timeout | ||
| ValidMaxCachedMessages, // specific max cached messages | ||
| ValidCachedMessageAge, // specific max cached message age | ||
| ) | ||
| } | ||
|
|
||
| // Dump the cache request | ||
| fmt.Println(fmt.Sprintf("\nConstructed Cache Request Strategy: %s \n", cacheRequestConfig)) | ||
| return cacheRequestConfig | ||
| } | ||
|
|
||
| // CacheResponseCallback - this is the callback to handle the cache response | ||
| // (used by the directReceiver.RequestCachedAsyncWithCallback()) | ||
| func CacheResponseCallback(cacheResponse solace.CacheResponse) { | ||
| // do something with the cache response | ||
| // ... | ||
| // ... | ||
|
|
||
| // dump the cache response here | ||
| // How to correlate the cache request ID of a request with a received message and response | ||
| fmt.Printf("Received Cache Response; CacheRequestID %s\n", cacheResponse.GetCacheRequestID()) | ||
|
|
||
| // How to check the CacheRequestOutcome of a cache response | ||
| fmt.Printf("Received Cache Response; CacheRequestOutcome %s\n", cacheResponse.GetCacheRequestOutcome()) | ||
|
|
||
| // How to check the error of a cache response | ||
| fmt.Printf("Received Cache Response; Error %s\n", cacheResponse.GetError()) | ||
| } | ||
|
|
||
| // HowToSendCacheRequestAndProcessCacheResponseWithCallback - example of send the cache request and retrieve the cache response using a callback | ||
| func HowToSendCacheRequestAndProcessCacheResponseWithCallback(directReceiver solace.DirectMessageReceiver, cacheRequestConfig resource.CachedMessageSubscriptionRequest) { | ||
| // submit a cache request with a specific cache request ID (in this case we use 1) | ||
| cacheRequestID := message.CacheRequestID(1) | ||
|
|
||
| // call the method on the direct receiver to send the cache request and retrieve the cache response using a callback | ||
| err := directReceiver.RequestCachedAsyncWithCallback(cacheRequestConfig, cacheRequestID, CacheResponseCallback) | ||
|
|
||
| // we do not expect an error | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // ... | ||
| } | ||
|
|
||
| // HowToSendCacheRequestAndProcessCacheResponseWithChannel - example of how to send the cache request and retrieve the cache response on a channel | ||
| func HowToSendCacheRequestAndProcessCacheResponseWithChannel(directReceiver solace.DirectMessageReceiver, cacheRequestConfig resource.CachedMessageSubscriptionRequest) { | ||
| // submit a cache request with a specific cache request ID (in this case we use 1) | ||
| cacheRequestID := message.CacheRequestID(1) | ||
|
|
||
| // call the method on the direct receiver to send the cache request and retrieve the cache response on a channel | ||
| cacheResponseChannel, err := directReceiver.RequestCachedAsync(cacheRequestConfig, cacheRequestID) | ||
|
|
||
| // we do not expect an error | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| select { | ||
| // this will hold the cache response that was received on the channel | ||
| case cacheResponse := <-cacheResponseChannel: | ||
| // do something with the cache response | ||
| // ... | ||
| // ... | ||
|
|
||
| // dump the cache response here | ||
| // How to correlate the cache request ID of a request with a received message and response | ||
| fmt.Printf("Received Cache Response; CacheRequestID %s\n", cacheResponse.GetCacheRequestID()) | ||
|
|
||
| // How to check the CacheRequestOutcome of a cache response | ||
| fmt.Printf("Received Cache Response; CacheRequestOutcome %s\n", cacheResponse.GetCacheRequestOutcome()) | ||
|
|
||
| // How to check the error of a cache response | ||
| fmt.Printf("Received Cache Response; Error %s\n", cacheResponse.GetError()) | ||
|
|
||
| case <-time.After(1 * time.Second): | ||
| fmt.Printf("timed out waiting for cache response to be recieved") | ||
Mrc0113 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // ... | ||
| } | ||
|
|
||
| // Code examples of how to use the Go PubSub+ cache. | ||
| func main() { | ||
| // logging.SetLogLevel(logging.LogLevelInfo) | ||
|
|
||
| // Configuration parameters | ||
| brokerConfig := config.ServicePropertyMap{ | ||
| config.TransportLayerPropertyHost: getEnv("SOLACE_HOST", "tcp://localhost:55555,tcp://localhost:55554"), | ||
| config.ServicePropertyVPNName: getEnv("SOLACE_VPN", "default"), | ||
| config.AuthenticationPropertySchemeBasicPassword: getEnv("SOLACE_PASSWORD", "default"), | ||
| config.AuthenticationPropertySchemeBasicUserName: getEnv("SOLACE_USERNAME", "default"), | ||
| config.TransportLayerPropertyReconnectionAttempts: 0, | ||
| } | ||
|
|
||
| messagingService, err := messaging.NewMessagingServiceBuilder(). | ||
| FromConfigurationProvider(brokerConfig). | ||
| Build() | ||
|
|
||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // Connect to the messaging serice | ||
Mrc0113 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if err := messagingService.Connect(); err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| fmt.Println("Connected to the broker? ", messagingService.IsConnected()) | ||
|
|
||
| // Build a Direct message receivers with given topics | ||
| directReceiver, err := messagingService.CreateDirectMessageReceiverBuilder(). | ||
| // we are using an abitary value for back pressure (you can configure this based on your use case) | ||
Mrc0113 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| OnBackPressureDropOldest(100100). | ||
| Build() | ||
|
|
||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // Start Direct Message Receiver | ||
| if err := directReceiver.Start(); err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| fmt.Println("Direct Receiver running? ", directReceiver.IsRunning()) | ||
|
|
||
| ///////////////////////////////////////// | ||
| // Cache EXAMPLES | ||
| ///////////////////////////////////////// | ||
|
|
||
| // For cache request with strategy AsAvailable | ||
| var cachedMessageSubscriptionRequest resource.CachedMessageSubscriptionRequest = HowToGetCacheRequestConfiguration(resource.CacheRequestStrategyAsAvailable) | ||
|
|
||
| // // For cache request with strategy CachedFirst | ||
| // var cachedMessageSubscriptionRequest resource.CachedMessageSubscriptionRequest = HowToGetCacheRequestConfiguration(resource.CacheRequestStrategyCachedFirst) | ||
|
|
||
| // // For cache request with strategy CachedOnly | ||
| // var cachedMessageSubscriptionRequest resource.CachedMessageSubscriptionRequest = HowToGetCacheRequestConfiguration(resource.CacheRequestStrategyCachedOnly) | ||
|
|
||
| // // For cache request with strategy LiveCancelsCached | ||
| // var cachedMessageSubscriptionRequest resource.CachedMessageSubscriptionRequest = HowToGetCacheRequestConfiguration(resource.CacheRequestStrategyLiveCancelsCached) | ||
|
|
||
| // Send the cache request and retrieve the cache response using a callback | ||
| HowToSendCacheRequestAndProcessCacheResponseWithCallback(directReceiver, cachedMessageSubscriptionRequest) | ||
|
|
||
| // Send the cache request and retrieve the cache response using a callback | ||
| HowToSendCacheRequestAndProcessCacheResponseWithChannel(directReceiver, cachedMessageSubscriptionRequest) | ||
|
|
||
| fmt.Println("\n===Interrupt (CTR+C) to handle graceful terminaltion of the messaiging service===") | ||
Mrc0113 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // cleanup after the main calling function has finished execution | ||
| defer func() { | ||
| // A graceful shutdown of the directReceiver is attempted within the specified | ||
| // grace period (in this case, we are using 1 second). | ||
| var gracePeriod time.Duration = 1 * time.Second | ||
|
|
||
| // Terminate the Direct Receiver | ||
| // The receiver can be terminated before the cache response has been completed, | ||
| // but this is not possible to demonstrate in this HowTo due to infrastructure limitations. | ||
| // Setting gracePeriod to 0 implies a non-graceful shutdown that ignores unfinished tasks or in-flight messages. | ||
| // This function returns an error if one occurred, or nil if it successfully and gracefully terminated. | ||
| // If gracePeriod is set to less than 0, the function waits indefinitely. | ||
| directReceiver.Terminate(gracePeriod) | ||
| fmt.Println("\nDirect Receiver Terminated? ", directReceiver.IsTerminated()) | ||
|
|
||
| // Disconnect the Message Service | ||
| messagingService.Disconnect() | ||
| fmt.Println("Messaging Service Disconnected? ", !messagingService.IsConnected()) | ||
| }() | ||
|
|
||
| // Run forever until an interrupt signal is received | ||
| c := make(chan os.Signal, 1) | ||
| signal.Notify(c, os.Interrupt) | ||
| // Block until a interrupt signal is received. | ||
| <-c | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.