Tutorial for using EJBs from Standalone app

Hi,

Could somebody point me to a tutorial/docs for standalone Java application using EJBs?

I am having trouble with using EJBs from an app. Currently I am using JBuilder for gui, NetBeans for EJBs and JBoss for EJB server.

Here is what i am trying to do:

GUI tries to use EJB called ROBean.

      Properties JNDIParm = new Properties() ;
      JNDIParm.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
      JNDIParm.setProperty(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
      JNDIParm.setProperty(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
      Context ctx = new InitialContext(JNDIParm);

      Object ref = (EJBHome) ctx.lookup("ROBean");

      ROBean home = (ROBean)PortableRemoteObject.narrow( ref, Class.forName( "ROBean" ) ) ;
      System.out.println(home.getString());

And the EJB looks like this:

public class ROBean implements SessionBean, RORemoteBusiness {
    private SessionContext context;
   
    public void setSessionContext(SessionContext aContext) {
        context = aContext;
    }
   
    public void ejbActivate() { }
    public void ejbPassivate() { }
    public void ejbRemove() { }
    public void ejbCreate() { }
   
    private ejb.RO1LocalHome lookupRO1Bean() {
        try {
            javax.naming.Context c = new javax.naming.InitialContext();
            ejb.RO1LocalHome rv = (ejb.RO1LocalHome) c.lookup("java:comp/env/ejb/RO1Bean");
            return rv;
        }
        catch(javax.naming.NamingException ne) {
            java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,"exception caught" ,ne);
            throw new RuntimeException(ne);
        }
    }

    public String getString() {
        return "testing..";
    }
}


Regards,
Mika
JamixOyAsked:
Who is Participating?
 
ss_pConnect With a Mentor Commented:
have you got it working

if you have any problems let us know. We can help you out

0
 
girionisCommented:
What problems do you have? The only thing I could spot is this

> ROBean home = (ROBean)PortableRemoteObject.narrow( ref, Class.forName( "ROBean" ) ) ;

which should be

ROBean home = (ROBean)PortableRemoteObject.narrow( ref, ROBean.class) ;
0
 
JamixOyAuthor Commented:
Both gives me a java.lang.ClassCastException.

I have been reading about EJBs for a few days, this is my first EJB, so I have no idea what's wrong with it. All of my docs are about Enterprise web clients using EJB.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
colr__Commented:
tbh, starting off with application based clients is starting the hard way. The easy way to use EJBs is to have the EJB and the (web) client both in the same container (JBoss) using local interfaces. If you want to explore after that, you would then try accessing the EJBs using remomte interfaces, and then you might want to consider standalone applications accessing EJBs.

There are detailed instructions here, but again you might want to reconsider your learning track:

https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB.

I tried learning J2EE in the same way you are at the start, but couldnt get it to work and so settled for web based client access.

colr__
0
 
girionisCommented:
It is probably because you are casting the jndi reference to the "ROBean", try the code below and tell me if it helps

Context ctx = new InitialContext(JNDIParm);
Object ref = ctx.lookup("ROBean");
ROBean home = (ROBean)PortableRemoteObject.narrow( ref,ROBean.class) ) ;
System.out.println(home.getString());
0
 
ss_pCommented:
try this one

Context ctx = new InitialContext(JNDIParm);
Object ref = ctx.lookup("ROBean");
ROBean home = (ROBean)PortableRemoteObject.narrow( ref,ROBean.class) ) ;
RO ro = home.create();

System.out.println(ro.getString());

I think this code should work

Please let me know if you need more help
0
 
girionisCommented:
Right, how is that different from my code?
0
 
colr__Commented:
Yours didnt call RO ro = home.create();

Lets not fight guys....
0
 
girionisCommented:
> Yours didnt call RO ro = home.create();

It is not necessary to call it since the asker does not want to create a remote object. The problems is a ClassCastException and I cannot see how the create() method would solve it.
0
 
colr__Commented:
>> Lets not fight guys....

I agree with you btw, there isnt any value in printing the same code twice.
0
 
girionisCommented:
We are not fighting, we are discussing :)
0
 
JamixOyAuthor Commented:
Hi and thanks for quick replys,

Still I am getting the same error (ClassCastException). It is coming from the 'PortableRemoteObject.narrow( ref,ROBean.class);' row. Object ref return 'ROBeanHome' string when printed out.

Could it be something to do with that I am using NetBeans to compile EJB and JBuilder to compile GUI? We were thinking about upgrading our jBuilder to Enterprise version, so all development could be done by one tool. Also yesterday I got to a point where It looked like the EJB could be executed, but then there came SecurityException...

/ mika
0
 
ss_pCommented:
girionis

I just want to help our friend out in giving the solution.

You are an expert. Let help each other than fighting

ssp
0
 
colr__Commented:
>>enoug  on that...

Could you not attempt to do it all in netbeans?

Can you show us the stack trace?
0
 
girionisCommented:
Try

Context ctx = new InitialContext(JNDIParm);
Object ref = ctx.lookup("ROBean");
ROBeanHome home = (ROBean)PortableRemoteObject.narrow( ref,ROBeanHome.class) ) ;
System.out.println(home.getString());
0
 
girionisCommented:
> I just want to help our friend out in giving the solution.

I understand that but lets not repeat things previously said/mentioned by other participants.
0
 
ss_pCommented:
hi

You should have a home interface ROHome

can you change

ROBean home = (ROBean)PortableRemoteObject.narrow( ref,ROBean.class) ) ;

to

ROHome home = (ROBean)PortableRemoteObject.narrow( ref,ROHome.class) ) ;

I think this should solve your problem

SSP
0
 
girionisCommented:
Sorry, typo:

Context ctx = new InitialContext(JNDIParm);
Object ref = ctx.lookup("ROBean");
ROBeanHome home = (ROBeanHome )PortableRemoteObject.narrow( ref,ROBeanHome.class) ) ;
System.out.println(home.getString());
0
 
ss_pCommented:
sorry

it should be

ROHome home = (ROHome )PortableRemoteObject.narrow( ref,ROHome.class) ) ;

this actually creates a home object.
0
 
JamixOyAuthor Commented:
Hi,

NetBeans gave more info in stacktrace, so here it is:

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 myjavaapplication.Main.main(Main.java:47)
Caused by: java.lang.ClassCastException: Class ejb.ROBean is not a valid remote interface
        at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:225)
        ... 2 more

Not a valid remote interface.. What does this mean?

/ mika
0
 
colr__Commented:
Are you using a local or remote interface in your ejb?
0
 
colr__Commented:
It looks like your ejb might be setup with a local interface, which doesnt allow connections from entites outside the container. You application is attempting to connect using a remote interface that thetefore doesnt exists.
0
 
colr__Connect With a Mentor Commented:
In ejb-jar.xml, can you show me the xml for the bean, something like this:

<session>
  <display-name>AddressesSB</display-name>
  <ejb-name>AddressesBean</ejb-name>
  <local-home>newsline.ejb.AddressesLocalHome</local-home>
  <local>newsline.ejb.AddressesLocal</local>
  <ejb-class>newsline.ejb.AddressesBean</ejb-class>
  <session-type>Stateless</session-type>
  <transaction-type>Container</transaction-type>
  </session>
0
 
girionisCommented:
Can you post the code of the interfaces and the bean class of your ejb? ALthough I think my comment on {http:#17008032} should help you.
0
 
ss_pCommented:
if i am not wrong

you should have

RO.java
ROHome.java
ROBean.java
ROLocal.java
ROLocalHome.java

if these are the class you are using then my comment should work.

girionis,

i dont know how your code would work.

please can you explain.

I am sorry I am newbie
0
 
girionisConnect With a Mentor Commented:
>i dont know how your code would work.
>
>please can you explain.

The user stated somewhere that the object he is getting is ROBeanHome so I think narrowing it down to this kind of object should work.
0
 
JamixOyAuthor Commented:
Hi,

Got it working!! The solution was as follows:

      Properties JNDIParm = new Properties() ;
      JNDIParm.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
      JNDIParm.setProperty(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");
      JNDIParm.setProperty(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");

      Context ctx = new InitialContext(JNDIParm);
      Object ref = ctx.lookup("ROBean");
     
      RORemoteHome home = (RORemoteHome )PortableRemoteObject.narrow( ref,RORemoteHome.class);
      RORemote ro = home.create();
      System.out.println( ro.getRes1() );

Thank you all very much. I guess this is the way to use EJBs from standalone application.
Now I am just getting started with EJBs so I think questions WILL rise shortly again in another thread. :)

Best Regards,
Mika
0
 
ss_pCommented:
You are welcome
0
 
girionisCommented:
Nice you got is solved, please make sure next time you accept correct comments as answers since these questions and solutions might be helpful to other users in the future.
0
 
ss_pCommented:
my friend

Please try to mention why you have accepted it seems people give wrong comments and try to post comments which tries to hurt others who try to help.

0
 
ss_pCommented:
Hi girionis

I know that you are an expert.

But unfortunately if you read the comments you had not tried to post the correct answer. But just copied and pasted solutions from some where else.

There is a proverb in Tamil Language "Yannai kum Adi serukkum"

what it means it,

Even THE Mighty Elephant One day might be defeated.

Therefore try to accept the comments posted by newbies as well

Cheers/SSP
0
 
girionisCommented:
??????

Who talked about defeat? I just said the user should have accepted valid comments (not just mine, all of ours). Your comment and my comment *are not* valid since they do not lead to a solution.

And since you started talking about proverbs, we have another one in the greek language which goes "ela papoy na soy deiksw ta ampeloxwrafa soy" and is used ironically which means "come grandfather, I will show you your vineyards".

Therefore you are trying to tell me what comments are valid and what not.
0
 
JamixOyAuthor Commented:
Hi girionis and ss_p,

Let me explain a bit why I accepted those answers.

In this thread there is no _one_ post that lead me to the solution(,but all together). I could have splitted the points to all posts, but that would have propably taken quite a while using complicated calculation.. :) I accepted the most recent post (near to the solution I've posted) and one answer for each person trying to help me.

Cheers,
Mika
0
 
colr__Commented:
I find it amasing how fickle humans can sometimes be. Thanks JamixOy, glad we helped! ;-)
0
 
girionisCommented:
I hope you are talking about ss_p, (s)he thinks this is a battle field, that's why (s)he is talking about "defeat"!!!!
0
 
colr__Commented:
lol, I like the battlefield analogy.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.