您的位置:首页 > 其它

EJB - 无状态SessionBean简单示例

2012-09-05 10:15 260 查看
示例环境: (理论上使用最新版会比较好, 但是因为某些原因,使用如下环境)

1. jdk1.5.0_14

2. Eclipse 3.7

3. jboss-4.2.2.GA

步骤和代码

1. 新建Project(EJB Project 和Java Project都可以) -- ejbTest

2. 导入jboss的lib 包。 -- 右键点击工程->属性-->Java Build Path--> Add External JARs, 把Jboss的client目录下的jar文件都包进去。

3. 新建远程接口 - HelloWorld.java

/**
* @Title:HelloWorld.java
* @package: service
* @Description:
* @author: oscar999
* @date: 2012-9-5
* @version V1.0
*/
package service;

import java.util.List;

import javax.ejb.Remote;

import entity.HelloWorlding;

@Remote
public interface HelloWorld {
public String hello(String message);

public List<HelloWorlding> getAllHelloWorlding();
}


注意: @Remote的注释一定要加上, 否则在后面的测试中会报出

javax.naming.NameNotFoundException: remote not bound 的错误

4. 编写传输实例类 --HelloWorlding.java

/**
* @Title:HelloWorlding.java
* @package: entity
* @Description:
* @author: oscar999
* @date: 2012-9-5
* @version V1.0
*/
package entity;

import java.io.Serializable;

public class HelloWorlding implements Serializable {

private int id;
private String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}


5. 编写 Session Bean --HelloWorldBean.java

/**
* @Title:HelloWorldBean.java
* @package: service
* @Description:
* @author: oscar999
* @date: 2012-9-5
* @version V1.0
*/
package service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import entity.HelloWorlding;

@Stateless
public class HelloWorldBean implements HelloWorld {

public String hello(String message) {
return "Hello World " + message;
}

public List<HelloWorlding> getAllHelloWorlding() {
List<HelloWorlding> helloworldings = new ArrayList<HelloWorlding>();
HelloWorlding helloworlding = new HelloWorlding();
helloworlding.setId(10);
helloworlding.setName("oscar999");
helloworldings.add(helloworlding);
helloworlding = new HelloWorlding();
helloworlding.setId(11);
helloworlding.setName("silver");
helloworldings.add(helloworlding);
return helloworldings;
}

}


6. 打包,部署

右键点击Project-->Export, 产生 ejbTest.jar 文件, 结构如下(如果创建的是EJB Project,会自动产生META-INF这个文件夹和里面的文件,)

entity

-- HelloWorlding.class

service

-- HelloWorld.class

-- HelloWorldBean.class

META-INF

-- NABUFEST.MF

把ejbTest.jar 直接复制到jboss的server\default\deploy 目录下

至此,服务端的工作就结束了, 接下来就可以在测试端测试了。

在原工程或是新建工程建立测试类(新建的话需要导入相应的lib包),

/**
* @Title:TestClient.java
* @package: client
* @Description:
* @author: oscar999
* @date: 2012-9-5
* @version V1.0
*/
package client;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

import service.HelloWorld;

public class TestClient {

public static void main(String[] args) {
try {
Properties prop = new Properties();
prop.setProperty(Context.PROVIDER_URL, "localhost:1099");
prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
InitialContext ctx = new InitialContext(prop);
HelloWorld helloworld = (HelloWorld) ctx
.lookup("HelloWorldBean/remote");
System.out.println(helloworld.hello("Oscar"));
System.out.println(helloworld.getAllHelloWorlding().get(0).getName());
} catch (Exception e) {
e.printStackTrace();
}
}

}


直接运行就能看到效果了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: