rabbitmq-cdi
rabbitmq-cdi copied to clipboard
Publishing and subscribing with binding the same event class
Hello, with the following configuration inside my JEE app we jump into infinitive loop after emitting even one event with EventOne class. This is due the binding the the same class for an exchange and for a queue.
ExchangeDeclaration testExchangeOne = declarerFactory()
.createExchangeDeclaration("test.exchange.one")
.withType(BuiltinExchangeType.DIRECT)
.withAutoDelete(false)
.withDurable(false); // (1)
QueueDeclaration testQueue = declarerFactory()
.createQueueDeclaration("test.queue")
.withAutoDelete(false)
.withDurable(false)
.withExclusiveAccess(false); // (2)
BindingDeclaration bindingDeclaration = declarerFactory()
.createBindingDeclaration(testQueue, testExchangeOne)
.withRoutingKey("test.queue");
bind(EventOne.class)
.toExchange("test.exchange.one")
.withDeclaration(testExchangeOne)
.withDeclaration(bindingDeclaration)
.withDeclaration(testQueue)
.withRoutingKey("test.queue"); // (3)
bind(EventOne.class)
.toQueue("test.queue")
.autoAck(); // (4)
Simple solution is to create a copy of EventOne class properties into class with different name and bind it to the queue.
bind(EventOne.class)
.toExchange("test.exchange.one")
.withDeclaration(testExchangeOne)
.withDeclaration(bindingDeclaration)
.withDeclaration(testQueue)
.withRoutingKey("test.queue"); // (3)
bind(EventOneClone.class)
.toQueue("test.queue")
.autoAck(); // (4)
But this leads to code duplication for every event type, DRY principle violated. Can You suggest any solution for that? Many thanks for Your help!
I do not have a good solution of the top of my head at the moment. The is no way to distinguish between incoming and outgoing events when having the same Type for both directions. Any idea to solve this problem and enhance the implementation are appreciated. 😊