Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

exchange_functions.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package functions
  2. import (
  3. "time"
  4. "git.x2erp.com/qdy/go-base/ctx"
  5. "git.x2erp.com/qdy/go-base/types"
  6. "git.x2erp.com/qdy/go-db/factory/rabbitmq"
  7. )
  8. // CreateExchange 创建交换机
  9. func CreateExchange(rabbitFactory *rabbitmq.RabbitMQFactory, req types.ExchangeRequest, reqCtx *ctx.RequestContext) *types.QueryResult {
  10. // 设置默认值
  11. if req.ChannelName == "" {
  12. req.ChannelName = "default"
  13. }
  14. if req.ExchangeType == "" {
  15. req.ExchangeType = "direct"
  16. }
  17. // 确保通道存在
  18. _, err := rabbitFactory.CreateChannel(req.ChannelName)
  19. if err != nil {
  20. return &types.QueryResult{
  21. Success: false,
  22. Error: "Failed to create channel: " + err.Error(),
  23. Time: time.Now().Format(time.RFC3339),
  24. }
  25. }
  26. // 创建交换机
  27. err = rabbitFactory.AddExchange(
  28. req.ChannelName,
  29. req.ExchangeName,
  30. req.ExchangeType,
  31. req.Durable,
  32. )
  33. if err != nil {
  34. return &types.QueryResult{
  35. Success: false,
  36. Error: "Failed to create exchange: " + err.Error(),
  37. Time: time.Now().Format(time.RFC3339),
  38. }
  39. }
  40. return &types.QueryResult{
  41. Success: true,
  42. Message: "Exchange created successfully",
  43. Time: time.Now().Format(time.RFC3339),
  44. Data: map[string]interface{}{
  45. "exchange_name": req.ExchangeName,
  46. "exchange_type": req.ExchangeType,
  47. "durable": req.Durable,
  48. "channel": req.ChannelName,
  49. },
  50. }
  51. }