一个BMP ejb,运行的client如下:cd classesjava -Djava.naming.factory.initial=weblogic.jndi.WLInitialContextFactory -Djava.naming.provider.url=t3://localhost:7001 examples.AccountClientcd ..AccountClient内容:package examples;import javax.ejb.*; import javax.naming.*; import java.rmi.*; import javax.rmi.*; import java.util.*;/** * Sample client code which manipulates a Bank Account Entity Bean. */ public class AccountClient { public static void main(String[] args) throws Exception { Account account = null; try { /* * Get a reference to the Account Home Object - the * factory for Account EJB Objects */ Context ctx = new InitialContext(System.getProperties());
Object obj = ctx.lookup("AccountHome"); AccountHome home = (AccountHome) javax.rmi.PortableRemoteObject.narrow(obj, AccountHome.class); System.err.println("Total of all accounts in bank initially = " + home.getTotalBankValue()); /* * Use the factory to create the Account EJB Object */ home.create("123-456-7890", "John Smith"); /* * Find an account */ Iterator i = home.findByOwnerName("John Smith").iterator(); if (i.hasNext()) { account = (Account) javax.rmi.PortableRemoteObject.narrow( i.next(), Account.class); } else { throw new Exception("Could not find account"); } /* * Call the balance() method, and print it */ System.out.println("Initial Balance = " + account.getBalance()); /* * Deposit $100 into the account */ account.deposit(100); /* * Retrieve the resulting balance. */ System.out.println("After depositing 100, account balance = " + account.getBalance()); System.out.println("Total of all accounts in bank now = " + home.getTotalBankValue()); /* * Retrieve the Primary Key from the EJB Object */ AccountPK pk = (AccountPK) account.getPrimaryKey(); /* * Release our old EJB Object reference. Now call * find() again, this time querying on Account ID * (i.e. the Primary Key). */ account = null; account = home.findByPrimaryKey(pk); /* * Print out current balance */ System.out.println("Found account with ID " + pk + ". Balance = " + account.getBalance()); /* * Try to withdraw $150 */ System.out.println("Now trying to withdraw $150, which is more than is currently available. This should generate an exception.."); account.withdraw(150); } catch (Exception e) { System.out.println("Caught exception!"); e.printStackTrace(); } finally { /* * Destroy the Entity permanently */ try { System.out.println("Destroying account.."); if (account != null) { account.remove(); } } catch (Exception e) { e.printStackTrace(); } } } }
<J2EETutorial>讲的很浅显,入门可以了.j2ee这个标准内容非常多啊.
j2sdk1.4+j2ee1.4+ant1.5开发个教学还差不多,做工程项目就郁闷了
ejb的服务器很多,所以配置方案也很多,楼上说的只是其中几种,另外还
jboss,orion等都不错。
至于jb,wsad是开发工具,可以是你快速开发,中间会生成一些代码。
2. File --> new --> enterprise --> EJB......
想了解更多, 可以在网上找
j2sdk1.4+j2ee1.4+ant1.5的组合不行,那用jdk1.4+weblogic这两个开发ejb可以吗?(jbuider我不大懂)
只用这两个吗?不要用别的吗?大家认为把rmi程序改为ejb可以吗?
有人说ejb是rmi的发展是吗?可以取代rmi吗?
打包和布署怎么办?
只用这两个吗?不要用别的吗?
你用这两个的话,部署ejb的时候,你会郁闷死的
用jb,点点鼠标,一切就都搞定了!! ------------------------------------------------------
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??
j2sdk1.4+j2ee1.4+ant1.5这是最直接的开发方式,可以用它来入门,jb是ide,开发起来比较快
package c15.rmi;
import java.rmi.*;
interface PerfectTimeI extends Remote
{
long getPerfectTime() throws RemoteException;
}2、服务代码PerfectTime.java
package c15.rmi;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.net.*;public class PerfectTime extends UnicastRemoteObject implements PerfectTimeI
{
public long getPerfectTime() throws RemoteException
{
return System.currentTimeMillis();
}
public PerfectTime() throws RemoteException
{
}
public static void main(String[] args) throws Exception
{
System.setSecurityManager( new RMISecurityManager());
PerfectTime pt=new PerfectTime();
Naming.bind("//localhost:2005/PerfectTime",pt);
System.out.println("Ready to do Time");
}
}3、客户代码DisplayPerfectTime.java
package c15.rmi;
import java.rmi.*;
import java.rmi.registry.*;public class DisplayPerfectTime
{
public static void main(String[] args) throws Exception
{
System.setSecurityManager( new RMISecurityManager());
PerfectTimeI t=(PerfectTimeI)Naming.lookup("//localhost:2005/PerfectTime");
for(int i=0;i<10;i++)
System.out.println("Perfect time= "+t.getPerfectTime());
}
}4、编译
F:\java\rmi>javac -d server PerfectTimeI.java PerfectTime.java
F:\java\rmi>javac -d client PerfectTimeI.java DisplayPerfectTime.java
生成目录及文件:
├─client
│ └─c15
│ └─rmi
│ DisplayPerfectTime.class
│ PerfectTimeI.class
└─server
└─c15
└─rmi
PerfectTime.class
PerfectTimeI.class5、生成rmi根
F:\java\rmi>cd server
F:\java\rmi\server>rmic c15.rmi.PerfectTime
又生成两个文件:
F:\JAVA\RMI\SERVER
└─c15
└─rmi
PerfectTime.class
PerfectTimeI.class
PerfectTime_Stub.class
PerfectTime_Skel.class
6、拷贝文件 PerfectTime_Stub.class、PerfectTime_Skel.class到client\c15\rmi目录
7、编辑策略文件policy:
grant {
// Allow everything for now
permission java.security.AllPermission;
};
保存在源文件所在目录中即F:\java\rmi\。
8、启动rmi注册服务器
F:\java\rmi>start rmiregistry 2005
9、启动服务类(为了验证rmi功能,最好屏蔽classpath环境变量,即set classpath=)
F:\java\rmi\server>java -Djava.rmi.server.codebase=file:///f:/java/rmi/server/
-Djava.security.policy=file:///f:/java/rmi/policy c15.rmi.PerfectTime
Ready to do Time说明:file:///f:/java/rmi/server/部分的server后面的/不能少
10、运行客户类(新开一个command窗口。为了验证rmi功能,最好屏蔽classpath环境变量,即set classpath=)
F:\java\rmi\client>java -Djava.security.policy=file:///f:/java/rmi/policy c15.rm
i.DisplayPerfectTime
Perfect time= 1049338846285
Perfect time= 1049338846295
Perfect time= 1049338846315
Perfect time= 1049338846325
Perfect time= 1049338846325
Perfect time= 1049338846335
Perfect time= 1049338846335
Perfect time= 1049338846335
Perfect time= 1049338846335
Perfect time= 1049338846345说明:不能少:-Djava.security.policy=file:///f:/java/rmi/policy,否则无法访问
这是我学rmi用的程序,我要做的就是用ejb来实现这个程序
我就是不知道怎么部署ejb ,
在一台机子提供服务,
别的机子怎么访问?????
rmi:PerfectTimeI t=(PerfectTimeI)Naming.lookup("//45.21.58.122:2005/PerfectTime");
这样来调用,ejb又是怎么实现的?
客户机怎么知道服务器提供这样服务,
远程调用怎么实现???
企业级开发就要用jdk1.4+weblogic等企业级工具了。
另外,我觉得《j2ee tutorial》不是很好。
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import java.util.*;/**
* Sample client code which manipulates a Bank Account Entity Bean.
*/
public class AccountClient { public static void main(String[] args) throws Exception { Account account = null; try {
/*
* Get a reference to the Account Home Object - the
* factory for Account EJB Objects
*/
Context ctx = new InitialContext(System.getProperties());
Object obj = ctx.lookup("AccountHome");
AccountHome home = (AccountHome) javax.rmi.PortableRemoteObject.narrow(obj, AccountHome.class); System.err.println("Total of all accounts in bank initially = " + home.getTotalBankValue()); /*
* Use the factory to create the Account EJB Object
*/
home.create("123-456-7890", "John Smith"); /*
* Find an account
*/
Iterator i = home.findByOwnerName("John Smith").iterator();
if (i.hasNext()) {
account = (Account)
javax.rmi.PortableRemoteObject.narrow(
i.next(), Account.class); }
else {
throw new Exception("Could not find account");
} /*
* Call the balance() method, and print it
*/
System.out.println("Initial Balance = " + account.getBalance()); /*
* Deposit $100 into the account
*/
account.deposit(100); /*
* Retrieve the resulting balance.
*/
System.out.println("After depositing 100, account balance = " + account.getBalance()); System.out.println("Total of all accounts in bank now = " + home.getTotalBankValue()); /*
* Retrieve the Primary Key from the EJB Object
*/
AccountPK pk = (AccountPK) account.getPrimaryKey(); /*
* Release our old EJB Object reference. Now call
* find() again, this time querying on Account ID
* (i.e. the Primary Key).
*/
account = null;
account = home.findByPrimaryKey(pk); /*
* Print out current balance
*/
System.out.println("Found account with ID " + pk + ". Balance = " + account.getBalance()); /*
* Try to withdraw $150
*/
System.out.println("Now trying to withdraw $150, which is more than is currently available. This should generate an exception..");
account.withdraw(150); }
catch (Exception e) {
System.out.println("Caught exception!");
e.printStackTrace();
}
finally {
/*
* Destroy the Entity permanently
*/
try {
System.out.println("Destroying account..");
if (account != null) {
account.remove();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
远程调用怎么实现???
rmi是利用PerfectTimeI t=(PerfectTimeI)Naming.lookup("//45.21.58.122:2005/PerfectTime");
这样来调用,ejb又是怎么实现的?
要有一个IP号和一端口号调用,
ejb是用什么jini,其它机子又怎么调用呢?>>>>??
Object obj = ctx.lookup("AccountHome");AccountHome home = (AccountHome) javax.rmi.PortableRemoteObject.narrow(obj, AccountHome.class);
Object obj = ctx.lookup("AccountHome");看这句根本没注明是哪个IP,哪台计算机,又怎么调用?
accountHome怎么知道是哪台计算机的?
context又不是一台计算机的唯一标志?