EJB Dev2: Ant JNDI JMS

      說三個方面問題:Ant的使用(對Dev1的補充)寫一個例子:如何將一個項目打包發送到指定位置,JNDI和JMS是理解EJB非常重要的兩個方面。

本文所有例子是基於JBoss5.1,JBoss5.1安裝及Home變量的配置,參見我的上篇博客EJB Dev1。

1. Ant

 Ant的官方定義:"Ant is a Java library and command-line tool. Ant's mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications."

從定義可以看出build files是Ant工作的主要描述文件,targets,dependent,compile等描述Ant的主要工作過程,下面從一個例子說明Ant工作過程

Eclipse下建立工程com.home.ant,在src目錄下建立package com.home.ant,在com.home.ant下建立類ANTTest.java,在工程根目錄下建立文件build.xml,如下圖:

 編輯build.xml

(1)定義property,定義完後寫一個target測試如下所示:

<?xml version="1.0"?>
<project name="com.home.ant" default="test" basedir="..">

	<property environment="env" />
	<property name="app.dir" value="${basedir}\com.home.ant" />
	<property name="src.dir" value="${app.dir}\src" />
	<property name="jboss.home" value="${env.JBOSS_HOME}" />
	<property name="jboss.server.config" value="default" />
	<property name="build.dir" value="${app.dir}\build" />
	<property name="build.classes.dir" value="${build.dir}\classes" />

	<target name="test" >
		<echo>app.dir: ${app.dir}</echo>
		<echo>src.dir: ${src.dir}</echo>
		<echo>jboss.home: ${jboss.home}</echo>
		<echo>jboss.server.config: ${jboss.server.config}</echo>
		<echo>build.dir: ${build.dir}</echo>
		<echo>build.classes.dir: ${build.classes.dir}</echo>
	</target>

</project>

上述文件定義了六個變量,工程位置,工程src位置,JBossHome位置,JBoss Config 位置,build完結果存放位置等。 

完成後運行ANT,可以直接在Eclipse中運行,也可以在磁盤上進入相應項目運行運行。這裏我直接在Eclipse中運行:在過程根目錄下運行build.xml,如下圖:



 運行結果:

Buildfile: D:\workbench\com.home.ant\build.xml
test:
     [echo] app.dir: D:\workbench\com.home.ant
     [echo] src.dir: D:\workbench\com.home.ant\src
     [echo] jboss.home: C:\jboss-5.1.0.GA
     [echo] jboss.server.config: default
     [echo] build.dir: D:\workbench\com.home.ant\build
     [echo] build.classes.dir: D:\workbench\com.home.ant\build\classes
BUILD SUCCESSFUL
Total time: 381 milliseconds

 (2)將build過程中用到的jar添加到classpath

<path id="build.classpath">
			<fileset dir="${jboss.home}\client">
				<include name="*.jar" />			
			</fileset>
			<pathelement location="${build.classes.dir}" />
	</path>

 

如上JBoss的client面臨下包含大量jar文件

(3) 定義target prepare和clean,target prepare依賴於clean,如下

<target name="prepare" depends="clean">
			<mkdir dir="${build.dir}" />
			<mkdir dir="${build.classes.dir}" />
	</target>
	
	<target name="clean">
			<delete dir="${build.dir}" />
			<delete file="${jboss.home}\server\${jboss.server.config}\deploy\antTest.jar" />
	</target>

 

clean的目的刪除之前build的結果

(4)加入項目編譯的描述

<target name="compile" depends="prepare" >
		<javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on" deprecation="on" optimize="off" includes="com/**">
			<classpath refid="build.classpath" />
		</javac>
	</target>

 

編譯依賴一prepare,所以編譯之前先運行target prepare

(5)加將項目打包描述到build.xml

<target name="ejbjar" depends="compile" >
		<jar jarfile="${app.dir}\antTest.jar">
			<fileset dir="${build.classes.dir}">
				<include name="com/**/*.class" />
			</fileset>
		</jar>
	</target>

 

顯示打包的target依賴於編譯,打包完的包名是antTest.jar,存放於項目根目錄下

(6)將上述jar發佈到指定目錄的描述

<target name="deploy" depends="ejbjar">
		<copy file="${app.dir}\antTest.jar" todir="${jboss.home}\server\${jboss.server.config}\deploy" />
	</target>

 

發佈依賴於打包,實質是將上述生成的antTest.jar複製到C:\jboss-5.1.0.GA\server\default\deploy位置

修改build.xml的默認target爲deploy,則可以完成將項目編譯,打包,發佈到JBoss工作目錄的任務,完整的build.xml內容如下:

<?xml version="1.0"?>
<project name="com.home.ant" default="deploy" basedir="..">

	<property environment="env" />
	<property name="app.dir" value="${basedir}\com.home.ant" />
	<property name="src.dir" value="${app.dir}\src" />
	<property name="jboss.home" value="${env.JBOSS_HOME}" />
	<property name="jboss.server.config" value="default" />
	<property name="build.dir" value="${app.dir}\build" />
	<property name="build.classes.dir" value="${build.dir}\classes" />

	<path id="build.classpath">
			<fileset dir="${jboss.home}\client">
				<include name="*.jar" />			
			</fileset>
			<pathelement location="${build.classes.dir}" />
	</path>
	
	<target name="prepare" depends="clean">
			<mkdir dir="${build.dir}" />
			<mkdir dir="${build.classes.dir}" />
	</target>
	
	<target name="clean">
			<delete dir="${build.dir}" />
			<delete file="${jboss.home}\server\${jboss.server.config}\deploy\antTest.jar" />
	</target>
	
	<target name="compile" depends="prepare" >
			<javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on" deprecation="on" optimize="off" includes="com/**">
				<classpath refid="build.classpath" />
			</javac>
	</target>
	
	<target name="ejbjar" depends="compile" >
		<jar jarfile="${app.dir}\antTest.jar">
			<fileset dir="${build.classes.dir}">
				<include name="com/**/*.class" />
			</fileset>
		</jar>
	</target>
	
	<target name="deploy" depends="ejbjar">
		<copy file="${app.dir}\antTest.jar" todir="${jboss.home}\server\${jboss.server.config}\deploy" />
	</target>

</project>

 

同樣在Eclipse下運行build.xml,可以完成發佈任務,到JBoss工作目錄(C:\jboss-5.1.0.GA\server\default\deploy)下查看,antTest.jar存在於此目錄下,說明測試成功;

注意:

當然Ant不止是可以將項目打成jar包,也可以編譯打包web項目war或企業項目等

當然Ant不止可以編譯java文件,還可以編譯C,C++等;

2.JNDI

摘幾句官網上JNDI的定義

      “The Java Naming and Directory Interface (JNDI) is part of the Java platform, providing applications based on Java technology with a unified interface to multiple naming and directory services. ” 這句說明首先JNDI是一個Java標準,事實上JDK1.3版本中就已經公佈了這一標準;其次JNDI的功能是爲基於Java知識的應用程序訪問命名和目錄服務提供統一的接口;

      那麼什麼是命名和目錄服務?

      "A Naming Service provides a mechanism for giving names to objects so you can retrieve and use those objects without knowing the location of the object. Objects can be located on any machine accessible from your network, not necessarily the local workstation.” 這句話對命名服務給出了一個很好的定義,即命名服務是一種根據名字檢索出實體的機制,檢索出的實體可以在任何位置,只要你的機器可以訪問的到;

     "A Directory Service also associates names with objects but provides additional information by associating attributes with the objects." 即目錄服務是擴展了的命名服務,在這個檢索出的實體上增加了一些屬性;

      "Naming and directory services play a vital role in intranets and the Internet by providing network-wide sharing of a variety of information about users, machines, networks, services, and applications." 這句說明命名和目錄服務的重要性。

 從一個圖來理解JNDI



 結合上圖,推薦一句非常到位的JNDI的官方定義:

"JNDI is a Java API that defines an interface to Naming and Directory Services for Java programs. JNDI is just an API and not, in itself, a Naming and Directory Service. To use JNDI, an implementation of a Naming and Directory service must be available. JNDI provides a service-independent interface to the underlying Service Provider implementation."

理解這句話:

      JNDI僅僅是一個API。

      JNDI定義的接口供Java程序訪問命名和目錄服務。

      要使用JNDI訪問命名和目錄服務,命名和目錄服務必須要有一個實現。

      JNDI和底層的服務實現是接口獨立的。

上圖還可以看出,在結構上,JNDI由兩部分組成客戶API和服務提供商接口(Service Provider Interface SPI) ,應用程序通過客戶API訪問目錄服務,服務提供接口用於供廠商創建命名和目錄服務的JNDI實現。

常見命名服務:

 

名稱 簡介
LDAP Lightweight Directory Access Protocol s the approved standard for an Internet naming service
DNS

Domain Name System is the Internet naming service for identifying machines on a network

NDS Novell Directory Services from Novell provides information about network services, such as files and printers.
NIS Network Information Service from Sun Microsystems provides system-wide information about machines, files, users, printers, and networks
CORBA for distributed component programming
RMI for distributed Java programming

 

 

 

 

 

 

 

 

 

 

 

 

 

 

JNDI編程:JBoss綁定了一個JNDI的實現,這裏通過JBoss簡單分析JNDI編程;

      啓動JBoss,http://localhost:8080/jmx-console/,點擊service=JNDIView,可以查看JBossJNDI相關明細。

      下面演示向Jboss註冊一個對象,訪問這個對象,銷燬這個對象:

直接給出一段代碼:

//Part one
		Properties properties = new Properties();
		properties.setProperty(Context.INITIAL_CONTEXT_FACTORY , "org.jnp.interfaces.NamingContextFactory");
        properties.setProperty(Context.PROVIDER_URL, "jnp://localhost");
        Context ctx = new InitialContext(properties);
        
        //Part two
        String str = "Kylin Soong Test";
        ctx.rebind("JNDITESTStr", str);
        User obj = new User();
        obj.setId("100");
        obj.setName("Kobe bryant");
        ctx.rebind("JNDITESTSObj", obj);
        
        //Part three
        String lookUpStr = (String) ctx.lookup("JNDITESTStr");
        System.out.println(lookUpStr);
        User user = (User) ctx.lookup("JNDITESTSObj");
        System.out.println(user);

 這段代碼分三個部分:

Part One:JNDI初始化,初始化時需要引入JBoss client段jar包,這在Jboss 目錄下client文件夾中可以找到;

Part two : 綁定 (向JBoss註冊對象),註冊的對象必須實現java.io.Serializable接口,如PartTwo中的User:

public class User implements Serializable {

	private String id, name;

	public void setId(String id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String toString() {
		return "[" + id + ", " + name + "]";
	}
}

 註冊完畢可以在Jboss控制檯查看到剛註冊的信息:



 可以看到剛註冊的JNDITESTStr和JNDITESTObj在JNDI View的列表中;

Part Three:查詢,上段代碼運行結果:

Kylin Soong Test
[100, Kobe bryant]

 

當然也可以對註冊的對象進行銷燬:

ctx.unbind("JNDITESTSObj");
        ctx.unbind("JNDITESTStr");

 

3. JMS

      JMS是中間件技術的基礎,由於我現在公司是一家中間件公司,所以對JMS我很早就有所瞭解,這裏我不想多做解釋,等以後有時間,我在對JMS做一研究。

more detail:

http://kylinsoong.iteye.com/blog/848713

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章