Through the research of JXTA technology for nearly a month, I finally got a general understanding of the JAVA language implementation process of JXTA technology in P2P network, and specially sorted it out for everyone to exchange and discuss. In fact, I can also find some information about the concept and introduction of this technology. I won't introduce it here. I will directly describe the implementation process of a P2P network in JAVA language through code. The first step is that everyone needs to download JXTA class library, which can download the latest version on the Internet. Sorry, I can't upload JXTA version here because it exceeds the limit! I will upload another technical manual including CHM format on the forum. Step 2: Create a new JAVA project and add these JAR packages to the build path (here, use Eclipse to use other IDE or directly add these JAR packages to the classpath. Step 3: We will start coding and create a new class. Its main method and the code for calling the method are as follows.
public static void main(String[]args){? resto peer myapp = new resto peer(); //Instantiate a peer object here? myapp startJxta(); //Start the JXTA method here to join the group to get services, etc. System exit (); //function exits the system? }public void startJxta() {? Try {? Join the default peer group NetPeerGroup? netpg = new NetPeerGroupFactory()get interface(); ? } catch (peer group exception e) {//todo automatically generates catch block e print stack trace (); } ? }
It should be noted that the structure and implementation of JXTA class library have changed greatly due to the continuous update of JXTA technology. Adding the default peer group here is a new method in the version. The methods in JXTA technical manual are out of date. Step 4, we need to obtain the discovery service provided by the default peer group.
? discovery service disco = netpggetDiscoveryService();
At this time, we can use disco service to find the advertisement we need. Fifth, we need to find a peer group of our own. If we can't find it, we will create such a peer group. At this time, we just want to get the services we need, because the services provided by each peer group are not all the same. We usually organize peers with the same purpose through a peer group. The code for joining the peer-to-peer group is as follows (this example is described by an auction service case of a restaurant in JXTA Technical Manual).
//Join the auction group? private void joinRestoNet() {? int count =; //What is the maximum number of cycles to try to discover? System out println (trying to discover a peer group with the group name RestoNet); ? Get discovery service from NetPeerGroup? discovery service h disco = netpg getdiscovery service(); ? Enumerate ae = null// Record found advertisements? //Loop until a RestoNet peer group is found, or until the expected number of discovery attempts is reached? while(count & gt; ) {? Try {? //The announcement that the local cache of peers was searched for the first time to find a RestoNet peer group//The peer group with the Name attribute of RestoNet was found through the discovery service provided by Net peer group? Ae = hdiscoget local advertisements (Discovery Service group name Restonet); //If it is found that the RestoNet peer group announced that the method has finished exiting the loop? If ((ae! = null)& amp; & ampAE hasmorelements()){? Break; ? }//Send a remote discovery request if you can't find it locally? //If the parameters are successively and the peer ID to be searched is empty, the notification types not found based on this condition are peer and ADV? //The maximum number of announcements to be obtained when obtaining the name attribute value of the announcement attribute to be discovered. H disco get remote advertisements (null discovery service group name restanettnull); ? //The thread pauses to wait for the discovery request try {? Thread sleep (timeout); ? } catch (interrupt exception e) {? // TODO automatically generates a catch block? e printStackTrace(); ? }} catch (IOException e) {// TODO automatically generated catch block? e printStackTrace(); ? }? }? /* ? * The above is the process of finding the target group circularly. The following is the process of joining? * */? //Create a peer-to-peer multicast that references peer-to-peer multicast with Restonetadv = null; ? //Check whether we have found the RestoNet announcement. If we don't find it, it means that we may be the first peer to try to join the group//or other peer members who know that the RestoNet group is closed or unreachable//In this case, we must create the RestoNet peer group? if (ae == null ||! Ae hasMoreElements()) {// Create grouping without prompting? The system outputs println (the RestoNext peer group cannot be found; Create me); Try {? //Create a new peer group RestoNet all-around peer group? //Get the general peer group announcement through NetPeerGroup? moduleimpladveditionimpladdv = netpg getallpurposeigergroupimpladvertisement(); //Creating a new peer group through JXTA network peer group will publish its own peer group announcement. //The parameters in turn are peer group ID, announcement group name description, Restonet = net PG, new group (MK group ID () Implodv restonet Inc); //Get the peer-to-peer group advertisement restonedv = netpg getpeergroupadvertisement (); } catch (exception e) {//catch block e print stack trace () automatically generated by todo; }} else {? //Finding the RestoNet notification in the cache means that we can join this existing group? //Extract peer-to-peer advertising elements from the collection? restone adv =(PeerGroupAdvertisement)AE next element(); Try to {//join the peer group. JXTA will not publish a new group (Restonet = net pg) because the announcement has been published; System out println (find the RestoNet peer group and join the existing group); ? } catch (peer group exception e) {//todo automatically generates catch block e print stack trace (); ? }}? //Get the discovery service and pipeline service provided by RestoNet? disco = restoNet getDiscoveryService(); pipes = restoNet getPipeService(); System out println (RestateRestaurant _ (+brand+) online); Return; }
At this point, we have joined or created a RestoNet peer group, which is located in the NetPerrGroup group, so we can find this group of advertisements through the discovery service provided by Net peer group. If it can't be found, it may be related to the network environment, because the waiting time is too short or the number of cycles is too small, and the P2P network is unreliable, so this parameter is difficult to determine and needs to be set according to the actual situation. At this point, our machine is already in the P2P network. In fact, we were already in the P2P network when we got the default peer group instance, but now we have found or created the peer group we need through network discovery and joined it, and then we can provide and consume the services provided by the peer group or the services provided by the peers in the peer group. The above simple applications for building P2P networks and joining peer groups will be posted in the future, so please pay attention! All of the above are personal experiences and understandings. If there are any mistakes, please let me criticize and correct them. If you need to communicate, please add MSN source code to download. Recently, I published an article about JXTA technology to build a P2P network, which attracted the attention and support of my friends. Now, at the request of my friends, I upload some test source code for your reference and discussion. I hope you can give me more valuable advice! It should be noted that the two files in SRCRARR need to be placed in different working directories to run successfully, because JXTA will set a cache directory for each peer when it runs for the first time. If they are in the same workspace, the latter will cover the information of the former. Therefore, it is recommended to use different workspaces, preferably two computers to test. A configuration window will pop up when running for the first time. Just fill in the user name and password, and don't worry about other settings for the time being. There must be more than eight passwords, and the other three packages are JXTA class libraries. Please extract them and import them into the project.
src rar lib rar src lib rar lib rar lishixinzhi/Article/program/Java/Javascript/20 13 1 1/25439