相关的文件代码如下:
Count.java:
package count;import javax.ejb.*;
import java.util.*;
import java.rmi.*;public interface Count extends javax.ejb.EJBObject {
public int Count() throws RemoteException;
}CountBean.java:
package count;import javax.ejb.*;public class CountBean implements SessionBean { SessionContext sessionContext;
public int val; public void ejbCreate(int val) throws CreateException {
this.val =val;
System.out.println("ejbCreate()") ;
} public void ejbRemove() {
System.out.println("ejbRemove") ;
} public void ejbActivate() {
System.out.println("ejbActivate") ;
} public void ejbPassivate() {
System.out.println("ejbPassivate") ;
} public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
} public int Count()
{
System.out.println("Count()") ;
return ++val;
}}CountHome.java:
package count;import javax.ejb.*;
import java.util.*;
import java.rmi.*;public interface CountHome extends javax.ejb.EJBHome {
public Count create(int val) throws CreateException, RemoteException;
}
Count.java:
package count;import javax.ejb.*;
import java.util.*;
import java.rmi.*;public interface Count extends javax.ejb.EJBObject {
public int Count() throws RemoteException;
}CountBean.java:
package count;import javax.ejb.*;public class CountBean implements SessionBean { SessionContext sessionContext;
public int val; public void ejbCreate(int val) throws CreateException {
this.val =val;
System.out.println("ejbCreate()") ;
} public void ejbRemove() {
System.out.println("ejbRemove") ;
} public void ejbActivate() {
System.out.println("ejbActivate") ;
} public void ejbPassivate() {
System.out.println("ejbPassivate") ;
} public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
} public int Count()
{
System.out.println("Count()") ;
return ++val;
}}CountHome.java:
package count;import javax.ejb.*;
import java.util.*;
import java.rmi.*;public interface CountHome extends javax.ejb.EJBHome {
public Count create(int val) throws CreateException, RemoteException;
}
解决方案 »
- struts2处理结果返回页面(action)刷新提示重新提交,地址栏回车出现500错误。
- 页面跳转问题
- hibernate无法建表,求解
- java 多线程实际应用的问题
- SSH集成,报错,请大家帮帮忙
- 关于eclipse下编写xml的DTD文件出现的编码问题。
- TOMCAT 5 的用法 ???????????????? (初学者)
- 十里开源最新决策『十里开源__http://www.opensource.com.cn』
- 请问谁用过castor和jdom?
- 模仿ibatis动态sql时,对于命名sql配置文件更改后,怎么及时知道或者监控配置文件发生变化,从而刷新我的缓存
- EJB的问题(高分请教)
- 初学EJB,javax.ejb.*; 应该将哪个library 放进来
package count;import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
import javax.ejb.*;
public class CountClient {
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 CountHome countHome = null;
private Count count = null; //Construct the EJB test client
public CountClient() {
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("Count"); //cast to Home interface
countHome = (CountHome) PortableRemoteObject.narrow(ref, CountHome.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 Count create(int val) {
long startTime = 0;
if (logging) {
log("Calling create(" + val + ")");
startTime = System.currentTimeMillis();
}
try {
count = countHome.create(val);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: create(" + val + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: create(" + val + ")");
}
e.printStackTrace();
} if (logging) {
log("Return value from create(" + val + "): " + count + ".");
}
return count;
} //----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//---------------------------------------------------------------------------- public int Count() {
int returnValue = 0;
if (count == null) {
System.out.println("Error in Count(): " + ERROR_NULL_REMOTE);
return returnValue;
}
long startTime = 0;
if (logging) {
log("Calling Count()");
startTime = System.currentTimeMillis();
} try {
returnValue = count.Count();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: Count()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: Count()");
}
e.printStackTrace();
} if (logging) {
log("Return value from Count(): " + returnValue + ".");
}
return returnValue;
} //----------------------------------------------------------------------------
// 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) {
CountClient client = new CountClient();
}
}
你的这个BEAN是从别的地方下载的吧?
你没必要把那个CLIENT弄到JB6中去
你可以在命令行编译他们的
你把REMOTE、HOME、EJB、CLIENT这几个文件放到同一个目录中
用:
javac -d . -classpath 你的CLASSPATH(最好把weblogic.jar写上) *.java
就可以编译成功了,运行CLIENT
java 包名.你的CLIENT的CLASS名
你就可以看到结果了
package count;import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
import javax.ejb.*;
public class CountClient {
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 CountHome countHome = null;
private Count count = null; //Construct the EJB test client
public CountClient() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
}
}
/************************************///add the method
public CountHome getCountHome() throws Exception
{
try {
//get naming context
Context ctx = getInitialContext(); //look up jndi name
Object ref = ctx.lookup("Count"); //cast to Home interface
countHome = (CountHome) PortableRemoteObject.narrow(ref, CountHome.class);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded initializing bean access.");
log("Execution time: " + (endTime - startTime) + " ms.");
}
return countHome;
}
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 Count create(int val) {
long startTime = 0;
if (logging) {
log("Calling create(" + val + ")");
startTime = System.currentTimeMillis();
}
try {
count = countHome.create(val);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: create(" + val + ")");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: create(" + val + ")");
}
e.printStackTrace();
} if (logging) {
log("Return value from create(" + val + "): " + count + ".");
}
return count;
} //----------------------------------------------------------------------------
// Methods that use Remote interface methods to access data through the bean
//---------------------------------------------------------------------------- public int Count() {
int returnValue = 0;
if (count == null) {
System.out.println("Error in Count(): " + ERROR_NULL_REMOTE);
return returnValue;
}
long startTime = 0;
if (logging) {
log("Calling Count()");
startTime = System.currentTimeMillis();
} try {
returnValue = count.Count();
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded: Count()");
log("Execution time: " + (endTime - startTime) + " ms.");
}
}
catch(Exception e) {
if (logging) {
log("Failed: Count()");
}
e.printStackTrace();
} if (logging) {
log("Return value from Count(): " + returnValue + ".");
}
return returnValue;
} //----------------------------------------------------------------------------
// 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) {
CountClient client = new CountClient();
CountHome=Home=client.getCountHome();
Count count[]=new Count[3];
int countVal=0;
System.out.println("Instantiating Bean......") ;
for (int i=0; i<3; i++)
{
count[i]=Home.create(countVal) ;
countVal=count[i].Count();
System.out.println(countVal);
Thread.sleep(500) ;
} System.out.println("Calling count() on Bean......") ;
for (int i=0; i<3; i++)
{
countVal=count[i].Count();
System.out.println(countVal) ;
Thread.sleep(500) ;
} for(int i=0; i<3; i++)
{
count[i].remove() ;
} } catch(Exception e)
{
e.printStackTrace();
}
}
}
Count count[]=new Count[3];
int countVal=0;
System.out.println("Instantiating Bean......") ;
for (int i=0; i<3; i++)
{
try
{count[i]=client.create(countVal) ;
countVal=count[i].Count();
System.out.println(countVal);
Thread.sleep(500) ;
} catch(Exception e)
{
e.printStackTrace() ;
}
}
System.out.println("Calling count() on Bean......") ;
for (int i=0; i<3; i++)
{
try
{countVal=count[i].Count();
System.out.println(countVal) ;
Thread.sleep(500) ;
}
catch(Exception e)
{
e.printStackTrace() ;
}
} for(int i=0; i<3; i++)
{
try
{count[i].remove() ;
}
catch(Exception e)
{
e.printStackTrace() ;
}
}非常感谢 pengji(彭乃超) 和uu_snow(薇薇) !