您的位置:首页 > 其它

EJB分布式远程调用的小例子的简单实现。[附源码]

2012-01-10 23:38 573 查看
EJB分布式远程调用的小例子的简单实现[附源码,欢迎学习研究。]

说起远程分布式应用大家可能都会联想到现在的云计算。基于分布式的架构。

翻出以前的代码,贴在这里,和大家一起学习关于分布式的几点见解。

可惜EJB这种分布式用的不多,不是一些大的项目,大的企业几乎少有人会用到EJB。一般人用RMI或WebService就能完成的东东,没有企业会付出太高的代价。当然还有更简单的基于HTTPURLCOnnection实现的。总之不管哪种都是远程调用的一种实现,这里说下EJB分布式的远程调用。

EBJ是基于接口的编程,所有它一定会有相应的接口级实现类。调用者无须关心如何实现。只用相应接口接收即可。

首先,使用创建一个EBJ项目。[服务端]

定义一个远程接口。用注解@Remote标注。[@Local表示本地。]

package com.tudou.t1Test;

import java.util.List;

import javax.ejb.Remote;

@Remote
public interface StudentDao {
	public List<Student> queryStudents();

	public boolean addStu(Student student);
}


以及它的实现类[@stateless表示的是无状态会话bean,@statefull表示的是有状态会话bean,区别是前者比后者效率高,但是不能保留后者那种在会话中的数据]:

package com.tudou.t1Test;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class StudentDaoBean implements StudentDao {
	// 动态生成entityManager
	@PersistenceContext
	public EntityManager em;

	public boolean addStu(Student student) {
		try {
			em.persist(student);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	public List<Student> queryStudents() {
		Query query = em.createQuery("select s from Student as s");
		return query.getResultList();
	}

}


<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="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
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 	version="1.0">

	<persistence-unit name="StudentPu" transaction-type="JTA">
		<jta-data-source>java:/MySqlDS</jta-data-source>
		<properties>
			<property name="hibernate.show_sql" value="true" />
			<property name="hibernate.format_sql" value="true" />
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
	</persistence-unit>

</persistence>


以上的配置文件persistence.xml文件来自JBOSS目录jboss/docs/examples/jca下的mysql-ds.Xml中,JNDI必须保持一致。[注:tomcat不支持分布式,JBOSS和WEBLOGIC可以实现]

附:mysql-ds.xml

<datasources>

  <local-tx-datasource>

    <jndi-name>MySqlDS</jndi-name>

    <connection-url>jdbc:mysql://10.3.34.83:3306/test</connection-url>

    <driver-class>com.mysql.jdbc.Driver</driver-class>

    <user-name>root</user-name>

    <password>root</password>


最后将以上java文件打包成jar包。

编写测试类,进行测试。新建一个java项目[客户端]。导入刚才打包的jar。

package com.tudou.test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.TimeZone;

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

import org.junit.BeforeClass;
import org.junit.Test;

import com.tudou.t1Test.Student;
import com.tudou.t1Test.StudentDao;

public class TestStu {
	private static StudentDao studentDao;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		Properties p = new Properties();
		// weblogic值为weblogic.jndi.WLInitialContextFactory
		// JBOSS为org.jnp.interfaces.NamingContextFactory
		p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		// Weblogic为 t3://localhost:7001
		p.setProperty(Context.PROVIDER_URL, "localhost");
		InitialContext init = new InitialContext(p);
		// weblogic为LoginBean#后面是实现类的全路径,包括包名
		// 此时接口中必须配置为:@Stateless(mappedName="LoginBean")
		studentDao = (StudentDao) init.lookup("StudentDaoBean/remote");
	}

	@Test
	public void loginTest() {
		TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		sdf.setTimeZone(timeZone);
		String times = sdf.format(new Date());
		 Student s1 = new Student("scce005", "tudou", times);
		 Student s2 = new Student("scce002", "doudou", times);
		 Student s3 = new Student("scce003", "qq", times);
		 Student s4 = new Student("scce004", "yaerfeng", times);
		boolean flag1 = studentDao.addStu(s1);
		boolean flag2 = studentDao.addStu(s2);
		boolean flag3 = studentDao.addStu(s3);
		boolean flag4 = studentDao.addStu(s4);
		if (flag1 == true) {
			System.out.println("用户1添加成功!!!");
		} else {
			System.out.println("添加失败!!!");
		}
		if (flag2 == true) {
			System.out.println("用户2添加成功!!!");
		} else {
			System.out.println("添加失败!!!");
		}
		if (flag3 == true) {
			System.out.println("用户3添加成功!!!");
		} else {
			System.out.println("添加失败!!!");
		}
		if (flag4 == true) {
			System.out.println("用户4添加成功!!!");
		} else {
			System.out.println("添加失败!!!");
		}
		List<Student> lists = studentDao.queryStudents();
		if (lists != null) {
			System.out.println("学号\t\t姓名\t\t入学日期");
			for (Student s : lists) {
				System.out.println(s.getStuNo() + "\t" + s.getStuName() + "\t\t"
						+ s.getCreateDate());
			}
		} else {
			System.out.println("里面是空的");
		}
	}

}


在测试之下。服务端必须先启动,可以开多个客户端进行测试。注释中有写jboss和weblogic使用的不同。

源码地址:点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: