RMI – ví dụ tạo ứng dụng phân tán đơn giản


Create RMI Application

  1. Create Remote Interface
  2. Create Remote Object
  3. Implement the Remote Interface
  4. Built RMI Server
  5. Built RMI Client

Step I.               Create Java Application

Create the Java application with name RMI.

Create Remote Interface


/*Create Remote Interface
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;

/**
 *
 * @author Administrator
 */
public interface WeatherInterface extends Remote{
    public Weather getWeather() throws RemoteException;
}

Step II.            Create Remote Object

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rmi;

import java.io.Serializable;

/**
 *
 * @author Administrator
 */
public class Weather implements Serializable{

    private String name = "";
    private float temperature = 0.0F;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the degree
     */
    public float getTemperature() {
        return temperature;
    }

    /**
     * @param degree the degree to set
     */
    public void setTemperature(float temperature) {
        this.temperature = temperature;
    }

    public Weather() {
        name = "NoCity";
        temperature = 0.0F;
    }
}

Step III.         Implement the Remote Interface

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
 *
 * @author Administrator
 */
public class WeatherImpl extends UnicastRemoteObject implements WeatherInterface{

    Weather weather;

    public WeatherImpl(Weather weatherObj) throws RemoteException
    {
        super();
        this.weather = weatherObj;
    }

    public Weather getWeather() throws RemoteException {
        return weather;
    }

}

Step IV.          Built RMI Server

When click on Start server

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
         weather = new Weather();
         weather.setName("Can Tho");
         weather.setTemperature(50.5F);
        try {
            weatherImpl = new WeatherImpl(weather);
            LocateRegistry.createRegistry(5000);
            Naming.rebind("rmi://localhost:5000/WeatherServer",weatherImpl);
            JOptionPane.showMessageDialog(this, "Server started");
            jButton1.setEnabled(false);
            jButton2.setEnabled(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

When click on  Update

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        

        // TODO add your handling code here:

        weather.setName(jTextField1.getText());

        weather.setTemperature(Float.parseFloat(jTextField2.getText()));

    }

Step V.             Built RMI Client

When click on Get weather

Weather weather;
WeatherInterface weatherInter;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
weatherInter = (WeatherInterface)Naming.lookup("rmi://localhost:5000/WeatherServer");
weather = weatherInter.getWeather();
JOptionPane.showMessageDialog(this, weather.getName() + " is " + weather.getTemperature());
} catch (RemoteException ex) {
ex.printStackTrace();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (NotBoundException ex) {
ex.printStackTrace();
}
}


Advertisements

Phản hồi

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Đăng xuất / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất / Thay đổi )

Connecting to %s