什么是EJB?
EJB 是 Java 企業(yè)Bean, 是JavaEE服務(wù)端 企業(yè)組件模型,它的設(shè)計(jì)目標(biāo)與核心應(yīng)用是部署分布式應(yīng)用程序。話不多說(shuō),直接看如何在本機(jī)部署EJB3。
部署環(huán)境:
操作系統(tǒng):Windows 8.1
EJB容器:Jboss 7.1
DB: MySQL 5.6.10
IDE: MyEclipse 10
JDK: 1.6
1、創(chuàng)建數(shù)據(jù)庫(kù)、表
由于在此過(guò)程中,需要和數(shù)據(jù)庫(kù)通信,需要首先創(chuàng)建數(shù)據(jù)庫(kù)表。
創(chuàng)建數(shù)據(jù)庫(kù):
1
|
create database student; //創(chuàng)建數(shù)據(jù)庫(kù) student |
創(chuàng)建表:
1
2
3
4
5
|
create table student( //創(chuàng)建表student 和 數(shù)據(jù)庫(kù)同名 `id` integer (11) not null , ` name ` varchar2(20) default null , primary key (`id`) )ENGINE=InnoDB DEFAULT CHARSET=latin1 |
1
2
|
insert into student values (1, 'easynoder' ); commit ; |
1
|
grant all privileges on *.* to root@localhost indentified by "1234" |
通過(guò)以上步驟,需要的數(shù)據(jù)庫(kù)表已建立好。可通過(guò)root用戶訪問(wèn)所有數(shù)據(jù)。
2、編寫(xiě)實(shí)體Bean、用戶操作接口和會(huì)話Bean
建立EJB工程,名為MyEJBProject。該工程META-INFO目錄下包含一個(gè)文件persistence.xml文件。該文件用來(lái)配置數(shù)據(jù)源,稍后進(jìn)行配置。
接著建立實(shí)體Bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
@Entity //表明這是一個(gè)實(shí)體Bean @Table (name = "student" ) //和數(shù)據(jù)庫(kù)表student 建立映射 public class StudentEntity implements Serializable { private static final long serialVersionUID = 4002145187978562529L; @Id // 表明是該實(shí)體的id @GeneratedValue (strategy = GenerationType. AUTO ) //id生成策略 @Column (name = "id" ) //對(duì)應(yīng)student表id字段 private int id ; @Column (name = "name" ) // 對(duì)應(yīng)student表name字段 private String name; public int getId() { return id ; } public String getName() { return name ; } public void setId( int id) { this .id = id; } public void setName(String name) { this .name = name; } } |
建立操作接口:
1
2
3
4
|
public interface BaseOperation { public List<?> findAll(); } |
該接口只有一個(gè)方法,獲取所有的學(xué)生
建立會(huì)話Bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
@Stateless //這是一個(gè)無(wú)狀態(tài)Bean @Remote (BaseOperation. class ) //指明Bean的remote接口 public class StudentDaoBean implements BaseOperation { // EntityManager是由EJB容器自動(dòng)配置和管理的,unitName屬性的值對(duì)應(yīng) persistence.xml 中< persistence-unit name = "MyEJBProject" transaction-type = "JTA" ></ persistence-unit > name的配置 @PersistenceContext (unitName = "MyEJBProject" ) private EntityManager em; @SuppressWarnings ( "unchecked" ) public List<?> findAll() { System. out .println( "查詢開(kāi)始..." ); List<StudentEntity> list = em.createQuery( " from StudentEntity " ).getResultList(); if (list != null ) { Iterator<StudentEntity> it = list.iterator(); while (it.hasNext()) { StudentEntity student = it.next(); System. out .println( "學(xué)生id:" + student.getId()); System. out .println( "學(xué)生名稱:" + student.getName()); } } System. out .println( "查詢完畢...." ); return list; } } |
3、數(shù)據(jù)源配置
這里需要注意下,在jboss6 和jboss7的配置是不同的。
jboss6和以前版本都是在deploy目錄下 添加**-ds.xml,這里**表示任意一種數(shù)據(jù)庫(kù)名稱,如果是mysql,則是mysql-ds.xml文件。而在jboss7中,是不一樣的。
將目錄切換至Jboss 的安裝目錄,即%JBOSS_HOME%下,進(jìn)入modules/com目錄,建立文件夾mysqldatabase,進(jìn)入,建立mysql文件夾,進(jìn)入,建立main文件夾。
在main目錄下,建立module.xml文件,該配置文件內(nèi)容為:
1
2
3
4
5
6
7
8
9
10
11
|
<? xml version = "1.0" encoding = "UTF-8" ?> < module xmlns = "urn:jboss:module:1.1" name = "com.mysqldatabase.mysql" > < resources > < resource-root path = "mysql-connector-java-5.**-bin.jar" /> </ resources > < dependencies > < module name = "javax.api" /> < module name = "javax.transaction.api" /> < module name = "javax.servlet.api" optional = "true" /> </ dependencies > </ module > |
尤其這里需要注意的是,module 節(jié)點(diǎn)屬性name的值,就是剛才咱們建立的文件夾的路徑。resources表示mysql驅(qū)動(dòng)的路徑。意味著,需要將mysql的驅(qū)動(dòng)放在main目錄下。即main目錄下包含兩個(gè)文件,module.xml和數(shù)據(jù)庫(kù)驅(qū)動(dòng)文件。
在做完上一步后,切換到%JBOSS_HOME%\standalone\configuration目錄下,
打開(kāi)standalone.xml,搜索datasources,進(jìn)行如下配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< datasources > < datasource jndi-name = "java:jboss/KouMySQLDS" pool-name = "MySQLDS" enabled = "true" use-java-context = "true" > < connection-url >jdbc:mysql://localhost:3306/student</ connection-url > < driver >mysql</ driver > < security > < user-name >root</ user-name > < password >1234</ password > </ security > </ datasource > < drivers > < driver name = "mysql" module = "com.mysqldatabase.mysql" > < driver-class >com.mysql.jdbc.Driver</ driver-class > < xa-datasource-class >com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</ xa-datasource-class > </ driver > </ drivers > </ datasources > |
jndi-name表示數(shù)據(jù)源jndi名稱,connection-url表示連接的url字符串;這里默認(rèn)使用3306端口,使用student庫(kù),用戶名和密碼即第一步配置的。module配置的即剛剛配置的module的路徑。
jboss的相關(guān)配置已經(jīng)完成,接著切換到剛新建的工程,其中有一個(gè)persistence.xml配置文件,該文件做如下配置,其中jta-data-source 就是上面配置的jndi-name.
1
2
3
4
5
6
7
8
|
< jta-data-source > java:jboss/KouMySQLDS </ jta-data-source > < properties > < property name = "hibernate.hbm2ddl.auto" value = "validate" /> < property name = "hibernate.jdbc.fetch_size" value = "15" /> < property name = "hibernate.jdbc.batch_size" value = "10" /> < property name = "hibernate.show_sql" value = "true" /> < property name = "hibernate.format_sql" value = "true" ></ property > </ properties > |
到此為止,服務(wù)端代碼和數(shù)據(jù)源配置已經(jīng)完成。接下來(lái)需要做的就是如何部署代碼以及如何在客戶端調(diào)用該EJB服務(wù)。
4、部署EJB服務(wù)。
將之前在工程中寫(xiě)的所有代碼打成jar包,命名為ejbservice.jar。同時(shí),只將實(shí)體Bean和接口打包成jar包,命名為ebjinterface.jar,這個(gè)jar將來(lái)用于客戶端調(diào)用使用。
將ejbservice.jar放入%JBOSS_HOME%\standalone\deployments目錄下。在jboss啟動(dòng)時(shí),會(huì)自動(dòng)掃描該目錄。然后部署該jar。
ok,我們將jboss配置到MyEclipse下,在MyEclipse中啟動(dòng)Jboss,觀察控制臺(tái)的輸出。
如果出現(xiàn)了 Deployed "ejbservice.jar" 這個(gè)日志,說(shuō)明ejb就部署成功了。
5、客戶端如何調(diào)用呢?
客戶端調(diào)用需要兩個(gè)必備條件:
引入jboss-ejb-client.properties配置、 jboss-client.jar和ejbinterface.jar。其中jboss-client.jar 位于jboss bin/client目錄下。ejbinterface.jar是我們剛剛創(chuàng)建的客戶端需要使用的接口jar包。
jboss-ejb-client.properties配置如下:
1
2
3
4
5
6
7
8
|
endpoint.name= client-endpoint remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED= false remote.connections= default remote.connection. default .host= localhost remote.connection. default .port= 4447 remote.connection. default .connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS= false remote.connection. default .username= yourUsername remote.connection. default .password= yourPassword |
有了這兩個(gè)條件,就可以安心的建立個(gè)測(cè)試類EJBTest.java,編寫(xiě)客戶端方法了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public static void main(String[] args) { Properties props = new Properties(); props.setProperty(Context. URL_PKG_PREFIXES, "org.jboss.ejb.client.naming" ); try { Context context = new InitialContext(props); // 這里需要注意字符串的寫(xiě)法:ejbservice 表示ejb的包名,StudentDaoBean表示咱們實(shí)際調(diào)用的會(huì)話Bean,org.easynoder.ejb2.dao.BaseOperation表示 對(duì)應(yīng)的接口 BaseOperation op = (BaseOperation) context .lookup( "ejb:/ejbservice//StudentDaoBean!org.easynoder.ejb2.dao.BaseOperation" ); op.findAll(); } catch (NamingException e) { e.printStackTrace(); } } |
運(yùn)行這段代碼,可以成功的查詢到數(shù)據(jù)庫(kù)的數(shù)據(jù)啦。
至此,EJB就部署成功啦。