Accessing stateless session bean from a main class with in the same package

866 Views Asked by At

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)
1

There are 1 best solutions below

12
Brett Kail On

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.