Currently, I have everything working with one injection of my CDI bean, but I need about 6 to 10 separate instances(?) depending on my inputs. (Ideally, I would like to be able to inject a dynamic number of the same CDI beans depending on how many I need.)
Unfortunately, I need to have the beans injected since the class depends on another CDI bean. (See the 2.CDI bean class below)
- Clients class
@ApplicationScoped
public class Clients {
Publisher pub;
Subscriber sub;
@Inject
ListenerBean listener;
public void init(){
pub = new Publisher();
sub = new Subscriber(listener);
}
}
- CDI bean class
@Dependent
public class ListenerBean{
@Inject
private eventHandler h;
public void onMessage(Msg msg){
h.doesSomething();
}
}
The functionality I'm looking for, but not sure what's available out there. I haven't found anyone looking to accomplish the same thing.
for(Subscriber s: listOfSubscribers){
@Inject
ListenerBean l;
s = new Subscriber(l);
}
// The only thing I can do right now is
@Inject
ListenerBean listener1;
@Inject
ListenerBean listener2;
@Inject
ListenerBean listener3;
@Inject
ListenerBean listener4;
Edit: The listeners do need to be identifiable. (Calls a setter after injection.) Probably not the best practice but not sure how else to implement this-
- The listeners will listen to different topics (declared by the respective consumer)
- Each listener must handle their messages based off of the topic
@Inject
ListenerBean listener1;
listener1.setTopic("TopicA");