rabbitmqworker

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2021 License: Apache-2.0 Imports: 11 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

func NewWorker(service factory.ServiceFactory, opts ...OptionFunc) factory.AppServerFactory

NewWorker create new rabbitmq consumer

Types

type OptionFunc added in v1.7.4

type OptionFunc func(*option)

OptionFunc type

func SetBrokerHost added in v1.7.4

func SetBrokerHost(broker string) OptionFunc

SetBrokerHost option func

func SetConsumerGroup added in v1.7.4

func SetConsumerGroup(consumerGroup string) OptionFunc

SetConsumerGroup option func, for log when startup

func SetDebugMode added in v1.7.4

func SetDebugMode(debugMode bool) OptionFunc

SetDebugMode option func

func SetExchangeName added in v1.7.4

func SetExchangeName(exchangeName string) OptionFunc

SetExchangeName option func

func SetMaxGoroutines added in v1.7.4

func SetMaxGoroutines(maxGoroutines int) OptionFunc

SetMaxGoroutines option func

Jump to

Keyboard shortcuts

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