• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Starting problem

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
author
Posts: 580
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chinna,

Remove the request to narrow the remote object. It is no longer needed in EJB 3.0.

Hope it helps,
Reza
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No more look up to the Remote Home interfaces. Direct on the Remote Interface.
 
Chinababu Illa
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Reza and Jyothi,

Thanks for your reply. The reasons for failure are two things.
1. jndi name was wrong, it expected "StatelessCalculatorBean/remote" instead of "StatelessCalculatorBean".
2. I had to add jar files from "jboss-4.2.3.GA\client" folder from jboss directory.

After I set these two it started working fine. One observation is if I give wrong jndi name for lookup, it's returning "org.jnp.interfaces.NamingContext" object instead of remote object.

Thanks both of you.
Chinna
 
Can't .... do .... plaid .... So I did this tiny ad instead:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic