pmorelli92 / bunnify

AMQP library to publish and consume events

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

exchange mode only direct, it not enought, may be should add topic etc.

lorentz-wu opened this issue · comments

exchange mode only direct, it not enought, may be should add topic etc.

Sure, I can look into that

Hi @lorentz-wu, sorry for the big delay, can you elaborate more on your use case?

Hi @pmorelli92, can you see if other exchange types can be added?
It would be very helpful, thank you!

https://medium.com/trendyol-tech/rabbitmq-exchange-types-d7e1f51ec825

@tomaswarynyca, as I asked before, what is the use case you are trying to achieve? The direct exchange approach that the library supports does not mean one event one receiver, as if multiple queues bind to the same routing key on the same exchange they will all receive the event:

Queue1 binds to "order.processed" on exchange Foo
Queue2 binds to "order.processed" on exchange Foo

Publisher publishes event "order.processed" on exchange Foo.

Queue1 receives event.
Queue2 receives event.

Configuring different exchange type is not hard to do, but I want to make sure what is the use case for it :)

@pmorelli92 In my case I would like to use the library for an event system, a service publishes a message and there are multiple services listening to do something with that message.

I extend the example a little more - the order service publishes order.processed and the services of users, invoices, etc. have to receive the message order.processed.

That is doable with the functionality I exposed above :)
I suggest you to check the tests to get a better understanding on how to make it work but I will also add a snippet in this comment. Please tell me if you could make it work :)

User service:

eventHandler := func(ctx context.Context, event bunnify.ConsumableEvent[orderProcessed]) error {
	return nil
}

consumer := connection.NewConsumer(
		"user-queue",
		bunnify.WithQuorumQueue(),
		bunnify.WithBindingToExchange("default-exchange"),
		bunnify.WithHandler("order.processed",  eventHandler))

Invoice service:

eventHandler := func(ctx context.Context, event bunnify.ConsumableEvent[orderProcessed]) error {
	return nil
}

consumer := connection.NewConsumer(
		"invoice-queue",
		bunnify.WithQuorumQueue(),
		bunnify.WithBindingToExchange("default-exchange"),
		bunnify.WithHandler("order.processed",  eventHandler))

Order service:

eventToPublish := bunnify.NewPublishableEvent(orderProcessed{
        ID: orderID,
})

err := publisher.Publish(
	context.TODO(),
	"default-exchange",
	"order.processed",
	eventToPublish)
if err != nil {
	t.Fatal(err)
}

You can look more information here:

func TestConsumerPublisher(t *testing.T) {

I assume the use case is fulfill with what I explained above, can I close @tomaswarynyca ?

I assume the use case is fulfill with what I explained above, can I close @tomaswarynyca ?

For my case it seems that yes, in the next few days I will develop it and I will comment you

@pmorelli92 for my use case with the current code it works perfectly, unless @lorentz-wu has a different use case we can close this issue for the moment, thanks for your help!