<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>开心e点 &#187; rmi</title>
	<atom:link href="http://www.ebnd.cn/tag/rmi/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ebnd.cn</link>
	<description>好好学习 天天向上</description>
	<lastBuildDate>Tue, 21 Jun 2011 15:00:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>在 Java 程序中释放 RMI 端口</title>
		<link>http://www.ebnd.cn/2009/09/14/free-rmi-port-in-java/</link>
		<comments>http://www.ebnd.cn/2009/09/14/free-rmi-port-in-java/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 23:55:28 +0000</pubDate>
		<dc:creator>dandan</dc:creator>
				<category><![CDATA[技术]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[rmi]]></category>

		<guid isPermaLink="false">http://www.ebnd.cn/?p=168</guid>
		<description><![CDATA[通过 LocateRegistry.createRegistry(port) 创建了一个 rmiregistry 监听 port 所在端口， 如果想要在程序中把这个端口关闭，也就是关闭 RMI registry 服务，可以参考以下的代码。 注：似乎在2003年的时候，这个方法还行不通，到了 JDK6 的版本才解决这个问题的。 参考 Sun bug 4457683， bug 4508962， 以及 这个 mail archive。 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 31 [...]]]></description>
			<content:encoded><![CDATA[<p>通过 <span style="color: #0000ff;">LocateRegistry.createRegistry(port) </span>创建了一个 rmiregistry 监听 port 所在端口，</p>
<p>如果想要在程序中把这个端口关闭，也就是关闭 RMI registry 服务，可以参考以下的代码。</p>
<p>注：似乎在2003年的时候，<a title="http://osdir.com/ml/java.sun.rmi/2003-11/msg00005.html" href="http://osdir.com/ml/java.sun.rmi/2003-11/msg00005.html" target="_blank">这个方法还行不通</a>，到了 JDK6 的版本才解决这个问题的。</p>
<p>参考 Sun  <a title="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4457683" href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4457683" target="_blank">bug 4457683</a>， <a title="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508962" href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508962" target="_blank">bug 4508962</a>， 以及 <a title="http://archives.java.sun.com/cgi-bin/wa?A2=ind0507&#038;L=rmi-users&#038;P=4036" href="http://archives.java.sun.com/cgi-bin/wa?A2=ind0507&#038;L=rmi-users&#038;P=4036" target="_blank">这个 mail archive</a>。</p>
<p><br class="spacer_" /></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
31
32
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.rmi.NoSuchObjectException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.rmi.Remote</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.rmi.RemoteException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.rmi.registry.LocateRegistry</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RMITest <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testRMIPortRelease<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Testing port release&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Acquiring port... &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">Remote</span> reg <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			reg <span style="color: #339933;">=</span> <span style="color: #003399;">LocateRegistry</span>.<span style="color: #006633;">createRegistry</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1102</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">RemoteException</span> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			ex.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;releasing port... &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			java.<span style="color: #006633;">rmi</span>.<span style="color: #006633;">server</span>.<span style="color: #003399;">UnicastRemoteObject</span>.<span style="color: #006633;">unexportObject</span><span style="color: #009900;">&#40;</span>reg, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NoSuchObjectException</span> ex1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			ex1.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		reg <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		RMITest s <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RMITest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		s.<span style="color: #006633;">testRMIPortRelease</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><br class="spacer_" /></p>
<h3  class="related_post_title">相关文章</h3><ul class="related_post"><li>三月 23, 2010 -- <a href="http://www.ebnd.cn/2010/03/23/breakpoint-nonstop-in-eclipse-under-jdk1-6-0_14/" title="Eclipse 在 jdk1.6.0_14 下加断点调试经常停不下来">Eclipse 在 jdk1.6.0_14 下加断点调试经常停不下来</a> (0)</li><li>三月 15, 2010 -- <a href="http://www.ebnd.cn/2010/03/15/eclipse-remote-debug-error/" title="Eclipse 远程调试 “Failed to connect to remote vm” error">Eclipse 远程调试 “Failed to connect to remote vm” error</a> (1)</li><li>十二月 3, 2009 -- <a href="http://www.ebnd.cn/2009/12/03/code-snippet-to-compare-two-versions/" title="代码：比较两个版本大小">代码：比较两个版本大小</a> (2)</li><li>九月 24, 2009 -- <a href="http://www.ebnd.cn/2009/09/24/no-unsigned-type-in-java/" title="Java 语言没有 unsigned 类型">Java 语言没有 unsigned 类型</a> (0)</li><li>九月 10, 2009 -- <a href="http://www.ebnd.cn/2009/09/10/java-prime-number-check/" title="[转] Java Prime number check">[转] Java Prime number check</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ebnd.cn/2009/09/14/free-rmi-port-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

