Skip to main content

injecting the collection of EJBs, not the specific one, is that possible?

3 replies [Last post]
Anonymous

Hi there,
I know this question is not Glassfish specific, but Java EE 5 in
general, but I do not know where to ask this: is this possible to use
something like @EJB to inject session bean, but I would like to inject
all the session beans implementing given interface? (not the only
one).
I would like to create session beans implementing something like:

public void process(Operation operation);

I could implement each operation type in separate session bean, then
inject all of them and use like this:
Collection operationBeans;
...
Operation someOperation = ....;
for (OperationLocal operationBean : operationBeans) {
operationBean.process(someOperation);
}

Each implementation could internally check if it is interested in that
operation and ignore it if that is so.

So, is this possible to inject collection of session beans
implementing given interface instead of only one per @EJB annotation?

Thanks,
Witek

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Witold Szczerba

Can you give me some hint how could I implement such a explicit lookup?

Is this possible to ask server about bean names? When I have only one
bean implementing an interface, the server somehow finds it and
injects that bean. When server finds more than one bean implementing
the interface, and I want to get such a bean without providing its
name, the server will not deploy such an application.

It means, he must have some kind of discovery service. Is such a
service available? If so, I could, in a loop, lookup for that beans
and return in a collection...

Witek

2007/7/9, Mahesh Kannan :
>
> On Jul 9, 2007, at 4:30 AM, Witold Szczerba wrote:
>
> > Hi there,
> > I know this question is not Glassfish specific, but Java EE 5 in
> > general, but I do not know where to ask this: is this possible to use
> > something like @EJB to inject session bean, but I would like to inject
> > all the session beans implementing given interface? (not the only
> > one).
> No, this is not possible. The closest that I can think of is to do
> explicit jndi lookup(s) to get references of all such session beans
> and add to the collection.
> Note that you can use @Resource and use a custom ObjectFactory, but
> the ObjectFactory will have to do multiple lookup(s) itself to
> populate the collection.,
>
> Thanks,
> --Mahesh
>
>
> > I would like to create session beans implementing something like:
> >
> > public void process(Operation operation);
> >
> > I could implement each operation type in separate session bean, then
> > inject all of them and use like this:
> > Collection operationBeans;
> > ...
> > Operation someOperation = ....;
> > for (OperationLocal operationBean : operationBeans) {
> > operationBean.process(someOperation);
> > }
> >
> > Each implementation could internally check if it is interested in that
> > operation and ignore it if that is so.
> >
> > So, is this possible to inject collection of session beans
> > implementing given interface instead of only one per @EJB annotation?
> >
> > Thanks,
> > Witek
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> > For additional commands, e-mail: users-help@glassfish.dev.java.net
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

Mahesh Kannan

On Jul 9, 2007, at 4:30 AM, Witold Szczerba wrote:

> Hi there,
> I know this question is not Glassfish specific, but Java EE 5 in
> general, but I do not know where to ask this: is this possible to use
> something like @EJB to inject session bean, but I would like to inject
> all the session beans implementing given interface? (not the only
> one).
No, this is not possible. The closest that I can think of is to do
explicit jndi lookup(s) to get references of all such session beans
and add to the collection.
Note that you can use @Resource and use a custom ObjectFactory, but
the ObjectFactory will have to do multiple lookup(s) itself to
populate the collection.,

Thanks,
--Mahesh

> I would like to create session beans implementing something like:
>
> public void process(Operation operation);
>
> I could implement each operation type in separate session bean, then
> inject all of them and use like this:
> Collection operationBeans;
> ...
> Operation someOperation = ....;
> for (OperationLocal operationBean : operationBeans) {
> operationBean.process(someOperation);
> }
>
> Each implementation could internally check if it is interested in that
> operation and ignore it if that is so.
>
> So, is this possible to inject collection of session beans
> implementing given interface instead of only one per @EJB annotation?
>
> Thanks,
> Witek
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

Witold Szczerba

So many years of EJB and Java Enterprise and nobody though about
referencing collection of objects implementing same interface :(
What you said about ObjectFactory seems interesting, I didn't know
about custom resources, This is too bad one have to accomplish special
steps (registering custom resource) before application can use this.
That means one more step before new application server is ready for
.ear to deploy.

2007/7/9, Mahesh Kannan :
>
> On Jul 9, 2007, at 4:30 AM, Witold Szczerba wrote:
>
> > Hi there,
> > I know this question is not Glassfish specific, but Java EE 5 in
> > general, but I do not know where to ask this: is this possible to use
> > something like @EJB to inject session bean, but I would like to inject
> > all the session beans implementing given interface? (not the only
> > one).
> No, this is not possible. The closest that I can think of is to do
> explicit jndi lookup(s) to get references of all such session beans
> and add to the collection.
> Note that you can use @Resource and use a custom ObjectFactory, but
> the ObjectFactory will have to do multiple lookup(s) itself to
> populate the collection.,
>
> Thanks,
> --Mahesh
>
>
> > I would like to create session beans implementing something like:
> >
> > public void process(Operation operation);
> >
> > I could implement each operation type in separate session bean, then
> > inject all of them and use like this:
> > Collection operationBeans;
> > ...
> > Operation someOperation = ....;
> > for (OperationLocal operationBean : operationBeans) {
> > operationBean.process(someOperation);
> > }
> >
> > Each implementation could internally check if it is interested in that
> > operation and ignore it if that is so.
> >
> > So, is this possible to inject collection of session beans
> > implementing given interface instead of only one per @EJB annotation?
> >
> > Thanks,
> > Witek

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net