EJB3已经出来了一段时间,这两天对它的功能进行了测试.发现与以前版本比较已经有了很大的改变..
测试环境:JBoss4.0.5.GA,Eclipse3.2.1.由于只是测试并没有与数据库进行连接..
1、定义需要在客户端进行调用的接口
java 代码
- package com.service.ejb3;
-
-
-
-
-
-
-
- public interface HelloWorld {
- public String SayHello(String name);
- }
定义了一个SayHello的方法.参数为一个String
2、服务器端需要实现这个接口:
java 代码
- 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;
-
-
-
-
-
-
-
-
- @Stateless
- @Remote ({HelloWorld.class})
- public class HelloWorldBean implements HelloWorld {
-
- private static Log log = LogFactory.getLog(HelloWorldBean.class);
-
- 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并有一定量的代码下载量.
java 代码
- 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;
-
-
-
-
-
-
-
-
- public class MyApplet extends JApplet {
-
- private HelloWorld hw = null;
-
- 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;
- try {
- if (props == null) {
- props = System.getProperties();
- }
- ctx = new InitialContext(props);
- if (ctx == null) {
- System.out.println("InitialContext is null");
- }
- hw = (HelloWorld) ctx.lookup("HelloWorldBean/remote");
- if (hw == null) {
- System.out.println("HelloWorld is null.");
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- JButton button = new JButton();
- button.setText("Test Button");
- button.addActionListener(new ActionListener() {
-
- public void actionPerformed(ActionEvent e) {
- onClick();
- }
-
- });
-
- getContentPane().setLayout(new BorderLayout());
- getContentPane().add(button, BorderLayout.NORTH);
- }
-
- private void onClick() {
- JOptionPane.showMessageDialog(this, hw.SayHello("xzgf"));
- }
-
- @Override
- public void init() {
-
- this.setSize(new Dimension(800, 900));
- super.init();
- initSelf();
- }
- }
4、用JSP实现applet的加载:
java 代码
- <jsp:plugin code="com.client.ejb3.MyApplet" archive="client.jar" codebase="./applet" type="applet">
- </jsp:plugin>
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页面即可