Create RMI Application
- Create Remote Interface
- Create Remote Object
- Implement the Remote Interface
- Built RMI Server
- Built RMI Client
Step I. Create Java Application
Create the Java application with name RMI.
Create Remote Interface
[sourcecode language="csharp"]
/*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;
}
[/sourcecode]
Step II. Create Remote Object
[sourcecode language="csharp"]
/*
* 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;
}
}
[/sourcecode]
Step III. Implement the Remote Interface
[sourcecode language="csharp"]
/*
* 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;
}
}
[/sourcecode]
Step IV. Built RMI Server
When click on Start server
[sourcecode language="csharp"]
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();
}
}
[/sourcecode]
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
[sourcecode language="csharp"]
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();
}
}
[/sourcecode]
No comments:
Post a Comment