??xml version="1.0" encoding="utf-8" standalone="yes"?>
_NRMI——Java与EJB企业U应用开?/span>
Java™ RemoteMethodInvocation Specification
Java tutorial- RMI
2. 基础知识
2.1 |络通信协议
|络通信层是分布式计环境中使用客户/服务器结构的一个核心技术,而网l编E中大体有两U通信模式Q无q接和面向连接的协议?/span>
UDP
UDP (universal datagram protocol)是一U用于无q接通信的标准化协议Q其建立在IP协议之上Q而IP协议是internet使用的基本数据传输协议。UDP协议的主要作用是网l数据流量压~成数据报文的Ş式。一个典型的数据报文是一个二q制数据的传输单位。每一个数据报的前8个字节用来包含报头信息,剩余字节则用来包含具体的传输数据。UDP协议q不提供数据传送的保证机制。如果在从发送方到接收方的传递过E中出现数据报的丢失Q协议本wƈ不能做出M或提示。因此,通常Z把UDP协议UCؓ不可靠的传输协议。一般用来传输少量数据,它资源消耗小Q处理速度快?/span>
TCP
TCPQtransport control protocolQ传输控制协议,标准化的面向q接的通信协议。在使用TCP协议中发送者和接收者必d通信之前建立q接Q连接徏立之后被看成是一个数据流Q发送者将数据发到该数据流上,接收者从该数据流上读取数据。如果接收者也同时是发送者则q接是双向的。通信完成后Q何一斚w可以关闭q接Q之后的d操作都会p|。TCP协议提供了可靠的面向对象的数据流传输服务的规则和U定。简单的说在TCP模式中,Ҏ发一个数据包l你Q你要发一个确认数据包l对斏VJava提供的类库都TCP/IP协议的用借助套接字(SocketQ进行了抽象。套接字包含了徏立与q程L的连接,与主行通信以及关闭q接所需要的所有操作,而这些操作实际上q是由TCP/IP来执行的?/span>
~组
在应用复杂尤其是面向对象的时候,一斚w会遇到处理数据或者对象的l构的问题。另一斚wq会遇到面向的用来发送字节或字符块的|络q接问题。此旉要将对象转换成连接可以处理的格式Q从而自定义的对象可以通过q接道?/span>
~组QmarshallingQ是一个将负责对象转换成字节流的过E,然后在用反q程----反编l将字节{换成对象。Java中编l的实现方式是序列化QserializationQ?br />
代理
代理是一个实现给定接口的对象Q但是不直接执行一些代码计结果,而是代表其他一些对象执行实际计的对象?br />
代理可以代表其他的库或者某U类似的可以代替它执行网l通信的东ѝ这是RMI工作的原理,代理在RMI也即存根QStubQ?/span>
引用位于服务器中的对象的代理是如何构造的Q客L不能有一个真的Java引用Q因为Java引用只在对象位于同一个JVM时才能正常工作。构造的基本思想是ؓ服务器对象分配一个唯一的标识序P该序L代理保存Q此外还有对象所在的L名?/span>
客户如何h代理
在获取代理时我们需要一个间接层把所有细节抽象,通过使用名字去获得对应对象的思想是命名。命名的一个主要作用是通过使用对象的名字,化获得对象的d?/span>
对于RMI来说Q最通用的命名实现时RMI注册Q它hbind和lookup操作Q对于命名服务来_客户端其实存在一个它的代理?/span>
命名使用CZ?br />
一.String对象的比较,+操作和internҎ
q里从一个问题入手来看看?br />
package testPackage;
public class Test {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.print((hello == "Hello") + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((other.Other.hello == hello) + " ");
System.out.print((hello == ("Hel" + "lo")) + " ");
System.out.print((hello == ("Hel" + lo)) + " ");
System.out.println(hello == ("Hel" + lo).intern());
}
}
class Other {
static String hello = "Hello";
}
package other;
public class Other {
static String hello = "Hello";
}
正确{案Qtrue true true true false true
主要要点有:
1.所有内容相同的String指向同一个内存块。但String对象不能是通过new操作创徏出来。主要原因是JVM对String做了优化QString加蝲之后会持有一个常量池Q?br /> 只要在常量池中找到内容相同的String׃把其引用q回。而new操作是直接在内存中分配新I间?br />
2.Java中有两种l定Q静态和动态。如?操作的两Ҏ帔R表达式那么会在采用静态绑定,也就是说~译之后值已l定下来了。而如果有一Ҏ通过new操作创徏?br /> 来的那么会采用动态绑定,只有在运行的时候才知道其具体的倹{?br /> 3.String的internҎ会到帔R池里面找是否有相同内容的StringQ如果有则返回其引用。如果没有则把这个String对象d到常量池之中q放回其引用。额外说
下,intern在英文中有保留区的意思,q样好理解其作用。internҎq是native的?/pre>?String中的正则表达式?/pre>
String中有些方法是需要正则表辑ּ作ؓ参数的。这个时候就要主要不要传错参数。最典型的例子就是replaceAll(String regex, String replacement)。第一?br /> 参数是需要正则表辑ּ的,而第二参数是普通的字符丌Ӏ?/pre>String ss = "???";
ss = ss.replaceAll("?", "=");//q行到这里会抛出PatternSyntaxExceptionQ因?#8220;?”在正则表辑ּ里面是特D符P需要{义?/span>
ss = ss.replaceAll("[?]", "=");//正确Q我个h比较們于这U写法?/span>
ss = ss.replaceAll("\\?", "=");//正确Q对“?”做{义?/span>
因此在用splitQreplaceAllQreplaceFirst{方法时要特别注意是不是需要{?
]]>
2. ThreadLocalc,定义了一个变量的本地副本Q与原有变量隔离Q作用类?/span>static变量Q只是不׃n。可?/span>setd变量Q?/span>get去获取变量。变量类型不限制?/span>
3. Eclipse plug in开发中可以实现IRuntimeClasspathProvider接口?/span>可以提供?/span>launch configuration去获?/span>unresolved?/span>resolved classpath。开发h员可以在resolveClasspathҎ中加入自定义?/span>classpath。实现类需要注册在extension point中?/span>
/**
*Computesandreturnsanunresolvedclasspathforthegivenlaunchconfiguration.
*Variableandcontainerentriesarenotresolved.
*
*@paramconfigurationlaunchconfiguration
*@returnunresolvedpath
*@exceptionCoreExceptionifunabletocomputeapath
*/
public IRuntimeClasspathEntry[] computeUnresolvedClasspath(ILaunchConfiguration configuration) throws CoreException;
/**
*Returnstheresolvedpathcorrespondingtothegivenpath,inthecontextofthe
*givenlaunchconfiguration.Variableandcontainerentriesareresolved.Thereturned
*(resolved)pathneednothavethesamenumberofentriesasthegiven(unresolved)
*path.
*
*@paramentriesentriestoresolve
*@paramconfigurationlaunchconfigurationcontexttoresolvein
*@returnresolvedpath
*@exceptionCoreExceptionifunabletoresolveapath
*/
public IRuntimeClasspathEntry[] resolveClasspath(IRuntimeClasspathEntry[] entries, ILaunchConfiguration configuration) throws CoreException;
A provider extension is defined in plugin.xml
. Following is an example definition of a runtime classpath provider extension.
<extension point="org.eclipse.jdt.launching.classpathProviders">
<classpathProvider
id="com.example.ExampleClasspathProvider"
class="com.example.ExampleClasspathProviderImpl"
</classpathProvider>
</extension>
4. plug in 开发中可以?/span>JavaRuntime d到运行环境的信息?/span>
IRuntimeClasspathProvider provider = JavaRuntime.getClasspathProvider(configuration);
其中configuration?/span>ILaunchConfigurationcd的?/span>
4. ?/span>Eclipse plug in开发中获取文g。两U解军_法:1.?/span>plug in实例中读取文件的URLQ然后用FileLocator把这?/span>URL转化成文件\径;2.直接利用FileLocator?/span>findҎ?/span>
Ҏ1
//filepath 是需要定位的文g
String filepath = "/bin/resources/test.jar";
//instance 是当?/span>plug in的实?/span>
URL url = instance.getBundle().getEntry(filepath);
String path = null;
try {
path = FileLocator.resolve(url).getPath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returnnew Path(path);
}
Ҏ2
String filepath = "/bin/resources/test.jar";
URL url = FileLocator.find(instance.getBundle(),new Path(filepath),null);
try {
path = FileLocator.resolve(url).getPath();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
5. 可以利用JarOutputStream来写jar包。必MؓJarOutputStream 实例创徏臛_一?/span>EntryQ可以调?/span>putNextEntryҎ?/span>
Manifest mf = new Manifest();
JarOutputStream jar = new JarOutputStream(new FileOutputStream("MainTest.jar")Q?/span>mf);
Properties properties = new Properties();
jar.putNextEntry(new ZipEntry("MainTest.property"));
properties.store(jar, "this is a test");
jar.close();
6. 得到IJavaModle
IJavaModel model = JavaCore.create(ResourcesPlugin.getWorkspace()
.getRoot());
IJavaProject[] projects = model.getJavaProjects();
IPackageFragmentRoot[] roots = projects[i] .getPackageFragmentRoots();
然后可以依次得到对应elements
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO]
----------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO] task-segment: [archetype:create] (aggregator-style)
[INFO]
----------------------------------------------------------------------------
。。。。。。?br />
[INFO] Velocimacro : initialization complete.
[INFO] Velocity successfully started.
[INFO] [archetype:create]
[INFO] Defaulting package to group ID: com.mycompany.app
[INFO]
------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO]
------------------------------------------------------------------------
[INFO] Failed to resolve artifact.
GroupId: org.apache.maven.archetypes
ArtifactId: maven-archetype-quickstart
Version: RELEASE
Reason: Unable to determine the release version
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=org.apache.maven.archetypes
-DartifactId=maven-arch
etype-quickstart \
-Dversion=RELEASE -Dpackaging=jar -Dfile=/path/to/file
org.apache.maven.archetypes:maven-archetype-quickstart:jar:RELEASE
[INFO]
------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO]
------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Tue Apr 17 07:08:50 MDT 2007
[INFO] Final Memory: 4M/8M
[INFO]
------------------------------------------------------------------------