1

I'm using EJB 2.1 stateless session bean in wildfly 9.

TestService testServiceLocal = (TestService) context.lookup("java:app/WildEarEJB/TestService!in.hinote.ejb.TestService");
testServiceLocal.someMethod();

While the above code works, but I was wondering if this is ok? As I'm not following the old practice of:

  1. Get ejb home object reference using JNDI
  2. Use PortableRemoteObject.narrow and typecast it to home interface.
  3. Use Home interface.create() to get the bean instance.

Is what I'm doing ok? Or should I find some way to follow the old practices. BTW, I tried using the home jndi and PortableRemoteObject.narrow(). But that seems to be giving a typecast error saying com.sun.proxy.$Proxy39 cannot be cast to org.omg.CORBA.Object.

Sahid
  • 100
  • 1
  • 1
  • 11

1 Answers1

0

A strict reading of the EJB spec says that if TestService extends EJBHome, then you must use PortableRemoteObject.narrow. You said you're using EJB 2.1, but TestService is an unusual home interface name; it sounds more like an EJB 3 style remote business interface. If TestSession extends EJBHome, then I would say yes, you must use PortableRemoteObject.narrow, and your application server should support narrow for it.

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
  • I'm using EJB 2.1 only. `TestService` is the remote interface for `TestServiceBean` whereas `TestServiceHome` is the remote home interface. I tried using the `PortableRemoteObject.narrow()` but that is giving typecast error. Any reason you could think of? – Sahid Sep 23 '15 at 10:12
  • Then you should be looking up `java:app/WildEarEJB/TestService!in.hinote.ejb.TestServiceHome`, and you should be doing a narrow. I don't know why your lookup is succeeding; perhaps it's a Wildfly extension. – Brett Kail Sep 23 '15 at 14:23
  • Thank you @bkail I've tried the lookup for Home as you suggested. The lookup succeeds, but `PortableRemoteObject.narrow()` fails with the typecast error. Strange, can you think of anything? – Sahid Sep 24 '15 at 06:26
  • I would report the issue to Wildfly. In the meantime, does the cast work if you don't narrow? – Brett Kail Sep 24 '15 at 14:16
  • Sorry for little late response. Yes the cast works. If I add a sysout of that object after the cast, I get this: _Proxy for view class: in.hinote.ejb.TestServiceLocalHome of EJB: TestService_ – Sahid Sep 26 '15 at 18:24
  • I guess JBoss does magic to give you a proxy that doesn't require a narrow (and in fact fails if you do). That's surprising to me, but I guess you can remain (semi?) portable if you do something like `if (!(o instanceof TestService)) o = PortableRemoteObject.narrow(o, TestService.class);` – Brett Kail Sep 28 '15 at 03:06