Solved

Tutorial for using EJBs from Standalone app

Posted on 2006-06-28
36
418 Views
Last Modified: 2013-11-24
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
0
Comment
Question by:JamixOy
  • 13
  • 9
  • 9
  • +1
36 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 17007344
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
 

Author Comment

by:JamixOy
ID: 17007520
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
 
LVL 8

Expert Comment

by:colr__
ID: 17007563
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
 
LVL 35

Expert Comment

by:girionis
ID: 17007564
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
 
LVL 3

Expert Comment

by:ss_p
ID: 17007691
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
 
LVL 35

Expert Comment

by:girionis
ID: 17007712
Right, how is that different from my code?
0
 
LVL 8

Expert Comment

by:colr__
ID: 17007831
Yours didnt call RO ro = home.create();

Lets not fight guys....
0
 
LVL 35

Expert Comment

by:girionis
ID: 17007877
> 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
 
LVL 8

Expert Comment

by:colr__
ID: 17007882
>> Lets not fight guys....

I agree with you btw, there isnt any value in printing the same code twice.
0
 
LVL 35

Expert Comment

by:girionis
ID: 17007902
We are not fighting, we are discussing :)
0
 

Author Comment

by:JamixOy
ID: 17007983
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
 
LVL 3

Expert Comment

by:ss_p
ID: 17007992
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
 
LVL 8

Expert Comment

by:colr__
ID: 17008000
>>enoug  on that...

Could you not attempt to do it all in netbeans?

Can you show us the stack trace?
0
 
LVL 35

Expert Comment

by:girionis
ID: 17008019
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
 
LVL 35

Expert Comment

by:girionis
ID: 17008025
> 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
 
LVL 3

Expert Comment

by:ss_p
ID: 17008027
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
 
LVL 35

Expert Comment

by:girionis
ID: 17008032
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
 
LVL 3

Expert Comment

by:ss_p
ID: 17008040
sorry

it should be

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

this actually creates a home object.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:JamixOy
ID: 17008041
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
 
LVL 8

Expert Comment

by:colr__
ID: 17008051
Are you using a local or remote interface in your ejb?
0
 
LVL 8

Expert Comment

by:colr__
ID: 17008060
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
 
LVL 8

Assisted Solution

by:colr__
colr__ earned 150 total points
ID: 17008067
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
 
LVL 35

Expert Comment

by:girionis
ID: 17008070
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
 
LVL 3

Expert Comment

by:ss_p
ID: 17008192
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
 
LVL 35

Assisted Solution

by:girionis
girionis earned 150 total points
ID: 17008241
>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
 
LVL 3

Accepted Solution

by:
ss_p earned 150 total points
ID: 17008265
have you got it working

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

0
 

Author Comment

by:JamixOy
ID: 17008384
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
 
LVL 3

Expert Comment

by:ss_p
ID: 17008418
You are welcome
0
 
LVL 35

Expert Comment

by:girionis
ID: 17008426
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
 
LVL 3

Expert Comment

by:ss_p
ID: 17016910
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
 
LVL 3

Expert Comment

by:ss_p
ID: 17016924
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
 
LVL 35

Expert Comment

by:girionis
ID: 17016974
??????

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
 

Author Comment

by:JamixOy
ID: 17017114
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
 
LVL 8

Expert Comment

by:colr__
ID: 17017282
I find it amasing how fickle humans can sometimes be. Thanks JamixOy, glad we helped! ;-)
0
 
LVL 35

Expert Comment

by:girionis
ID: 17017314
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
 
LVL 8

Expert Comment

by:colr__
ID: 17017367
lol, I like the battlefield analogy.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now