1

I am having some issue accessing stateless session bean properties. These are the things I have tried so far. Please shed some light what I am missing.

I have name space binding in the IBM WAS 8.5 as global/env

the I have my ejb as :

@Singleton 
@Stateless(mappedName="envEJB")
@LocalBean
@ConcurrencyManagement
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class EnvEJB {

    @Resource(lookup="global/env")
    protected String env;


    @Lock(LockType.READ)
    @AccessTimeout(1000)
    public String getEnv() {
        return env;
    }

And I am trying to access on my main class as:

Hashtable<String, String> env = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2890");

String str = "java:comp/env/envEJB";
Context context = new InitialContext(env);
EnvironmentEJB ejb = (EnvironmentEJB) context.lookup(str);

Only thing I am trying to achieve here is trying to access some name space binding from the server.

Doing above call I am getting

javax.naming.ConfigurationException: Name space accessor for the java: name space has not been set. Possible cause is that the user is specifying a java: URL name in a JNDI Context method call but is not running in a J2EE client or server environment.

Calling as String str = "java:app/appname/envEJB"; gives me the same error.

UPDATE

While doing as

Hashtable<String, String> pdEnv = new Hashtable<String, String>();
pdEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2890");
Context context = new InitialContext(pdEnv);        
java.lang.Object ejbBusIntf = context.lookup("com.test.EnvironmentEJBRemote");        
EnvironmentEJB ejb = (EnvironmentEJB)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, EnvironmentEJB.class);

the following exception occurs

Exception in thread "P=925268:O=0:CT" java.lang.ClassCastException: cannot cast class org.omg.stub.java.rmi._Remote_Stub to class com.test.EnvironmentEJB
at com.ibm.rmi.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:396)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:148)
at com.test.Test.main(Test.java:43)
António Ribeiro
  • 4,129
  • 5
  • 32
  • 49
jj sharma
  • 41
  • 1
  • 4

1 Answers1

1

The java: namespace is only supported in a managed environment (application server or application client). They are not supported in thin clients (standalone Java SE main classes). For that, you will need to use a non-java: name. See the CNTR0167I messages in SystemOut.log to find the non-java: bindings.

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
  • Thanks for pointing that out but I also tried context.lookup("ejb/ejbname") also but no luck. – jj sharma Mar 24 '16 at 18:49
  • Is `ejb/ejbname` the value printed in the `CNTR0167I` message? Can you show the output of those messages for the EJB you're trying to lookup? – Brett Kail Mar 24 '16 at 21:31
  • EJBContainerI I CNTR0167I: The server is binding the com.test.EnvironmentEJB interface of the envEJB enterprise bean in the Test.war module of the TestEAR application. The binding location is: ejblocal:TestEAR/Test.war/envEJB#com.test.EnvironmentEJB 2nd: The binding location is: ejblocal:com.test.EnvironmentEJB 3rd:The binding location is: java:global/TestEAR/Test/envEJB!com.test.EnvironmentEJB – jj sharma Mar 28 '16 at 19:04
  • As Gas said in the comments, you have to actually expose a remote interface or else you won't be able to access the EJB remotely. That means defining a remote interface and adding `@Remote(YourRemoteIntf.class)` to the EJB. Then, you should have a new `CNTR0167I` message with a remote binding (one that doesn't start with `ejblocal:` or `java:`). – Brett Kail Mar 28 '16 at 19:28
  • Now I have it as:ejb/TestEAR/Test.war/envEJB#com.test.EnvironmentEJBRemote and com.test.EnvironmentEJBRemote and java:global/TestEAR/Test/envEJB!com.test.EnvironmentEJBRemote – jj sharma Mar 28 '16 at 19:51
  • I am getting the following error now:java.lang.ClassCastException: org.omg.stub.java.rmi._Remote_Stub incompatible with com.test.EnvironmentEJB – jj sharma Mar 28 '16 at 20:00
  • You must use `PortableRemoteObject.narrow(o, EnvironmentEJB.class)`, where `o` is the result of the lookup. – Brett Kail Mar 29 '16 at 03:19
  • getting :java.lang.ClassCastException: cannot cast class org.omg.stub.java.rmi._Remote_Stub to class com.test.EnvironmentEJB. I am doing as Hashtable pdEnv = new Hashtable(); pdEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory"); pdEnv.put(Context.PROVIDER_URL, "iiop://localhost:2890"); Context context = new InitialContext(pdEnv); java.lang.Object ejbBusIntf = context.lookup("com.test.EnvironmentEJBRemote");EnvironmentEJB ejb = (EnvironmentEJB)javax.rmi.PortableRemoteObject.narrow(ejbBusIntf, EnvironmentEJB.class); – jj sharma Mar 29 '16 at 14:26
  • @jjsharma Can you update your post with the new code and full stack trace (including all "Caused by") of the ClassCastException you're currently getting? – Brett Kail Mar 29 '16 at 17:54
  • Do you have a local _EnvironmentEJB_Stub class (i.e., does `Class.forName("com.test._EnvironmentEJB_Stub")` work)? If not, you will need to use the createEJBStubs command to generate stubs for your client classpath. – Brett Kail Mar 29 '16 at 21:25
  • I followed link : http://www-01.ibm.com/support/docview.wss?uid=swg21393419 to create stubs. Class.for name does print the stub but I keep getting that cannot cast error. – jj sharma Mar 30 '16 at 16:51
  • Then I'm out of ideas, sorry. You might try opening a PMR with IBM to get more support. – Brett Kail Mar 30 '16 at 19:00