使用EJB3 jdk必需在1.5及以上
首先写一个EJB3.0的项目肯定有2个部分组成(下面是用eclipse+jboss-4.2.3.GA说明):
1. EJB的组件(封装了业务逻辑的组件)
2. 客户端(注意:客户端可以说Java程序,也可以说Web程序,或是WebService)
第一步在jboss服务器上配置数据源 我采用的oracle11g数据库 我采用是默认服务器所以是 \default\deploy目录 名字命名 name-ds.xml 后缀必需是 -ds.xml 复制到 jboss安装目录 D:\setup\jboss-4.2.3.GA\server\default\deploy 然后复制jdbc驱动 在相应的服务器lib目录 D:\setup\jboss-4.2.3.GA\server\default\lib 这样数据源就建好了
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>leamDS</jndi-name>
<connection-url>jdbc:oracle:thin:@localhost:1521:orcl</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>userName</user-name>
<password>password</password>
<min-pool-size>5</min-pool-size>
<max-pool-size>100</max-pool-size>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>Oracle11g</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
EJB3实体bean采用JPA技术 使用注解也大大简化了程序 使ejb更简单
新建项目 添加jboss D:\setup\jboss-4.2.3.GA\client 目录下所有包添加到工程中 接下来编写代码 创建实体对象
@Entity
@Table(name = "TB_JOBTYPE")
public class Jobtype implements java.io.Serializable {
private static final long serialVersionUID = 1L;
@Id @Column(name="jobtypeid") @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="JOBTYPESEQ",allocationSize=1)
private Integer jobtypeid;
@Column(name="name",length=30,nullable=false)
private String name;
public Jobtype(){}
/**
* @return the jobtypeid
*/
public Integer getJobtypeid() {
return jobtypeid;
}
/**
* @param jobtypeid the jobtypeid to set
*/
public void setJobtypeid(Integer jobtypeid) {
this.jobtypeid = jobtypeid;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
实体对象服务接口
public interface JobtypeService {
public void save(Jobtype job);
public void delete(Integer jobtypeid);
public void update(Jobtype record);
public List<Jobtype> getJobtypes();
}
实现类 远程bean组件
@Stateless
@Remote(JobtypeService.class)
public class JobtypeServiceBean implements JobtypeService {
//创建实体管理器 unitName="leam" leam是persistence.xml文件配置的名称
@PersistenceContext(unitName="leam") EntityManager em;
public void save(Jobtype job) {
em.persist(job);
}
public void delete(Integer jobtypeid) {
em.remove(em.getReference(Jobtype.class, jobtypeid));
}
public void update(Jobtype job) {
em.merge(job);
}
@SuppressWarnings("unchecked")
public List<Jobtype> getJobtypes() {
return em.createQuery("select j from Jobtype j").getResultList();
}
}
获取jboss配置好的数据源 必需在src 新建META-INF目录 persistence.xml文件
<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" xmlns:persistence="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd "> <!-- 获取jboss数据源 使用jpa事务类型 --> <persistence-unit name="leam" transaction-type="JTA"> <!-- jboss数据源名称 leamDS --> <jta-data-source>java:leamDS</jta-data-source> <properties> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="false"/> <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/> </properties> </persistence-unit> </persistence>
这样ejb实体组件就写好了 我们只需要导出成jar包 部署到jboss服务器上 我使用jboss默认的设计服务器 D:\setup\jboss-4.2.3.GA\server\default\deploy 我导出的名字 EntityBean.jar 发布之后在
http://localhost:8080/jmx-console/
- service=JNDIView 点击
-
java.lang.String list() 点击invoke按钮转到
http://localhost:8080/jmx-console/HtmlAdaptor 的底部
Global JNDI Namespace
JobtypeServiceBean + -remote
能查到你发布好的名称 说明发布成功。
编写客户端测试程序 测试保存 删除 查询 src下面新建文件jndi.properties
调用ejb要使用jndi技术查找发布的ejb组件名称 这里配置 jndi 初始化接口 默认端口 本机地址
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=localhost:1099
测试类
public class JobtypeServiceTest {
private static JobtypeService jobtypeService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
InitialContext ctx=new InitialContext();
//发布的ejb名称
jobtypeService=(JobtypeService)ctx.lookup("JobtypeServiceBean/remote");
}
@Test
public void testSave(){
try{
Jobtype job=new Jobtype();
job.setAddTime(new Date());
job.setFlag(1);
job.setName("ejb测试");
job.setOpcode("admin");
jobtypeService.save(job);
}catch(Exception e){e.printStackTrace();}
}
@Test
public void testDelete() {
jobtypeService.delete(13701);
}
@Test
public void testGetJobtypes() {
System.out.print(jobtypeService.getJobtypes().size());
}
}
所有工作完成 一个简单的ejb3例子 附件中源代码