JXTA的一個例子[加了注釋]
系統
1688 0
這是一個簡單的應用
JXTA
的客戶/服務器結構的
例子
,服務器端創建監聽線程,客戶端主動連接一次。所有代碼非常簡潔的說明了
JXTA
的C/S應用的設計框架,原
例子
是在
http://www.
jxta
.org/Tutorials.html
,我在本機和局域網內已調試通過,并加了注釋,有興趣的朋友看看吧。
需要引用的jar包有三個:
jxta
.jar,log4j.jar,bcprov-jdk14.jar
服務器端
客戶端
package
tutorial.socket;
import
java.io.
*
;
import
java.util.
*
;
import
net.jxta.discovery.
*
;
import
net.jxta.document.
*
;
import
net.jxta.endpoint.
*
;
import
net.jxta.exception.
*
;
import
net.jxta.peergroup.
*
;
import
net.jxta.pipe.
*
;
import
net.jxta.protocol.
*
;
public
class
SoloClient
...
{
static
PeerGroupnetPeerGroup
=
null
;
static
PeerGroupAdvertisementgroupAdvertisement
=
null
;
private
DiscoveryServicediscovery;
private
PipeServicepipes;
private
OutputPipemyPipe;
//
輸出管道,到服務的
private
Messagemsg;
public
SoloClient()
...
{
}
public
static
void
main(String[]args)
...
{
SoloClientmyClient
=
new
SoloClient();
System.out.println(
"
StartingClientpeer....
"
);
myClient.startJxta();
System.out.println(
"
GoodBye....
"
);
System.exit(
0
);
}
/***/
/**
*
*startJxta
*/
private
void
startJxta()
...
{
try
...
{
//
創建默認的JXTA組。
netPeerGroup
=
PeerGroupFactory.newNetPeerGroup();
}
catch
(PeerGroupExceptione)
...
{
System.out.println(
"
fatalerror:groupcreationfailure
"
);
e.printStackTrace();
System.exit(
1
);
}
//
獲取該組通告
groupAdvertisement
=
netPeerGroup.getPeerGroupAdvertisement();
//
獲取該組發現服務
System.out.println(
"
GettingDiscoveryService
"
);
discovery
=
netPeerGroup.getDiscoveryService();
//
獲取該組管道服務
System.out.println(
"
GettingPipeService
"
);
pipes
=
netPeerGroup.getPipeService();
startClient();
}
private
void
startClient()
...
{
System.out.println(
"
StarttheClient
"
);
//
找到服務
System.out
.println(
"
searchingfortheJXTA-SOLO-XDS-CNServiceadvertisement
"
);
Enumerationen
=
null
;
while
(
true
)
...
{
try
...
{
//
在本地的緩存中查找服務(JXTA-SOLO-XDS-CN)
en
=
discovery.getLocalAdvertisements(DiscoveryService.ADV,
"
Name
"
,
"
JXTASPEC:JXTA-SOLO-XDS-CN
"
);
//
找到就好
if
((en
!=
null
)
&&
en.hasMoreElements())
...
{
break
;
}
//
本地沒有,只好根據服務名稱遠程搜索
discovery.getRemoteAdvertisements(
null
,DiscoveryService.ADV,
"
Name
"
,
"
JXTASPEC:JXTA-SOLO-XDS-CN
"
,
1
,
null
);
//
發現過程是異步的,我們不知道需要多長時間
try
...
{
Thread.sleep(
2000
);
}
catch
(Exceptione)
...
{
}
}
catch
(IOExceptione)
...
{
//
啥也沒找到,繼續
}
//
顯示點吧
System.out.print(
"
.
"
);
}
System.out.println(
"
wefoundtheserviceadvertisement
"
);
//
找到以后,得到特殊服務的通告
ModuleSpecAdvertisementmdsadv
=
(ModuleSpecAdvertisement)en
.nextElement();
try
...
{
//
顯示一下通告而已
StructuredTextDocumentdoc
=
(StructuredTextDocument)mdsadv
.getDocument(MimeMediaType.TEXT_DEFAULTENCODING);
StringWriterout
=
new
StringWriter();
doc.sendToWriter(out);
System.out.println(out.toString());
out.close();
//
根據獲取的通告,獲取管道
PipeAdvertisementpipeadv
=
mdsadv.getPipeAdvertisement();
//
用上面的通道,通知服務創建輸出通道了,試著創建3次。
for
(
int
i
=
0
;i
<
3
;i
++
)
...
{
myPipe
=
pipes.createOutputPipe(pipeadv,
10000
);
}
//
要說的話
Stringdata
=
"
我靠,成功啦?。?!
"
;
//
組織成消息
msg
=
new
Message();
StringMessageElementsme
=
new
StringMessageElement(
"
DataTag
"
,
data,
null
);
msg.addMessageElement(
null
,sme);
//
通過管道發送吧
myPipe.send(msg);
System.out.println(
"
message"
"
+
data
+
"
"senttotheServer
"
);
}
catch
(Exceptionex)
...
{
ex.printStackTrace();
System.out.println(
"
Client:Errorsendingmessagetotheservice
"
);
}
}
}
==========================================
在服務器端的根目錄下還需要一個叫作pipeserver.adv的管道通告文件,這個文件的作用是使得每次服務啟動時創建的都是同一個管道服務。
<?xml version="1.0"?>
<!DOCTYPE
jxta
:PipeAdvertisement>
<
jxta
:PipeAdvertisement xmlns:
jxta
="http://
jxta
.org">
<Id>
urn:
jxta
:uuid-9CCCDF5AD8154D3D87A391210404E59BE4B888209A2241A4A162A10916074A9504
</Id>
<Type>JxtaUnicast</Type>
<Name>
JXTA
-SOLO-XDS-CN</Name>
</
jxta
:PipeAdvertisement>
第一次啟動服務器和客戶端時需要輸入節點名和密碼,隨便輸入就可以了。
JXTA的一個例子[加了注釋]
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
評論