【Java开源代码栏目提醒】:网学会员为广大网友收集整理了,CmpKeysClientBeanTestClient1.java,希望对大家有所帮助!
package cmpkeys;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import javax.naming.InitialContext;
import java.util.Hashtable;
public class CmpKeysClientBeanTestClient1 {
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 CmpKeysClient cmpKeysClient = null;
private CmpKeysClientHome cmpKeysClientHome = null;
//Construct the EJB test client
public CmpKeysClientBeanTestClient1() {
initialize();
}
public void initialize() {
try {
//get naming context
Context context = getInitialContext();
//look up jndi name
Object ref = context.lookup("CmpKeysClientBean");
//look up jndi name and cast to Home interface
cmpKeysClientHome = (CmpKeysClientHome) PortableRemoteObject.narrow(
ref, CmpKeysClientHome.class);
PersonPK pk = new PersonPK("f1", "p1");
cmpKeysClient =create();
displayPerson(pk);
} catch (Exception e) {
e.printStackTrace();
}
}
private Context getInitialContext() throws NamingException {
Hashtable environment = new Hashtable();
environment.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
environment.put(Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
environment.put(Context.PROVIDER_URL, "jnp://localhost:1099");
return new InitialContext(environment);
}
//----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//----------------------------------------------------------------------------
public CmpKeysClient create() {
try {
cmpKeysClient = cmpKeysClientHome.create();
} catch (Exception e) {
e.printStackTrace();
}
return cmpKeysClient;
}
//----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//----------------------------------------------------------------------------
public void displayPerson(PersonPK pk) {
if (cmpKeysClient == null) {
System.out.println("Error in displayPerson(): " + ERROR_NULL_REMOTE);
return;
}
try {
cmpKeysClient.displayPerson(pk);
} catch (Exception e) {
e.printStackTrace();
}
}
//----------------------------------------------------------------------------
// Utility Methods
//----------------------------------------------------------------------------
public CmpKeysClientHome getHome() {
return cmpKeysClientHome;
}
//Main method
public static void main(String[] args) {
CmpKeysClientBeanTestClient1 client = new CmpKeysClientBeanTestClient1();
// Use the getHome() method of the client object to call Home interface
// methods that will return a Remote interface reference. Then
// use that Remote interface reference to access the EJB.
}
}
上一篇:
CmpKeysClientBean.java
下一篇:
临床前药物安全性评价中毒性病理学新技术的应用