The Source for Java Technology Collaboration
Webmaster Alert: Posting to Jive Forums is currently not working. Estimated time for fix is unknown.

Home » java.net Forums » GlassFish » GlassFish

Thread: Question about dependency injection

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 5 - Last Post: Mar 2, 2006 4:36 PM by: guruwons
dibyendumajumdar

Posts: 55
Question about dependency injection
Posted: Feb 23, 2006 2:19 AM
  Click to reply to this thread Reply

I have a Session Bean with Local and Remote annotations.
How can I say to the client that I want the Local or Remote interface?

I notice that if I use both annotations, then dependency injection fails when I try to access the EJB from an application client. If I remove the Local annotation, it works okay.

I am using Glassfish build 32g.

Regards

Dibyendu

ss141213

Posts: 500
Re: Question about dependency injection
Posted: Feb 23, 2006 5:46 AM   in response to: dibyendumajumdar
  Click to reply to this thread Reply

1.
Do you have one bean implementation class that has the same Java interface as local and remote interface, e.g.
@Local(Foo.class)
@Remote(Foo.class)
@Stateless public class MyEJB implements Foo {
...
}

and
you are trying to use it from an ejb or a web app?

In this case, you probably have to use deployment descritor (ejb-jar.xml) so that you can deploy the same implementation class twice each time using a separate ejb-name. Then you can use those names in @EJB(beanName="xxx") format in your client.

2. The other alternative is to have something like this:

public interface Foo {...
}

public interface LocalFoo extends Foo{}
public interface RemoteFoo extends Foo{}

@Remote(RemoteFoo.class)
@Local(LocalFoo.class)
@Stateless
public class FooBean implements Foo {
...
}

Then you can use
@EJB LocalFoo myBean;
or
@EJB RemoteFoo myBean;

in your client. Container can use the interface type to figure out which one to use.

3. There is another thing worth mentioning here: if you use your ejb from an application client, then you can never use local interface. So container will always use the remote view. I actually don't know if this is a portable behavior or not. I can check and let you know. But I know GlassFish behaves this way.

Thanks,
Sahoo

ksak

Posts: 481
Re: Question about dependency injection
Posted: Feb 23, 2006 12:18 PM   in response to: ss141213
  Click to reply to this thread Reply

>
> 3. There is another thing worth mentioning here: if
> you use your ejb from an application client, then
> you can never use local interface. So container will
> always use the remote view
. I actually don't know
> if this is a portable behavior or not.

Local EJB access is not allowed from an Application Client.
An Application Client runs in a different JVM than the
ejb component so there is no way to achieve the correct the Local EJB
invocation semantics, most importantly pass-by-reference.

> I can check
> and let you know. But I know GlassFish behaves this
> way.
>
> Thanks,
> Sahoo

dibyendumajumdar

Posts: 55
Re: Question about dependency injection
Posted: Feb 23, 2006 12:47 PM   in response to: ksak
  Click to reply to this thread Reply

> Local EJB access is not allowed from an Application
> Client.
> An Application Client runs in a different JVM than
> the
> ejb component so there is no way to achieve the
> correct the Local EJB
> invocation semantics, most importantly
> pass-by-reference.

Glassfish allows an interface to be annotated both as @Remote and @Local. I assumed therefore that this is legal, and also that there is some intelligence to decide which one is appropriate. However, what happened was that when the application client tried to lookup the EJB, it got an exception. Almost as if the Local interface had hidden or taken precedence over the Remote interface.

From my perspective, I want the ability to decide which interface I want to use, subject to what is permissible. Obviously, in an application client that is remote, it does not make sense to try to obtain a Local interface. However, when I know that the client is in the same JVM, I'd like to use the Local interface.

I like the solution proposed by Sahoo in point 2.

Regards

Dibyendu

dibyendumajumdar

Posts: 55
Re: Question about dependency injection
Posted: Feb 23, 2006 1:09 PM   in response to: dibyendumajumdar
  Click to reply to this thread Reply

On further thought, I feel that it would be better to enhance the EJB annotation to allow the client to say that they want the Remote or Local interface. The problem with the workaround that Sahoo has suggested is that it destroys the POJO-ness of the EJB, and introduces artifacts that are not part of the "design" but purely a workaround for a problem introduced by the implementation technology - ie - J2EE (Rod Johnson presents this point very well in chapter 1 of his classic book on J2EE Design and Development).

I think it is vital for EJB 3.0 to enable POJO style programming. Business Logic of an application is its most valuable asset. Creating Business Logic as POJOs is the best way to preserve the investment in it.

One of the greatest benefits of Spring Framework is that does not impose any special requirements on the design of Business Logic.

guruwons

Posts: 6
Re: Question about dependency injection
Posted: Mar 2, 2006 4:36 PM   in response to: dibyendumajumdar
  Click to reply to this thread Reply

The same business interface cannot be used both for remote and local. This restriction is added to the spec(after PFD) recently to avoid ambiguous situation.

The spec says at core contract 4.6.6:
* The same business interface cannot be both a local and a remote business interface of the bean.(It is also an error if the Local and/or Remote annotations are specified both on the bean class and on the referenced interface
and the values differ.)




 XML java.net RSS