Unable to access EJB server from client 2
// Client 1
package nepe.client;
import javax.ejb.EJB;
import nepe.ejb.Nepe;
/**
*
* @author java
*/
public class NepeClient {
@EJB
static Nepe nepe;
/** Creates a new instance of NepeClient */
public NepeClient(String[] args) {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
NepeClient client = new NepeClient(args);
client.doTestClient();
client.doTestHijo();
}
public void doTestClient() {
try {
String strDato = "00";
strDato = nepe.rutina(strDato);
if (strDato.equals("ERROR")) {
System.out.println(" RESULTADO " + strDato);
} else {
System.out.println(" RESULTADO " + strDato);
}
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
System.exit(1);
}
}
public void doTestHijo() {
try {
NepeHijo hijo = new NepeHijo();
hijo.busca();
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
System.exit(1);
}
}
}
// client 2
package nepe.client;
import javax.ejb.EJB;
import nepe.ejb.Nepe;
/**
*
* @author java
*/
public class NepeHijo {
@EJB
static Nepe nepe;
/** Creates a new instance of NepeHijo */
public NepeHijo() {
}
public void busca() {
try {
String strDato = "00";
strDato = nepe.rutina(strDato);
if (strDato.equals("ERROR")) {
System.out.println(" RESULTADO " + strDato);
} else {
System.out.println(" RESULTADO " + strDato);
}
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
System.exit(1);
}
}
}
// interface
package nepe.ejb;
import javax.ejb.Remote;
@Remote
public interface Nepe {
public String rutina(String strRutina);
public void remove();
}
// bean
package nepe.ejb;
import javax.ejb.Remove;
import javax.ejb.Stateful;
@Stateful()
public class NepeBean implements Nepe {
public String rutina(String strDato) {
if (strDato.equals("00")) {
strDato = "RECIBI 00";
} else {
strDato = "PROBLEMA";
}
return strDato;
}
@Remove()
public void remove() {
}
}
appclient -client Nepe-app-client.jar
RESULTADO RECIBI 00
Caught an unexpected exception!
java.lang.NullPointerException
at nepe.client.NepeHijo.busca(NepeHijo.java:21)
at nepe.client.NepeClient.doTestHijo(NepeClient.java:47)
at nepe.client.NepeClient.main(NepeClient.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.enterprise.util.Utility.invokeApplicationMain(Utility.java:232)
at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:329)
at com.sun.enterprise.appclient.Main.main(Main.java:180)
# 3
// environmet
java jdk1.6.0_01
sun java system application server platform edition 9.0_01 (build b14)
netbeans 5.5
// trace
appclient -client Nepe-app-client.jar
RESULTADO RECIBI 00
Caught an unexpected exception!
java.lang.NullPointerException
at nepe.client.NepeHijo.busca(NepeHijo.java:21)
at nepe.client.NepeClient.doTestHijo(NepeClient.java:47)
at nepe.client.NepeClient.main(NepeClient.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.enterprise.util.Utility.invokeApplicationMain(Utility.java:232)
at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:329)
at com.sun.enterprise.appclient.Main.main(Main.java:180)
// Client 1
package nepe.client;
import javax.ejb.EJB;
import nepe.ejb.Nepe;
/**
*
* @author java
*/
public class NepeClient {
@EJB
static Nepe nepe;
/** Creates a new instance of NepeClient */
public NepeClient(String[] args) {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
NepeClient client = new NepeClient(args);
client.doTestClient();
client.doTestHijo();
}
public void doTestClient() {
try {
String strDato = "00";
strDato = nepe.rutina(strDato);
if (strDato.equals("ERROR")) {
System.out.println(" RESULTADO " + strDato);
} else {
System.out.println(" RESULTADO " + strDato);
}
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
System.exit(1);
}
}
public void doTestHijo() {
try {
NepeHijo hijo = new NepeHijo();
hijo.busca();
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
System.exit(1);
}
}
}
// client 2
package nepe.client;
import javax.ejb.EJB;
import nepe.ejb.Nepe;
/**
*
* @author java
*/
public class NepeHijo {
@EJB
static Nepe nepe;
/** Creates a new instance of NepeHijo */
public NepeHijo() {
}
public void busca() {
try {
String strDato = "00";
strDato = nepe.rutina(strDato);// < null pointer exception
if (strDato.equals("ERROR")) {
System.out.println(" RESULTADO " + strDato);
} else {
System.out.println(" RESULTADO " + strDato);
}
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
System.exit(1);
}
}
}
// interface
package nepe.ejb;
import javax.ejb.Remote;
@Remote
public interface Nepe {
public String rutina(String strRutina);
public void remove();
}
// bean
package nepe.ejb;
import javax.ejb.Remove;
import javax.ejb.Stateful;
@Stateful()
public class NepeBean implements Nepe {
public String rutina(String strDato) {
if (strDato.equals("00")) {
strDato = "RECIBI 00";
} else {
strDato = "PROBLEMA";
}
return strDato;
}
@Remove()
public void remove() {
}
}
# 4
Annotations are only processed on the Application Client main class
(and any super-classes).The @EJB annotation on the supporting
class , NepeHijo, is ignored, which is why it's null.
However, any classes called from the Application Client main class
are also available to the supporting class since it runs in the same
component environment. An easy way to access the same ejb
dependency is to add the name() attribute to the @EJB declaration
in the main class :
@EJB(name="nepe_ref") static Nepe nepe;
Then, in the supporting class, look up the ejb dependency via
the Java EE component environment :
InitialContext ic = new InitialContext();
Nepe nepe = (Nepe) ic.lookup("java:comp/env/nepe_ref");
Note how the name relative to java:comp/env/ matches the
name attribute of the @EJB annotation.
--ken