Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29308428
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Java

2009-11-09 12:31:46

学习新东西学习新技术往架构方向发展。新技术也要结合新架构进来!

对于 Java RMI 而言,我们用接口(interface)定义行为,用类(class)定义实现。
对外部而言我们会提供一些接口说明文档。帮助用户理解我们的接口从而能够方便他们进行调用。

在 运行rmic之前一定要用javac编过,rmic是找编译好的类的,不是找源程序的。rmic的工作原理是,
首先反编译 ICustomerImpl.class,加入rmi架构需要的代码,然后把这个中间过程产生的Java文件编译成 ICustomerImpl_stub.class

以下学习笔记摘自官网:
RMI supports two classes that implement the same interface. The first class is the implementation of the behavior, and it runs on the server. The second class acts as a proxy for the remote service and it runs on the client.
(定义一个公共的接口单元然后分别在服务端与客户端作布置。服务端实现代码逻辑客户端相当于是做代理)

[RMI Interfaces]


接口即服务!!!

[RMI Architecture Layers]

RMI的架构图


The Transport Layer makes the connection between JVMs. All connections are stream-based network connections that use TCP/IP.

两台不同的主机之间的JVM之间的传输是建立在TCP/IP上面的。

[RMI Transport Requires a network connection]


How does a client find an RMI remote service?

A: Clients find remote services by using a naming or directory service. This may seem like circular logic. How can a client locate a service by using a service? In fact, that is exactly the case. A naming or directory service is run on a well-known host and port number.

即指定好服务器的域名与端口就可以正常作通讯处理了!

RMI can use many different directory services, including the Java Naming and Directory Interface (JNDI). RMI itself includes a simple service called the RMI Registry, rmiregistry. The RMI Registry runs on each machine that hosts remote service objects and accepts queries for services, by default on port 1099.

RMI可以使用JNDI的哦。也可以使用自身的注册服务机制的!

On the client side, the RMI Registry is accessed through the static class . It provides the method that a client uses to query a registry. The method lookup() accepts a URL that specifies the server host name and the name of the desired service. The method returns a remote reference to the service object. The URL takes the form:

 
rmi://
[:]

/
客户端会去寻找到这个服务出来!


学会如何去构建一个简单的RMI
A working RMI system is composed of several parts.
  • Interface definitions for the remote services 接口
  • Implementations of the remote services  实现
  • Stub and Skeleton files 代理文件
  • A server to host the remote services 远程服务
  • An RMI Naming service that allows clients to find the remote services
  • A class file provider (an HTTP or FTP server)
  • A client program that needs the remote services
编写的步骤:
1.编写接口即服务
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Calculator extends Remote {
    public long add(long a,long b) throws RemoteException;
    public long sub(long a,long b) throws RemoteException;
    public long mul(long a,long b) throws RemoteException;
    public long div(long a,long b) throws RemoteException;
}
2.编写实现代码
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class CalculatorImpl extends UnicastRemoteObject implements Calculator{
    public CalculatorImpl() throws RemoteException{
        super();
    }

    public long add(long a, long b) throws RemoteException {
        // TODO Auto-generated method stub
        return a+b;
    }

    public long div(long a, long b) throws RemoteException {
        // TODO Auto-generated method stub
        if (b==0) {
            return 0;
        }
        return a/b;
    }

    public long mul(long a, long b) throws RemoteException {
        // TODO Auto-generated method stub
        return a*b;
    }

    public long sub(long a, long b) throws RemoteException {
        // TODO Auto-generated method stub
        return a-b;
    }
   
}
生成程序的存根文件
       右键点击工程名称RMITest,RMI->Enable Stubs Generations
3.
可以生成这样的两个文件出来:Stud文件!

开始绑定服务。启动服务才能监听到端口!

import java.rmi.Naming;

public class CalculatorServer {
public CalculatorServer() {
try {
Calculator c = new CalculatorImpl();
Naming.bind("rmi://localhost:1025/CalculatorService", c);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new CalculatorServer();
}
}
因为我的电脑端口1099被占用就只能用其他的端口号了!

5、编写客户端代码
import cn.tianya.server.Calculator;


public class CalculatorClient {
public static void main(String[] args) {
try {
Calculator c = (Calculator)Naming.lookup("rmi://localhost:1025/CalculatorService");
System.out.println(c.sub(4, 6));
} catch (Exception e) {
e.printStackTrace();
}
}
}

6.

If all goes well, the registry will start running and you can switch to the next console.


In the second console start the server hosting the CalculatorService, and enter the following:

>java CalculatorServer

It will start, load the implementation into memory and wait for a client connection.

In the last console, start the client program.

>java CalculatorClient

If all goes well you will see the following output:


That's it; you have created a working RMI system. Even though you ran the three consoles on the same computer, RMI uses your network stack and TCP/IP to communicate between the three separate JVMs. This is a full-fledged RMI system.

JVM之间的通讯是通过走TCP/IP的!



When the property
java.rmi.server.useCodebaseOnly
is set to true, then the JVM will load
classes from either a location specified by
the CLASSPATH environment variable


or the URL specified in this property.



[RMI client communicating with server 
outside firewall]

Since almost all firewalls recognize the HTTP protocol,

[Communication with both RMI client and 
server behind firewalls]

可以好好看看主块的东西哦!


This causes the execution of the CGI script, java-rmi.cgi 走CGI了!






  
阅读(1066) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~