|
@@ -0,0 +1,48 @@
|
|
|
|
+package message_queue
|
|
|
|
+
|
|
|
|
+// MessageHandler 消息处理函数
|
|
|
|
+// 参数:
|
|
|
|
+// - queue: 消息队列
|
|
|
|
+// - topic: 主题
|
|
|
|
+// - data: 消息数据
|
|
|
|
+// 返回值: 无
|
|
|
|
+type MessageHandler func(queue MessageQueue, topic string, data []byte)
|
|
|
|
+
|
|
|
|
+// MessageQueue 消息队列接口
|
|
|
|
+type MessageQueue interface {
|
|
|
|
+ Subscribe(topic string, handler MessageHandler) error
|
|
|
|
+ UnSubscribe(topic string) error
|
|
|
|
+ Publish(topic string, data []byte) error
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Subscribe 订阅
|
|
|
|
+// 参数:
|
|
|
|
+// - queue: 消息队列
|
|
|
|
+// - topic: 主题
|
|
|
|
+// - handler: 消息处理函数
|
|
|
|
+// 返回值:
|
|
|
|
+// - 错误
|
|
|
|
+func Subscribe(messageQueue MessageQueue, topic string, handler MessageHandler) error {
|
|
|
|
+ return messageQueue.Subscribe(topic, handler)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// UnSubscribe 取消订阅
|
|
|
|
+// 参数:
|
|
|
|
+// - queue: 消息队列
|
|
|
|
+// - topic: 主题
|
|
|
|
+// 返回值:
|
|
|
|
+// - 错误
|
|
|
|
+func UnSubscribe(messageQueue MessageQueue, topic string) error {
|
|
|
|
+ return messageQueue.UnSubscribe(topic)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Publish 发布消息
|
|
|
|
+// 参数:
|
|
|
|
+// - queue: 消息队列
|
|
|
|
+// - topic: 主题
|
|
|
|
+// - data: 消息数据
|
|
|
|
+// 返回值:
|
|
|
|
+// - 错误
|
|
|
|
+func Publish(messageQueue MessageQueue, topic string, data []byte) error {
|
|
|
|
+ return messageQueue.Publish(topic, data)
|
|
|
|
+}
|