在jxta里面,所有的資源都是通過廣告來發布的,這里的服務業不例外,在這里的服務 發布里面有兩個很總要的概念,
• ModuleClassAdvertisement— defines the service class; its main purpose is to formally document the
existence of a module class. It is uniquely identified by a ModuleClassID.
• ModuleSpecAdvertisement — defines a service specification; uniquely identified by a ModuleSpecID.
Its main purpose is to provide references to the documentation needed in order to create conforming
implementations of that specification. A secondary use is to make running instances usable remotely,
by publishing any or all of the following:
• PipeAdvertisement
• ModuleSpecID of a proxy module
• ModuleSpecID of an authenticator module
• ModuleImplAdvertisement — defines an implementation of a given service specification.
這里的 ModuleClassAdvertisement 僅僅用來告知服務的存在,對等點若是需要訪問該服務的話,還需要發現與之關聯的 ModuleSpecAdvertisement 廣告信息。
而這里的 ModuleSpecAdvertisement 則包含了 對等點節點 要訪問該服務所需要的所有相關信息,比如:管道廣告信息,通過它才能夠連接上所需要的服務。
服務端的代碼示例大抵如下:
創建發布 ModuleClassAdvertisement :
ModuleClassAdvertisement mcadv = (ModuleClassAdvertisement)AdvertisementFactory.newAdvertisement(ModuleClassAdvertisement.getAdvertisementType());
mcadv.setName("JXTAMOD:JXTA-EX1");
mcadv.setDescription("Tutorial example to use JXTA module advertisement Framework");
ModuleClassID mcID = IDFactory.newModuleClassID();
mcadv.setModuleClassID(mcID);//通過mcID來建立ModuleClassAdvertisement 與ModuleSpecAdvertisement 的聯系
discovery.publish(mcadv);
discovery.remotePublish(mcadv);
創建發布 ModuleSpecAdvertisement :
ModuleSpecAdvertisement mdadv = (ModuleSpecAdvertisement)AdvertisementFactory.newAdvertisement(ModuleSpecAdvertisement.getAdvertisementType());
mdadv.setName("JXTASPEC:JXTA-EX1");
mdadv.setVersion("Version 1.0");
mdadv.setCreator("sun.com");
mdadv.setModuleSpecID(IDFactory.newModuleSpecID(mcID));
mdadv.setSpecURI("http://www.jxta.org/Ex1");
PipeAdvertisement pipeadv = null;
try {
FileInputStream is = new FileInputStream("pipeserver.adv");
pipeadv = (PipeAdvertisement)AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is);
is.close();
} catch (Exception e) {
System.out.println("failed to read/parse pipe advertisement");
}
mdadv.setPipeAdvertisement(pipeadv);
discovery.publish(mdadv);
discovery.remotePublish(mdadv);
myPipe = pipes.createInputPipe(pipeadv);
在客戶端,通過不斷的查找廣告(分本地查找和遠端查找)來 獲取 所需要服務的廣告信息,通過它就可以獲取 管道信息 來創建管道以達到通訊的目的。客戶端代碼示例大抵如下:
Enumeration en = null;
while (true) {
try {
/* let's look first in our local cache to see if we have it! We try to discover an adverisement which as the (Name, JXTA-EX1) tag value
en = discovery.getLocalAdvertisements(DiscoveryService.ADV,"Name","JXTASPEC:JXTA-EX1");
// Ok we got something in our local cache does not
// need to go further!
if ((en != null) && en.hasMoreElements()) {
break;
}
// nothing in the local cache?, let's remotely query
// for the service advertisement.
discovery.getRemoteAdvertisements(null,DiscoveryService.ADV,"Name","JXTASPEC:JXTA-EX1",1, null);
// The discovery is asynchronous as we do not know
// how long is going to take
try { // sleep as much as we want. Yes we
// should implement asynchronous listener pipe...
Thread.sleep(2000);
} catch (Exception e) {}
} catch (IOException e) {
// found nothing! move on
}
System.out.print(".");
}
System.out.println("we found the service advertisement");
// Ok get the service advertisement as a Spec Advertisement
ModuleSpecAdvertisement mdsadv = (ModuleSpecAdvertisement)en.nextElement();
try {
// let's print the advertisement as a plain text document
StructuredTextDocument doc = (StructuredTextDocument)mdsadv.getDocument(MimeMediaType.TEXT_DEFAULTENCODING);
StringWriter out = new StringWriter();
doc.sendToWriter(out);
System.out.println(out.toString());
out.close();
// we can find the pipe to connect to the service
// in the advertisement.
PipeAdvertisement pipeadv = mdsadv.getPipeAdvertisement();
// Ok we have our pipe advertiseemnt to talk to the service
// create the output pipe endpoint to connect to the server
myPipe = pipes.createOutputPipe(pipeadv, 10000);
}
// send the message to the service pipe
myPipe.send (msg);