users@glassfish.java.net

Re: Local access to an EJB

From: Marina Vatkina <marina.vatkina_at_oracle.com>
Date: Tue, 01 Nov 2011 17:54:18 -0700

Alexis,

You can define a dependency on another EJB if say a helper class down
the calling stack needs to do a lookup. But in this particular case, you
are right - it is not used correctly.

-marina

Alexis Moussine-Pouchkine wrote:
> @EJB is used to inject a reference to an EJB, not to define one (one could argue that this wasn't the best annotation name but with CDI's @Inject this is all pretty much going away). We should probably trigger a warning when deploying an EJB annotated with @EJB as it doesn't make much sense.
>
> When it comes to injecting the EJB reference (you don't mention from which component btw), you should really avoid JNDI as this can be quite error-prone and use @EJB instead (or @Inject if using CDI which is part of Java EE 6). If you really need to use JNDI, then use the standard and portable JNDI names given to the EJBs (see GlassFish log on deployment).
>
> Check also the EJB FAQ: http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html
>
> -Alexis
>
>
> On 31 oct. 2011, at 15:43, forums_at_java.net wrote:
>
>
>> I have an EJB running in Glassfish 3.1.1 (build 12)
>>
>> @Local
>> public interface MyService{ ....... }
>>
>> @Startup
>> @Singleton
>> @EJB(name = "java:global/MyService", beanInterface = MyService.class,
>> beanName="MyService")
>> public class MyServiceImpl implements ScheduleService{ ....... }
>>
>> It deploys well. My problem is when I try to use it. All the examples for
>> JNDI I've seen I can cast the object I get to the interface like:
>>
>> MyService svc=(MyService)new
>> InitialContext().lookup("java:global/MyService");
>>
>> This is giving me a ClassCastException. If I use reflection I can use the
>> object.
>>
>>
>>
>>
>>
>>
>> --
>>
>> [Message sent by forum member 'fawzib']
>>
>> View Post: http://forums.java.net/node/858733
>>
>>
>>
>
>