Created
January 14, 2022 11:08
-
-
Save seagalputra/3eda4b9eebfbbb400e2a4503122f048d to your computer and use it in GitHub Desktop.
Publisher abstraction for sending message using several message broker
This file contains 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
class Publisher | |
attr_accessor :strategy | |
def initialize(strategy) | |
@strategy = strategy | |
end | |
def publish(data) | |
@strategy.call(data) | |
end | |
class Strategy | |
def call(data) | |
raise NotImplementedError, "#{self.class} has no implementation method '#{__method__}'" | |
end | |
end | |
# in case you want to add more strategy, just inherit call method from Strategy class and write your own logic | |
# to call the producer from message broker | |
class KafkaStrategy < Strategy | |
def call(data) | |
# implement some logic for sending message using Apache Kafka here... | |
end | |
end | |
class GooglePubSubStrategy < Strategy | |
def call(data) | |
# implement some logic for sending message using Google Pub Sub here... | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment