FroMage / redpipe

Redpipe Web Framework
Apache License 2.0
70 stars 10 forks source link

eventbus events weld consumer #14

Closed fiorenzino closed 6 years ago

fiorenzino commented 6 years ago

I created a simple project to test eventbus with cdi: https://github.com/fiorenzino/redpipe-eventbus

i created a subscriber using weld:

void echoConsumer(@Observes @VertxConsumer("test-queue") VertxEvent event) {
    System.out.println("received new event");
    System.out.println(event.getMessageBody());
}

My events never be received from consumer.

Suggestions?

FroMage commented 6 years ago

If you did add the redpipe-cdi module, then perhaps I'm not registering the vertx-cdi integration properly.

FroMage commented 6 years ago

I can reproduce, trying to understand why this happens.

FroMage commented 6 years ago

OK, I'm pretty sure it's related to the CDI context, which is not associated with the thread which is doing the event sending. Not sure at all how to fix that, though.

FroMage commented 6 years ago

Actually, I wonder if this is really about contexts and scopes. I'm pretty sure the resource bean is request-scoped by default, and the observer might need to be application scoped. If I make it application-scoped then it works. It also makes some sense, because observers don't make a lot of sense as request-scoped, no?

WDYT?

fiorenzino commented 6 years ago

You are right, if the consumer is annotated with @ApplicationScoped, it work correctly. I created a branch for that: https://github.com/fiorenzino/redpipe/tree/REDPIPE12/redpipe-example-cdi

@ApplicationScoped public class HelloService {

static final String HELLO_QUEUE = "test-queue";

public HelloService() {
    System.out.println("hello service construct");
}

public String name() {
    return "flower";
}
void helloConsumer(@Observes @VertxConsumer(HELLO_QUEUE) VertxEvent event) {
    System.out.println("hello " + event.getMessageBody());
}

}

FroMage commented 6 years ago

OK, I'll push a test for this and then close then.

FroMage commented 6 years ago

Done.