请问如何用jb8+jboss3.x测试ejb?
--------------bean--------
package converterejb;
import javax.ejb.*;
public class ConverterBean implements SessionBean {
SessionContext sessionContext;
public void ejbCreate() throws CreateException {
/**@todo Complete this method*/
}
public void ejbRemove() {
/**@todo Complete this method*/
}
public void ejbActivate() {
/**@todo Complete this method*/
}
public void ejbPassivate() {
/**@todo Complete this method*/
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public double DollarToRMB(double dollars) {
/**@todo Complete this method*/
return dollars * 8.3000;
}
public double RMBToDollar(double RMB) {
/**@todo Complete this method*/
return RMB / 8.3000;
}
}
-------------------------------------------
------------------remote-------------------------
package converterejb;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
public interface Converter extends javax.ejb.EJBObject {
public double DollarToRMB(double dollars) throws RemoteException;
public double RMBToDollar(double RMB) throws RemoteException;
}
---------------------------------------------------
-----------------home-----------------
package converterejb;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
public interface ConverterHome extends javax.ejb.EJBHome {
public Converter create() throws CreateException, RemoteException;
}
-------------------------------------------
测试客户端是:
package converterejb;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import converterejb.Converter;
import converterejb.ConverterHome;
public class ConverterClient extends Object {
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private static final int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private ConverterHome converterHome = null;
private Converter converter = null;
//Construct the EJB test client
public ConverterClient() {
initialize();
}
public void initialize() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
}
try {
//get naming context
Context context = getJBossInitialContext();
//look up jndi name
Object ref = context.lookup("Converter");
//look up jndi name and cast to Home interface
converterHome = (ConverterHome) PortableRemoteObject.narrow(ref, ConverterHome.class);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded initializing local bean access through Local Home interface.");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed initializing bean access.");
}
e.printStackTrace();
}
}
private javax.naming.Context getJBossInitialContext() throws NamingException {
java.util.Hashtable JNDIParm = new java.util.Hashtable();
JNDIParm.put(Context.PROVIDER_URL, "localhost");
JNDIParm.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
return new InitialContext(JNDIParm);
}
//----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//----------------------------------------------------------------------------
public Converter create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
try {
converter = converterHome.create();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: create()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: create()");
}
e.printStackTrace();
}
if (logging) {
log("Return value from create(): " + converter + ".");
}
return converter;
}
//----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//----------------------------------------------------------------------------
public double DollarToRMB(double dollars) {
double returnValue = 0f;
if (converter == null) {
System.out.println("Error in DollarToRMB(): " + ERROR_NULL_REMOTE);
return returnValue;
}
long startTime = 0;
if (logging) {
log("Calling DollarToRMB(" + dollars + ")");
startTime = System.currentTimeMillis();
}
try {
returnValue = converter.DollarToRMB(dollars);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: DollarToRMB(" + dollars + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: DollarToRMB(" + dollars + ")");
}
e.printStackTrace();
}
if (logging) {
log("Return value from DollarToRMB(" + dollars + "): " + returnValue + ".");
}
return returnValue;
}
public double RMBToDollar(double RMB) {
double returnValue = 0f;
if (converter == null) {
System.out.println("Error in RMBToDollar(): " + ERROR_NULL_REMOTE);
return returnValue;
}
long startTime = 0;
if (logging) {
log("Calling RMBToDollar(" + RMB + ")");
startTime = System.currentTimeMillis();
}
try {
returnValue = converter.RMBToDollar(RMB);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: RMBToDollar(" + RMB + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: RMBToDollar(" + RMB + ")");
}
e.printStackTrace();
}
if (logging) {
log("Return value from RMBToDollar(" + RMB + "): " + returnValue + ".");
}
return returnValue;
}
public void executeRemoteCallsWithDefaultArguments() {
if (converter == null) {
System.out.println("Error in executeRemoteCallsWithDefaultArguments(): " + ERROR_NULL_REMOTE);
return ;
}
DollarToRMB(0f);
RMBToDollar(0f);
}
//----------------------------------------------------------------------------
// Utility Methods
//----------------------------------------------------------------------------
private void log(String message) {
if (message == null) {
System.out.println("-- null");
return ;
}
if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");
}
else {
System.out.println("-- " + message);
}
}
//Main method
public static void main(String[] args) {
try{
ConverterClient client = new ConverterClient();
Converter nowConverter = client.create();
double amount = nowConverter.DollarToRMB(100.00);
System.out.println(String.valueOf(amount));
}
catch (Exception e){
System.out.println("exception:"+e.toString());
}
}
}
我就在jb8的run->configurations里设了一个server,但vm和application参数什么都没填,用这个启动了jboss,另外一个就是生成测试程序自动生成的configuration,运行他,出现了错误,-- Failed: create()
-- Return value from create(): null.
exception:java.lang.NullPointerException