当前位置: 技术问答>java相关
我怎么运行(调用)我的EJB呀(在发布和部署后)?
来源: 互联网 发布时间:2015-11-17
本文导语: 当然,我的意思不是说在Jbuilder中运行它,意思是说我是不是要用jsp/servlet才可以访问这些发布和部署后的程序呀?如,我可以用类似http://localhost:7001/.../来访问EJB吗?可以,怎么访问呀?或者有什么其它的方法。 等...
当然,我的意思不是说在Jbuilder中运行它,意思是说我是不是要用jsp/servlet才可以访问这些发布和部署后的程序呀?如,我可以用类似http://localhost:7001/.../来访问EJB吗?可以,怎么访问呀?或者有什么其它的方法。
等待。。。。
谢谢。
等待。。。。
谢谢。
|
package ejbtest2;
import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
public class ejbLJTestClient1 {
static final private String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
static final private int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private ejbLJHome ejbLJHomeObject = null;
private ejbLJ ejbLJObject = null;
//Construct the EJB test client
public ejbLJTestClient1() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
}
try {
//get naming context
Context ctx = getInitialContext();
//look up jndi name
Object ref = ctx.lookup("ejbLJ");
//cast to Home interface
ejbLJHomeObject = (ejbLJHome) PortableRemoteObject.narrow(ref, ejbLJHome.class);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded initializing bean access.");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed initializing bean access.");
}
e.printStackTrace();
}
}
private Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
String user = null;
String password = null;
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
}
return new InitialContext(properties);
}
catch(Exception e) {
log("Unable to connect to WebLogic server at " + url);
log("Please make sure that the server is running.");
throw e;
}
}
//----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//----------------------------------------------------------------------------
public ejbLJ create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
try {
ejbLJObject = ejbLJHomeObject.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(): " + ejbLJObject + ".");
}
return ejbLJObject;
}
//----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//----------------------------------------------------------------------------
public String tellLJ() {
String returnValue = "";
if (ejbLJObject == null) {
System.out.println("Error in tellLJ(): " + ERROR_NULL_REMOTE);
return returnValue;
}
long startTime = 0;
if (logging) {
log("Calling tellLJ()");
startTime = System.currentTimeMillis();
}
try {
returnValue = ejbLJObject.tellLJ();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: tellLJ()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: tellLJ()");
}
e.printStackTrace();
}
if (logging) {
log("Return value from tellLJ(): " + returnValue + ".");
}
return returnValue;
}
public void testRemoteCallsWithDefaultArguments() {
if (ejbLJObject == null) {
System.out.println("Error in testRemoteCallsWithDefaultArguments(): " + ERROR_NULL_REMOTE);
return ;
}
tellLJ();
}
//----------------------------------------------------------------------------
// 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) {
ejbLJTestClient1 client = new ejbLJTestClient1();
// Use the client object to call one of the Home interface wrappers
// above, to create a Remote interface reference to the bean.
// If the return value is of the Remote interface type, you can use it
// to access the remote interface methods. You can also just use the
// client object to call the Remote interface wrappers.
client.create();
System.out.println(client.tellLJ());
}
}
import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
public class ejbLJTestClient1 {
static final private String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
static final private int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private ejbLJHome ejbLJHomeObject = null;
private ejbLJ ejbLJObject = null;
//Construct the EJB test client
public ejbLJTestClient1() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
}
try {
//get naming context
Context ctx = getInitialContext();
//look up jndi name
Object ref = ctx.lookup("ejbLJ");
//cast to Home interface
ejbLJHomeObject = (ejbLJHome) PortableRemoteObject.narrow(ref, ejbLJHome.class);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded initializing bean access.");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed initializing bean access.");
}
e.printStackTrace();
}
}
private Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
String user = null;
String password = null;
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
}
return new InitialContext(properties);
}
catch(Exception e) {
log("Unable to connect to WebLogic server at " + url);
log("Please make sure that the server is running.");
throw e;
}
}
//----------------------------------------------------------------------------
// Methods that use Home interface methods to generate a Remote interface reference
//----------------------------------------------------------------------------
public ejbLJ create() {
long startTime = 0;
if (logging) {
log("Calling create()");
startTime = System.currentTimeMillis();
}
try {
ejbLJObject = ejbLJHomeObject.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(): " + ejbLJObject + ".");
}
return ejbLJObject;
}
//----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//----------------------------------------------------------------------------
public String tellLJ() {
String returnValue = "";
if (ejbLJObject == null) {
System.out.println("Error in tellLJ(): " + ERROR_NULL_REMOTE);
return returnValue;
}
long startTime = 0;
if (logging) {
log("Calling tellLJ()");
startTime = System.currentTimeMillis();
}
try {
returnValue = ejbLJObject.tellLJ();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: tellLJ()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: tellLJ()");
}
e.printStackTrace();
}
if (logging) {
log("Return value from tellLJ(): " + returnValue + ".");
}
return returnValue;
}
public void testRemoteCallsWithDefaultArguments() {
if (ejbLJObject == null) {
System.out.println("Error in testRemoteCallsWithDefaultArguments(): " + ERROR_NULL_REMOTE);
return ;
}
tellLJ();
}
//----------------------------------------------------------------------------
// 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) {
ejbLJTestClient1 client = new ejbLJTestClient1();
// Use the client object to call one of the Home interface wrappers
// above, to create a Remote interface reference to the bean.
// If the return value is of the Remote interface type, you can use it
// to access the remote interface methods. You can also just use the
// client object to call the Remote interface wrappers.
client.create();
System.out.println(client.tellLJ());
}
}