rabbitmqworker

package
v1.6.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

README

Example

This is example for create RabbitMQ consumer handler in delivery layer.

RabbitMQ Consumer with delayed message feature, please add this plugin to RabbitMQ broker.

Create delivery handler

package workerhandler

import (
	"context"
	"encoding/json"
	
	"example.service/internal/modules/examplemodule/delivery/workerhandler"

	"github.com/golangid/candi/codebase/factory/types"
	"github.com/golangid/candi/tracer"
)

// RabbitMQHandler struct
type RabbitMQHandler struct {
	uc        usecase.Usecase
	validator interfaces.Validator
}

// NewRabbitMQHandler constructor
func NewRabbitMQHandler(uc usecase.Usecase, validator interfaces.Validator) *RabbitMQHandler {
	return &RabbitMQHandler{
		uc:        uc,
		validator: validator,
	}
}

// MountHandlers mount handler group
func (h *RabbitMQHandler) MountHandlers(group *types.WorkerHandlerGroup) {
	group.Add("example-queue", h.handleQueue) // consume queue "example-queue"
}

func (h *RabbitMQHandler) handleQueue(ctx context.Context, message []byte) error {
	trace := tracer.StartTrace(ctx, "DeliveryRabbitMQ:HandleQueue")
	defer trace.Finish()
	ctx = trace.Context()

	log.Printf("message consumed. message: %s\n", message)
	// call usecase
	return nil
}

Register in module

package examplemodule

import (
	"example.service/internal/modules/examplemodule/delivery/workerhandler"

	"github.com/golangid/candi/codebase/factory/dependency"
	"github.com/golangid/candi/codebase/factory/types"
	"github.com/golangid/candi/codebase/interfaces"
)

type Module struct {
	// ...another delivery handler
	workerHandlers map[types.Worker]interfaces.WorkerHandler
}

func NewModules(deps dependency.Dependency) *Module {
	return &Module{
		workerHandlers: map[types.Worker]interfaces.WorkerHandler{
			// ...another worker handler
			// ...
			types.RabbitMQ: workerhandler.NewRabbitMQHandler(usecaseUOW.User(), deps.GetValidator()),
		},
	}
}

// ...another method

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewWorker

NewWorker create new rabbitmq consumer

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL