EJB3已经出来了一段时间,这两天对它的功能进行了测试.发现与以前版本比较已经有了很大的改变..
测试环境:JBoss4.0.5.GA,Eclipse3.2.1.由于只是测试并没有与数据库进行连接..
1、定义需要在客户端进行调用的接口
package
com.service.ejb3;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* 提供给客户端的接口
* @author xzgf javac_oracle@163.com
* @create 2007-1-31
*
*/
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
interface
HelloWorld
...
{
public String SayHello(String name);
}
定义了一个SayHello的方法.参数为一个String
2、服务器端需要实现这个接口:
package
com.service.ejb3.impl;
import
java.util.Date;
import
javax.ejb.Remote;
import
javax.ejb.Stateless;
import
org.apache.commons.lang.time.StopWatch;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
import
com.service.ejb3.HelloWorld;
import
com.service.pub.DateUtil;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* 客户端接口的实现类...服务器端
* @author xzgf javac_oracle@163.com
* @create 2007-1-31
*
*/
@Stateless
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
@Remote (
...
{HelloWorld.class}
)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
HelloWorldBean
implements
HelloWorld
...
{
private static Log log = LogFactory.getLog(HelloWorldBean.class);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public String SayHello(String name) ...{
StopWatch sw = new StopWatch();
sw.start();
log.debug("Begin execute in service:" + name + " Time:" + DateUtil.CN_DATETIME.format(new Date()));
String str = name + ": Hello,World." ;
sw.stop();
str += DateUtil.CN_DATETIME.format(new Date());
log.debug("Over execute in service:" + str + " Time:" + DateUtil.CN_DATETIME.format(new Date()));
return str;
}
}
实现很简单,根据客户端传入的String,加上Hello,World和当前服务器时间后返回.
3、实现客户端调用.采用applet.优点:比较容易实现一些在Web下不好实现的界面功能.缺点:需要在客户端安装jre并有一定量的代码下载量.
package
com.client.ejb3;
import
java.awt.BorderLayout;
import
java.awt.Dimension;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
java.util.Properties;
import
javax.naming.InitialContext;
import
javax.swing.JApplet;
import
javax.swing.JButton;
import
javax.swing.JOptionPane;
import
com.service.ejb3.HelloWorld;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
/** */
/**
* 客户端
*
* @author xzgf javac_oracle@163.com
* @create 2007-1-31
*
*/
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
MyApplet
extends
JApplet
...
{
private HelloWorld hw = null;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private void initSelf() ...{
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
InitialContext ctx = null;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (props == null) ...{
props = System.getProperties();
}
ctx = new InitialContext(props);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (ctx == null) ...{
System.out.println("InitialContext is null");
}
hw = (HelloWorld) ctx.lookup("HelloWorldBean/remote");
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if (hw == null) ...{
System.out.println("HelloWorld is null.");
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (Exception e) ...{
e.printStackTrace();
}
JButton button = new JButton();
button.setText("Test Button");
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
button.addActionListener(new ActionListener() ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void actionPerformed(ActionEvent e) ...{
onClick();
}
});
getContentPane().setLayout(new BorderLayout());
getContentPane().add(button, BorderLayout.NORTH);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private void onClick() ...{
JOptionPane.showMessageDialog(this, hw.SayHello("xzgf"));
}
@Override
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public void init() ...{
// TODO Auto-generated method stub
this.setSize(new Dimension(800, 900));
super.init();
initSelf();
}
}
4、用JSP实现applet的加载:
"
com.client.ejb3.MyApplet
"
archive
=
"
client.jar
"
codebase
=
"
./applet
"
type
=
"
applet
"
>
5、部署时需要注意的是:由于在jsp中指定了codebase和archive,所以客户端会在此目录下的client.jar进行类查找.所以需要把JBoss中自带的jbossall-client.jar、jboss-aop-jdk50-client.jar、jboss-aspect-jdk50-client.jar、jboss-ejb3-client.jar解压后和applet主类即MyApplet一起打成一个client.jar.否则会报类找不到异常.下一步会解决这个问题...
6、部署后访问上面的jsp页面即可