JINI学习笔记2-HelloWorld

来源:互联网 发布:choice数据接口 编辑:程序博客网 时间:2024/04/29 06:16

下载安装jini

配置环境变量

<>

<非正常操作顺序>

 

WinXP

开启服务

1 HTTP服务器

D:/jini2_1>java -jar lib/tools.jar -port 8080 -dir lib –verbose

 

2 RMI激活守护进程

D:/jini2_1>rmid -J-Dsun.rmi.activation.execPolicy=none

 

3 查找服务

D:/jini2_1>java -Djava.ext.dirs=lib -Djava.security.policy=configentry/all.policy -jar lib/start.jar configentry/start-reggie.config

 

4.编译

D:/jini2_1/helloworld/HelloWorld.java  [interface]

import java.rmi.Remote;

import java.rmi.RemoteException;

 

public interface HelloWorld extends Remote {

 

       public String getMessage() throws RemoteException;

      

}

 

 

D:/jini2_1/helloworld/HelloWorldClient.java [class]

import java.io.IOException;

import java.rmi.RMISecurityManager;

 

import com.sun.jini.config.ConfigUtil;

 

import net.jini.core.discovery.LookupLocator;

import net.jini.core.lookup.ServiceRegistrar;

import net.jini.core.lookup.ServiceTemplate;

 

 

public class HelloWorldClient {

       private ServiceRegistrar registrar;

       private ServiceTemplate template;

       private HelloWorld helloItem;

      

      

       public HelloWorldClient() throws IOException, ClassNotFoundException{

         

              if(System.getSecurityManager() == null){

                     System.setSecurityManager(new RMISecurityManager());

              }

             

              Class[] type = new Class[]{HelloWorld.class};

              template = new ServiceTemplate(null,type,null);

             

              LookupLocator locator = new LookupLocator("jini://" + ConfigUtil.getHostName());

              registrar = locator.getRegistrar();

              helloItem = (HelloWorld) registrar.lookup(template);

              System.out.println(helloItem.getMessage());

       }

      

       public static void main(String[] args) {

              try {

                   new HelloWorldClient();

              } catch (IOException e) {

                     e.printStackTrace();

              } catch (ClassNotFoundException e) {

                     e.printStackTrace();

              }

       }

 

}

 

 

D:/jini2_1/helloworld/HelloWorldProxy.java [class]

import java.io.Serializable;

import java.rmi.RemoteException;

 

import net.jini.core.lookup.ServiceID;

import net.jini.lookup.ServiceIDListener;

 

 

public class HelloWorldProxy implements Serializable, ServiceIDListener, HelloWorld {

      

       private static final long serialVersionUID = -4111105409575620426L;

 

       public HelloWorldProxy() throws RemoteException{

              super();

       }

 

       public String getMessage() throws RemoteException {

              return "Hello World!!!  JINI TEST SUCCESS";

       }

 

       public void serviceIDNotify(ServiceID serviceID) {

              System.out.println("ServiceID is  " + serviceID);

       }

 

}

 

D:/jini2_1/helloworld/HelloWorldService.java [class]

import java.io.IOException;

import java.rmi.RMISecurityManager;

 

import net.jini.discovery.LookupDiscovery;

import net.jini.discovery.LookupDiscoveryManager;

import net.jini.lookup.JoinManager;

 

 

public class HelloWorldService {

       public HelloWorldService() throws IOException{

             

              if(System.getSecurityManager() == null){

                     System.setSecurityManager(new RMISecurityManager());

              }

             

              HelloWorldProxy helloProxy = new HelloWorldProxy();

              LookupDiscoveryManager ldm = new LookupDiscoveryManager(LookupDiscovery.ALL_GROUPS,null,null);

              JoinManager jm = new JoinManager(helloProxy,null,helloProxy,ldm,null);

       }

      

       public static void main(String[] args) {

              try {

                     new HelloWorldService();

                    

                     Object obj = new Object();

                     synchronized (obj) {

                            obj.wait();

                     }

              } catch (IOException e) {

                     e.printStackTrace();

              } catch (InterruptedException e) {

                     e.printStackTrace();

              }

       }

 

}

 

编译:

D:/jini2_1/helloworld>javac -classpath ../lib/jini-core.jar;../lib/jini-ext.jar;../lib/sun-util.jar; *.java

 

5 开启一个自己的JINI-http服务<可选>

D:/jini2_1>java -jar lib/tools.jar -port 8089 -dir helloworld –verbose

 

6 开启HelloWorldService服务

D:/jini2_1>java -cp lib/jini-core.jar;lib/jini-ext.jar;lib/sun-util.jar;hellowold

-Djava.rmi.server.codebase=http://COMPUTERNAME:8089/

-Djava.security.policy=configentry/all.policy

HelloWorldService

 

显示结果:ServiceID is  357eb6c6-1620-41d0-9738-e82c225f0ab6

 

7 开启HelloWorldClient服务

D:/jini2_1>java -cp lib/jini-core.jar;lib/jini-ext.jar;lib/sun-util.jar;helloworld

-Djava.security.policy=configentry/all.policy

HelloWorldClient

 

显示结果: Hello World!!!  JINI TEST SUCCESS

原创粉丝点击