ejb@glassfish.java.net

RE: EJB3 injection in Glassfish

From: Mohanapriyanka_Dasari <Mohanapriyanka_Dasari_at_mahindrasatyam.net>
Date: Mon, 19 Oct 2009 19:16:50 +0530

Hi Eve,



this is how my EJB3 app looks :



Interface:



@Remote

public interface Hello{

public String sayHello(String name);

}



Implementation class:(Stateless Session Bean)



@Stateless

public class HelloBean implements Hello

{

public String sayHello(String name){

System.out.println("Hello"+name);

return "Hello!!!";

}

}



In the AppClient(JSP)



<%!

@EJB

private static Hello hello = null;

public Hello getHello(){

return hello;

}

%>

<%

hello.sayHello("XYZ");

%>



result:Null Pointer Exception..



is there anything i missed out?

________________________________
From: Eve Pokua [gorgeous65_at_msn.com]
Sent: Monday, October 19, 2009 6:53 PM
To: ejb glassfish
Subject: RE: EJB3 injection in Glassfish


Mohana,

Sorry correction -

3. hello.sayHello("xyz");
>





IT is supposed to be something like this -



Main.getHello().sayHello("xyz");



YOu call the static method by using Main as above.



I imagine you have a method similar -



sayHello(String s)



in your interface class Hello?



eve




________________________________
From: gorgeous65_at_msn.com
To: ejb_at_glassfish.dev.java.net
Date: Mon, 19 Oct 2009 10:13:19 +0100
Subject: RE: EJB3 injection in Glassfish



________________________________

From: Mohanapriyanka_Dasari_at_mahindrasatyam.net
To: ejb_at_glassfish.dev.java.net
Date: Mon, 19 Oct 2009 13:40:52 +0530
Subject: EJB3 injection in Glassfish

<

Hi Eve,

I followed the steps mentioned in the provided thread:

1. @EJB
private static Hello hello;

2.static Hello getHello() {
    return hello;
   }

3. hello.sayHello("xyz");
>





Try this:


Main.getHello("xyz");

eve


it did not work.. it showed null pointer exception..

Regards,
Priyanka.
________________________________
From: Eve Pokua [gorgeous65_at_msn.com]
Sent: Friday, October 16, 2009 8:31 PM
To: ejb glassfish
Subject: RE: EJB3 worked in Glassfish

Mohanna,

Take a look at this thread. I Had years of problems with injections and
it took me ages to get people to understand the problem I was experiencing.

Date: Mon, 13 Oct 2008 16:26:56 -0500
From: Timothy.Quinn_at_Sun.COM
To: ejb_at_glassfish.dev.java.net
CC: persistence_at_glassfish.dev.java.net
Subject: Re: injections

Eve,

It's important to note that the Java EE 5 spec specifies that injection take place only in "managed" classes. In the app client, there are only two managed classes: the main class (as specified either in the manifest or on the appclient command line) and the optional log-in callback class.

Also, the spec also mandates that in the app client main class only static elements can be injected.

Here is one general approach that some people find works for them:

1. Define the injected static fields on the app client's main class:

@EJB
private static UsersRemote usersRemote;

2. Write a static accessor method on the app client's main class:
static UserRemote getUserRemote() {
    return usersRemote;
}

3. From anywhere in any of the classes in your app client use Main.getUserRemote() when you need to use the EJB.

- Tim

Eve Pokua wrote:


Hello Ian/Everyone,

Yes,

I have carried out a lot more testing and found that this seems to work
on the first main class or any class that is run with appclient. Any other class
that I call from the main class, results as NullPointer when I try to insert. E.g:

If I try insert data of the same bean from the main class, I get the following:

C:\jee\STOCKINFOR2>appclient -client STOCKINFOR2-app-client.jar
successfully recorded new Department details

However, if I call the class from the main class, then I get the following:

C:\jee\STOCKINFOR2>appclient -client STOCKINFOR2-app-client.jar
Caught an Exception: can not insert new Department details
java.lang.NullPointerException
        at stockinfor2.NewDepartclient.saveNewDeptdetails(NewDepartclient.java:1
22)
        at stockinfor2.NewDepartclient.newdepatbutActionPerformed(NewDepartclien
t.java:113)

So I guess, I have to use just one JFrame client and use containers such as panels and so forth, to separate this.
Or I have to use lookups - Context.

This' now the limitation with server side JEE combined with Client side programming.

Thanks for your help.

eve



________________________________


> Date: Thu, 2 Oct 2008 14:06:29 -0700
> From: Ian.Evans_at_Sun.COM<mailto:Ian.Evans_at_Sun.COM>
> To: ejb_at_glassfish.dev.java.net<mailto:ejb_at_glassfish.dev.java.net>
> CC: persistence_at_glassfish.dev.java.net<mailto:persistence_at_glassfish.dev.java.net>
> Subject: Re: injections
>
> Eve Pokua wrote:
> > I followed your advice and created a whole new application. Took a copy
> > of the tutorial client:
> >
> > package newapplication;
> > import javax.ejb.EJB;
> > import machinedetails2.*;
> > /**
> > *
> > * @author Administrator
> > */
> > public class Main {
> >
> > @EJB
> > private static UsersRemote usersRemote;
> > public Main(String[] args) {
> > }
> > /**
> > * @param args the command line arguments
> > */
> > public static void main(String[] args) {
> > Main client = new Main(args);
> > client.doTest();
> > }
> >
> > public void doTest() {
> > try {
> > usersRemote.createUser("t7","testing7","null","null");
> > System.out.println("successfull");
> >
> > } catch (Exception ex) {
> > System.err.println("Caught a Exception: not working ");
> >
> > ex.printStackTrace();
> > //System.exit(0);
> > }
> > }
> > }
> >
> >
> >
> > amended it, tested it on my application and for the first time in
> > a hundred years it works.
> >
> > So, sorry everyone, it was all my mistake trying to mix swing user interface
> > with JEE 5 injection. I also took a look at the dukesbank application
> > and got
> > confuse with the BankAdmin.java swing application. Thought I could do it.
>
> You can use injection in Swing apps provided the application is run via
> the application client container (appclient in GlassFish). It's possible
> that problem you had with your original Swing app was your use of an
> injected resource in your nested class. Or you may not have instantiated
> the inner class before calling actionPerformed().
>
> In general, it's easier to test your server application with a simple
> console test client, as you did above, before coding the GUI application.
>
> > Thanking you all for your patience and excuse me if I offended anyone.
>
> No offense at all. Glad you got through one tough patch to getting your
> app running.
>
> -ian
> --
> Ian Evans
> ian dot evans at sun dot com
> Java EE technical documentation
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ejb-unsubscribe_at_glassfish.dev.java.net<mailto:ejb-unsubscribe_at_glassfish.dev.java.net>
> For additional commands, e-mail: ejb-help_at_glassfish.dev.java.net<mailto:ejb-help_at_glassfish.dev.java.net>
>

I am not sure if this is addressed in glassfish v3 though.

eve


________________________________
From: Mohanapriyanka_Dasari_at_mahindrasatyam.net
To: ejb_at_glassfish.dev.java.net
Date: Fri, 16 Oct 2009 20:19:11 +0530
Subject: RE: EJB3 worked in Glassfish

Tried with Injections(@EJB in client) when i wanted to try the EJB3 first application in Glassfish.
It did not work.

________________________________
From: Eve Pokua [gorgeous65_at_msn.com]
Sent: Friday, October 16, 2009 8:01 PM
To: ejb glassfish
Subject: RE: EJB3 worked in Glassfish

Or you could just use injections.

eve

________________________________
From: Mohanapriyanka_Dasari_at_mahindrasatyam.net
To: ejb_at_glassfish.dev.java.net
Date: Fri, 16 Oct 2009 19:36:12 +0530
Subject: EJB3 worked in Glassfish


Hi

It worked for me when i changed code like this:

@Stateless(mappedName="HelloBeanJNDI")
public class HelloBean implements Hello
{


and creating instance for Remote interface like this in JSP
hello = (Hello) ic.lookup("HelloBeanJNDI");

Thank you.

Priyanka

________________________________
DISCLAIMER:
This email (including any attachments) is intended for the sole use of the intended recipient/s and may contain material that is CONFIDENTIAL AND PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or distribution or forwarding of any or all of the contents in this message is STRICTLY PROHIBITED. If you are not the intended recipient, please contact the sender by email and delete all copies; your cooperation in this regard is appreciated.

________________________________
Use Windows Live Messenger for free on selected mobiles. Learn more.<http://clk.atdmt.com/UKM/go/174426567/direct/01/>

________________________________
DISCLAIMER:
This email (including any attachments) is intended for the sole use of the intended recipient/s and may contain material that is CONFIDENTIAL AND PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or distribution or forwarding of any or all of the contents in this message is STRICTLY PROHIBITED. If you are not the intended recipient, please contact the sender by email and delete all copies; your cooperation in this regard is appreciated.

________________________________
Chat to your friends for free on selected mobiles. Learn more.<http://clk.atdmt.com/UKM/go/174426567/direct/01/>

________________________________
DISCLAIMER:
This email (including any attachments) is intended for the sole use of the intended recipient/s and may contain material that is CONFIDENTIAL AND PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or distribution or forwarding of any or all of the contents in this message is STRICTLY PROHIBITED. If you are not the intended recipient, please contact the sender by email and delete all copies; your cooperation in this regard is appreciated.

________________________________
Chat to your friends for free on selected mobiles. Learn more.<http://clk.atdmt.com/UKM/go/174426567/direct/01/>
________________________________
Use Windows Live Messenger for free on selected mobiles. Learn more.<http://clk.atdmt.com/UKM/go/174426567/direct/01/>

________________________________
DISCLAIMER:
This email (including any attachments) is intended for the sole use of the intended recipient/s and may contain material that is CONFIDENTIAL AND PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or distribution or forwarding of any or all of the contents in this message is STRICTLY PROHIBITED. If you are not the intended recipient, please contact the sender by email and delete all copies; your cooperation in this regard is appreciated.