Exemplo de Remote Method Invocation
Autor: Danilo Cunha | Publicado: 2010-11-26T22:41:00-08:00

Exemplo simples de usar RMI em Java para calcular imposto:

Calculadora.java

public interface Calculadora extends java.rmi.Remote {&nbsp;
&nbsp;&nbsp; &nbsp;public String imposto(String a, long b)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;throws java.rmi.RemoteException;&nbsp;
&nbsp;&nbsp;
}


CalculadoraCliente.java

import java.rmi.RemoteException;&nbsp;
import java.net.MalformedURLException;&nbsp;
import java.rmi.NotBoundException;&nbsp;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import javax.swing.JFrame;

public class CalculadoraCliente{&nbsp;


&nbsp;&nbsp; &nbsp;public static void main(String[] args) {&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;try {&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Registry registry =LocateRegistry.getRegistry("localhost", 1500);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Calculadora c = (Calculadora) registry.lookup("calculando");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println( c.imposto("Ana", 1000) );
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println( c.imposto("Maria", 1500) );
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println( c.imposto("Braga", 1800) );
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;catch (RemoteException re) {&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println();&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("RemoteException");&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(re);&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;catch (NotBoundException nbe) {&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println();&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("NotBoundException");&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(nbe);&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;catch (
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;java.lang.ArithmeticException ae) {&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println();&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("java.lang.ArithmeticException");&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(ae);&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;
&nbsp;&nbsp; &nbsp;}&nbsp;
}&nbsp;


CalculadoraImpl.java

public class CalculadoraImpl extends java.rmi.server.UnicastRemoteObject implements Calculadora {&nbsp;

&nbsp;&nbsp; &nbsp;public CalculadoraImpl() throws java.rmi.RemoteException {&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;super();&nbsp;
&nbsp;&nbsp; &nbsp;}&nbsp;

&nbsp;&nbsp; &nbsp;public String imposto(String a, long b)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;throws java.rmi.RemoteException {&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;System.out.println ("Calculando imposto");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;String tmp ="";
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if(b&lt;1200){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tmp = a + " pagará "+ (b-(b*0.05)) +" de imposto";
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}else if(b&gt;=1200 &amp;&amp; b&lt;1800){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tmp = a + " pagará "+ (b-(b*0.10))+" de imposto";
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}else{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tmp = a + " pagará "+ (b-(b*0.15))+" de imposto";
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}


&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return tmp;
&nbsp;&nbsp; &nbsp;}&nbsp;

&nbsp;&nbsp; &nbsp;&nbsp;
}&nbsp;

CalculadoraServidor.javaimport java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;
public class CalculadoraServidor {
&nbsp;&nbsp; public CalculadoraServidor() {&nbsp;&nbsp; &nbsp; try {&nbsp;&nbsp; &nbsp; &nbsp; Calculadora c = new CalculadoraImpl();&nbsp;&nbsp; &nbsp; &nbsp; Registry registry =LocateRegistry.createRegistry(1500);&nbsp;&nbsp; &nbsp; &nbsp; registry.bind("calculando",c);&nbsp;&nbsp; &nbsp; } catch (Exception e) {&nbsp;&nbsp; &nbsp; &nbsp; System.out.println("Problema: " + e);&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp; }
&nbsp;&nbsp; public static void main(String args[]) {&nbsp;&nbsp; &nbsp; new CalculadoraServidor();&nbsp;&nbsp; }}

Copie, melhore e disponibilize.


Até mais!