Hi All,
I started preparing for the SCBCD 5 recently and I started of with writing simple stateless bean, deploying and running. But I could not cross the initial step. Below are the details...
Remote Interface:
package com.calculator;
import javax.ejb.Remote;
@Remote
public interface StatelessCalculator {
public void add(int a, int b);
}
Bean Class:
package com.calculator;
import javax.ejb.Stateless;
@Stateless
public class StatelessCalculatorBean implements StatelessCalculator {
public void add(int a, int b){
System.out.println("Addition = " + (a+b));
}
}
Client Code:
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import com.calculator.StatelessCalculator;
public class CalculatorEJB3Test {
/**
* @param args
*/
public static void main(
String[] args) {
try {
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory" );
props.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces" );
Context ctx = new InitialContext( props );
System.out.println("context : " + ctx.getEnvironment());
Object ref = ctx.lookup("StatelessCalculatorBean");
System.out.println("ref = " + ref);
StatelessCalculator calc = (StatelessCalculator) PortableRemoteObject
.narrow(ref, StatelessCalculator.class);
calc.add(5, 2);
} catch (NamingException ne) {
ne.printStackTrace();
}
}
}
I created jar file out of remote interface and bean class and deployed in
jboss 4.2.3 (ie., placed jar file inside /server/default/deploy folder). When I run the client file I am getting the below error.
Output:
context : {jnp.parsedName=, java.naming.provider.url=localhost:1099, java.naming.factory.initial=org.jboss.naming.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jboss.naming}
ref = org.jnp.interfaces.NamingContext@1729854
Exception in thread "main" java.lang.ClassCastException
at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
at CalculatorEJB3Test.main(CalculatorEJB3Test.java:28)
Caused by: java.lang.ClassCastException: org.jnp.interfaces.NamingContext cannot be cast to org.omg.CORBA.Object
at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:212)
... 2 more
I am getting the same error even if I created InitialContext object without setting any properties. I am not able to find out what is the problem. I am very glad if somebody help me in crossing my initial step. I would request someone to take a minute and provide me the solution.
Thanks in advance,
Chinna