用这么久的EJB 3 .0 过去一直用EJB1.x 和 EJB 2.x 今天写了一个EJB 3 的小程序。在这里与大家分享一下
实现的功能就是在在内存中CRUD 操作
Step 1: Define a common interface(定义一个普通接口)
package com.use.test;
import java.util.Collection;
import com.use.entity.Student;
public interface IStudentOperation {
void addStudent(Student student);
void deleteStudent(int id);
void updateStudent(Student student);
Collection<Student> getAll();
}
setup 2: Definition of the remote interface and local interface (定义远程接口和本地接口)
package com.use.test;
import javax.ejb.Local;
//VM 表示不同虑拟机
//这个注解用于同一下VM
@Local
public interface IStudentOperationLocal extends IStudentOperation {
}
package com.use.test;
import javax.ejb.Remote;
//不同VM 通信时要用到RPC 方式来通信可在不同VM 中
@Remote
public interface IStudentOperationRemote extends IStudentOperation {
}
当然还要定义一个测试的对象
package com.use.entity;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Student implements Serializable {
private Integer id;
private String name;
private String sex;
private int age;
public Student(Integer id, String name, String sex, int age) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
public Student() {
super();
}
public Student(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString()+" Student: 姓名:"+this.name+" 年龄:"+ this.age+"性别:"+this.sex;
}
}
step 3 To achieve the corresponding interfaces (实现对应的接口)
package com.use.test.impl;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.Stateless;
import com.use.entity.Student;
import com.use.test.IStudentOperationLocal;
import com.use.test.IStudentOperationRemote;
@Stateless
public class StudentOperationImpl implements IStudentOperationLocal,
IStudentOperationRemote {
private Collection<Student> students = new ArrayList<Student>();
@Override
public void addStudent(Student student)
{
students.add(student);
}
@Override
public void deleteStudent(int id)
{
for (Student en:students)
{
if (en.getId().equals(id))
{
students.remove(en);
return;
}
}
}
@Override
public void updateStudent(Student student)
{
for (Student en:students)
{
if (en.getId().equals(student.getId()))
{
en.setAge(student.getAge());
en.setName(student.getName());
en.setSex(student.getSex());
}
}
}
@Override
public Collection<Student> getAll()
{
return this.students;
}
}
step 4 Preparation of test class 编写测试类
package com.use.test.client;
import java.util.Collection;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import com.use.entity.Student;
import com.use.test.IStudentOperationRemote;
public class Client {
public static void main(String args[]) throws Exception
{
Context ctx = null;
String url = "jnp://localhost:1099";
String purl = "org.jboss.naming:org.jnp.interfaces";
String fcy = "org.jnp.interfaces.NamingContextFactory";
Properties ps = new Properties();
ps.put(Context.INITIAL_CONTEXT_FACTORY, fcy);
ps.put(Context.PROVIDER_URL, url);
ps.put(Context.URL_PKG_PREFIXES, purl);
ctx = new InitialContext(ps);
IStudentOperationRemote studentOperationRemote = null;
Object h = ctx.lookup("StudentOperationImpl/remote");
studentOperationRemote = (IStudentOperationRemote)PortableRemoteObject.narrow(h, IStudentOperationRemote.class);
int count = 0;
for (int i = 0 ; i < 7 ; i++)
{
Student student = new Student(i,"Liuqing" + i,"Usb" + i,i + 20);
System.out.println("Count:" + count);
studentOperationRemote.addStudent(student);
count++;
}
Collection<Student> students = studentOperationRemote.getAll();
for (Student en:students)
{
System.out.println("Spring+Ejb :"+en.toString());
}
}
}
在运行就OK 注意思要先将对应的内容部署到JBOSS 服务器上才行
17:06:53,546 INFO [JBossASKernel] Created KernelDeployment for: UsbHelloWorld.jar
17:06:53,546 INFO [JBossASKernel] installing bean: jboss.j2ee:jar=UsbHelloWorld.jar,name=StudentOperationImpl,service=EJB3
17:06:53,546 INFO [JBossASKernel] with dependencies:
17:06:53,546 INFO [JBossASKernel] and demands:
17:06:53,546 INFO [JBossASKernel] jboss.ejb:service=EJBTimerService
17:06:53,546 INFO [JBossASKernel] and supplies:
17:06:53,546 INFO [JBossASKernel] jndi:StudentOperationImpl/local-com.use.test.IStudentOperationLocal
17:06:53,546 INFO [JBossASKernel] jndi:StudentOperationImpl/local
17:06:53,546 INFO [JBossASKernel] jndi:StudentOperationImpl/remote
17:06:53,546 INFO [JBossASKernel] jndi:StudentOperationImpl/remote-com.use.test.IStudentOperationRemote
17:06:53,546 INFO [JBossASKernel] Class:com.use.test.IStudentOperationLocal
17:06:53,546 INFO [JBossASKernel] Class:com.use.test.IStudentOperationRemote
17:06:53,546 INFO [JBossASKernel] Added bean(jboss.j2ee:jar=UsbHelloWorld.jar,name=StudentOperationImpl,service=EJB3) to KernelDeployment of: UsbHelloWorld.jar
17:06:53,609 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=UsbHelloWorld.jar,name=StudentOperationImpl,service=EJB3
17:06:53,609 INFO [EJBContainer] STARTED EJB: com.use.test.impl.StudentOperationImpl ejbName: StudentOperationImpl
17:06:53,656 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
StudentOperationImpl/remote - EJB3.x Default Remote Business Interface
StudentOperationImpl/remote-com.use.test.IStudentOperationRemote - EJB3.x Remote Business Interface
StudentOperationImpl/local - EJB3.x Default Local Business Interface
StudentOperationImpl/local-com.use.test.IStudentOperationLocal - EJB3.x Local Business Interface
step 5 运行结果 如果是这个结果就正常了
Count:0
Count:1
Count:2
Count:3
Count:4
Count:5
Count:6
Spring+Ejb :com.use.entity.Student@23e5d1 Student: 姓名:Liuqing0 年龄:20性别:Usb0
Spring+Ejb :com.use.entity.Student@c4fe76 Student: 姓名:Liuqing1 年龄:21性别:Usb1
Spring+Ejb :com.use.entity.Student@11e1e67 Student: 姓名:Liuqing2 年龄:22性别:Usb2
Spring+Ejb :com.use.entity.Student@5e176f Student: 姓名:Liuqing3 年龄:23性别:Usb3
Spring+Ejb :com.use.entity.Student@1549f94 Student: 姓名:Liuqing4 年龄:24性别:Usb4
Spring+Ejb :com.use.entity.Student@b8c8e6 Student: 姓名:Liuqing5 年龄:25性别:Usb5
Spring+Ejb :com.use.entity.Student@18d9850 Student: 姓名:Liuqing6 年龄:26性别:Usb6
分享到:
相关推荐
HelloWorld helloworld = (HelloWorld) ctx.lookup("HelloWorldBean/remote"); out.println(helloworld.SayHello("佛山人")); 5.用ant或eclipse,把客户端文件打成war包,发布到jboss上 6.输入...
Java EJB简单例子,这是HelloWorldBean的Home接口,它是EJB对象的生成库,无状态会话(将在下一个实例中具体讲解)Bean,这个接口是客户端与EJB对象相互作用的中间途径,通过Client触发调用Bean方法: try { //...
把文件用weblogic部署上去,然后反编译jar中的bind类 执行主方法weblogic就有打印出hello ejb的字样了,很好的一个ejb实例。
内容索引:Java源码,初学实例,EJB 一个简单的Java EJB例子代码,以经典的HelloWorld程序为例,介绍通过Client触发调用Bean方法、JNDI初始化取得系统属性、连接JNDI树的起始点、用EJB生成库Home生成EJB对象、调用EJB...
EJBTest.zip //03.EJB入门样例——HelloWorld EJBTestJava.zip //03.Java测试客户端 EJBTestJSP.zip //03.JSP测试客户端 SessionBeanTest.zip //04.会话Bean开发样例 MDBTest.zip //05.消息驱动Bean...
1个目标文件,JNDI的使用例子,有源代码,可以下载参考,JNDI的使用,初始化Context,它是连接JNDI树的起始点,查找你要的对象,打印找到的对象,关闭Context…… ftp文件传输 2个目标文件,FTP的目标是:(1)提高...
3.1 HelloWorld实例 3.2 程序代码 3.3 配置文件jboss-service.xml 3.4 将实例部署到JBOSS 3.5 MBean的效果 s四.EJB3.0使用说明 1. Enterprice JavaBeans(EJB)的概念 1.1 会话 Bean: 1.2 实体Bean: 1.3 消息驱动...
6.1 第一个JSP程序—HelloWorld! 6.2 注释的使用 6.3 脚本元素 6.3.1 声明 6.3.2 表达式 6.3.3 脚本代码 6.4 page指令 6.4.1 import 6.4.2 session 6.4.3 错误处理 6.5 包含其他文件 6.6 使用JavaBean ...
EJBTest.zip //03.EJB入门样例——HelloWorld EJBTestJava.zip //03.Java测试客户端 EJBTestJSP.zip //03.JSP测试客户端 SessionBeanTest.zip //04.会话Bean开发样例 MDBTest.zip //05.消息驱动Bean...
EJBTest.zip //03.EJB入门样例——HelloWorld EJBTestJava.zip //03.Java测试客户端 EJBTestJSP.zip //03.JSP测试客户端 SessionBeanTest.zip //04.会话Bean开发样例 MDBTest.zip //05.消息驱动Bean...
EJBTest.zip //03.EJB入门样例——HelloWorld EJBTestJava.zip //03.Java测试客户端 EJBTestJSP.zip //03.JSP测试客户端 SessionBeanTest.zip //04.会话Bean开发样例 MDBTest.zip //05.消息驱动Bean...
Jetty和Maven HelloWorld教程 Jetty(6)入门 (www.itjungle.com) Jetty Start.jar 配置Jetty 如何设置上下文(Context Path) 如何知道使用了那些jar包 如何配置SSL 如何使用非root用户监听80端口 如何配置连接器...
1个目标文件,JNDI的使用例子,有源代码,可以下载参考,JNDI的使用,初始化Context,它是连接JNDI树的起始点,查找你要的对象,打印找到的对象,关闭Context…… ftp文件传输 2个目标文件,FTP的目标是:(1)提高...
6.1 第一个JSP程序—HelloWorld! 6.2 注释的使用 6.3 脚本元素 6.3.1 声明 6.3.2 表达式 6.3.3 脚本代码 6.4 page指令 6.4.1 import 6.4.2 session 6.4.3 错误处理 6.5 包含其他文件 6.6 使用...
6.1 第一个JSP程序—HelloWorld! 6.2 注释的使用 6.3 脚本元素 6.3.1 声明 6.3.2 表达式 6.3.3 脚本代码 6.4 page指令 6.4.1 import 6.4.2 session 6.4.3 错误处理 6.5 包含其他文件 6.6 使用JavaBean ...
6.1 第一个JSP程序—HelloWorld! 6.2 注释的使用 6.3 脚本元素 6.3.1 声明 6.3.2 表达式 6.3.3 脚本代码 6.4 page指令 6.4.1 import 6.4.2 session 6.4.3 错误处理 6.5 包含其他文件 6.6 使用JavaBean ...
EJB中JNDI的使用源码例子 1个目标文件,JNDI的使用例子,有源代码,可以下载参考,JNDI的使用,初始化Context,它是连接JNDI树的起始点,查找你要的对象,打印找到的对象,关闭Context…… ftp文件传输 2个目标文件...
1个目标文件,JNDI的使用例子,有源代码,可以下载参考,JNDI的使用,初始化Context,它是连接JNDI树的起始点,查找你要的对象,打印找到的对象,关闭Context…… ftp文件传输 2个目标文件,FTP的目标是:(1)提高...
1个目标文件,JNDI的使用例子,有源代码,可以下载参考,JNDI的使用,初始化Context,它是连接JNDI树的起始点,查找你要的对象,打印找到的对象,关闭Context…… ftp文件传输 2个目标文件,FTP的目标是:(1)提高...
1个目标文件,JNDI的使用例子,有源代码,可以下载参考,JNDI的使用,初始化Context,它是连接JNDI树的起始点,查找你要的对象,打印找到的对象,关闭Context…… ftp文件传输 2个目标文件,FTP的目标是:(1)提高...