Skip to main content

Writing a Swing-EJB Client (with Netbeans 6.0)

3 replies [Last post]
janmueller
Offline
Joined: 2008-02-20

Hello folks!

I'm want to develop an Swing-based EJB-Client in the easiest possible way. How can I connect to the Session-Beans?

When I use Netbeans "Enterprise Ressources" -> "Call Enterprise Bean" Feature, it generates the following Code:

public class NewJFrame extends javax.swing.JFrame {
@EJB
private static AuftragsAbwicklungRemote auftragsAbwicklungBean;
....

The Main-Class is quite straight-forwarded:

package pi_praktikum1;
import javax.swing.JFrame;

public class Main {

public static void main(String[] args) {

JFrame gui = new NewJFrame();
gui.setVisible(Boolean.TRUE);
}
}

When I try to use the Session-Bean (even by starting the program with Webstart) I get an Null-Pointer Exception.

so my work-around is to develop a separate class, to access the Bean:

public class TestClass {
public AuftragsAbwicklungRemote lookupAuftragsAbwicklungBean() {
try {
Context c = new InitialContext();
return (AuftragsAbwicklungRemote) c.lookup("java:comp/env/AuftragsAbwicklungBean");
} catch (NamingException ne) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ne);
throw new RuntimeException(ne);
}
}

}

My Question is: "Is there a simpler way (directly from the NewJFrame-Class)"
and: "Why doesnt Dependency Injection work?"

I would be happy if someone could help me.

Greetings,
Jan

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Markus KARG

Read this: https://glassfish.dev.java.net/javaee5/docs/DG/beakt.html

Have Fun
Markus

glassfish@javadesktop.org schrieb:
> Hello folks!
>
> I'm want to develop an Swing-based EJB-Client in the easiest possible way. How can I connect to the Session-Beans?
>
> When I use Netbeans "Enterprise Ressources" -> "Call Enterprise Bean" Feature, it generates the following Code:
>
> public class NewJFrame extends javax.swing.JFrame {
> @EJB
> private static AuftragsAbwicklungRemote auftragsAbwicklungBean;
> ....
>
>
> The Main-Class is quite straight-forwarded:
>
> package pi_praktikum1;
> import javax.swing.JFrame;
>
> public class Main {
>
> public static void main(String[] args) {
>
> JFrame gui = new NewJFrame();
> gui.setVisible(Boolean.TRUE);
> }
> }
>
>
> When I try to use the Session-Bean (even by starting the program with Webstart) I get an Null-Pointer Exception.
>
> so my work-around is to develop a separate class, to access the Bean:
>
> public class TestClass {
> public AuftragsAbwicklungRemote lookupAuftragsAbwicklungBean() {
> try {
> Context c = new InitialContext();
> return (AuftragsAbwicklungRemote) c.lookup("java:comp/env/AuftragsAbwicklungBean");
> } catch (NamingException ne) {
> java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, "exception caught", ne);
> throw new RuntimeException(ne);
> }
> }
>
> }
>
> My Question is: "Is there a simpler way (directly from the NewJFrame-Class)"
> and: "Why doesnt Dependency Injection work?"
>
> I would be happy if someone could help me.
>
> Greetings,
> Jan
> [Message sent by forum member 'janmueller' (janmueller)]
>
> http://forums.java.net/jive/thread.jspa?messageID=262666
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

--
http://www.xing.com/go/invita/58469

[smime.p7s]

janmueller
Offline
Joined: 2008-02-20

Thanks a lot!

The hints and links where very useful.
At the end I just renamed the "NewJFrame"-Class to "Main", and now it works :-)

Greetings,
Jan

ksak
Offline
Joined: 2005-05-20

Hi Jan,

Java EE environment annotations and injection only work within certain kinds of managed classes. The Application Client main class is one example, but your NewJFrame is not, so the @EJB annotation is ignored in that case. You can move the @EJB to the main class and then pass the injected reference to your NewJFrame.

You can also directly lookup the global JNDI name of the target EJB instead of using an EJB dependency in java:comp/env. This gives up some portability but will allow you to access the EJB reference without defining a static EJB dependency. You can find more info about global JNDI names in our EJB FAQ :

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

--ken