文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>EJB 3 学习例子 HelloWorld 小程序(使用JNDI)

EJB 3 学习例子 HelloWorld 小程序(使用JNDI)

时间:2010-07-18  来源:mianhuaman

用这么久的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

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载