`
kylinsoong
  • 浏览: 235904 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

  • 大小: 5.3 KB
  • 大小: 5.8 KB
  • 大小: 57.8 KB
  • 大小: 28.7 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics