Wersja z użyciem java.util.concurrent na dole.
=== RMI: Bufor 1-elementowy ===
Interfejs bufora
import java.io.Serializable;
public interface Bufferable extends Serializable
{
}
Implementacja interfejsu bufora
public class BufferableInteger implements Bufferable
{
private static final long serialVersionUID = -3124822365091517613L;
public int value;
}
Interfejs
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RemoteServer extends Remote
{
public Bufferable get() throws RemoteException;
public void put(Bufferable o) throws RemoteException;
}
Implementacja serwera
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class ServerImplementation extends UnicastRemoteObject implements RemoteServer
{
private static final long serialVersionUID = -1205845288411624541L;
private Bufferable buffer;
protected ServerImplementation() throws RemoteException
{
super();
}
@Override
synchronized public Bufferable get()
{
System.out.println("get");
return buffer;
}
@Override
synchronized public void put(Bufferable o)
{
buffer = o;
System.out.println("put");
}
}
Przykład programu serwera
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
public class Server
{
public static void main(String[] args)
{
if (System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
}
String name = "//localhost:1099/Counter";
try
{
ServerImplementation server = new ServerImplementation();
Naming.rebind(name, server);
System.out.println("Server ok");
}
catch (Exception e)
{
System.err.println("Server excep." + e.getMessage());
e.printStackTrace();
}
}
}
Przykład programu klienta-producenta (dostawcy)
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
public class Putter
{
public static void main(String[] args)
{
System.out.println("Start");
if (System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
}
try
{
String name = "//"+args[0]+"/Counter";
System.out.println(name);
RemoteServer server = (RemoteServer) Naming.lookup(name);
BufferableInteger bi = new BufferableInteger();
bi.value = Integer.parseInt(args[1]);
server.put(bi);
System.out.println("set " + bi.value);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Przykład programu klienta-konsumenta (odbiorcy)
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
public class Getter
{
public static void main(String[] args)
{
System.out.println("Start");
if (System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
}
try
{
String name = "//"+args[0]+"/Counter";
System.out.println(name);
RemoteServer server = (RemoteServer) Naming.lookup(name);
BufferableInteger bi = (BufferableInteger)(server.get());
System.out.println(bi.value);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
=== RMI: Bufor 1-elementowy z JUC===
Interfejs bufora
import java.io.Serializable;
public interface IBufferable extends Serializable {
}
Implementacja interfejsu bufora
public class BufferableInteger implements IBufferable {
public int value;
}
Interfejs
import java.rmi.Remote;
public interface IBuffer extends Remote{
public void putValue(IBufferable value) throws Exception;
public IBufferable getValue() throws Exception;
}
Implementacja serwera
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Server extends UnicastRemoteObject implements IBuffer {
private IBufferable value;
private int count = 0;
private final Lock mutex = new ReentrantLock();
private final Condition event = mutex.newCondition();
public Server() throws RemoteException {
}
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
Server serv = new Server();
Registry r = LocateRegistry.createRegistry(6001);
r.rebind("Server", serv);
} catch (Exception e) {
System.err.println("Błąd" + e);
}
}
@Override
public void putValue(IBufferable value) {
try {
mutex.lock();
while (count != 0)
event.await();
this.value = value;
count++;
event.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
mutex.unlock();
}
}
@Override
public IBufferable getValue() {
IBufferable result;
try {
mutex.lock();
while (count == 0)
event.await();
result = this.value;
count--;
event.signal();
} catch (InterruptedException e) {
e.printStackTrace();
return null;
} finally {
mutex.unlock();
}
return result;
}
}
Implementacja klienta
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
public class Client {
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
Client c1 = new Client();
try {
IBuffer obj = (IBuffer) Naming.lookup("//localhost:6001/Server");
BufferableInteger foo = new BufferableInteger();
foo.value = 105;
obj.putValue(foo);
System.out.println(((BufferableInteger)obj.getValue()).value);
} catch (Exception e) {
System.err.println("Błąd: " + e.getMessage());
e.printStackTrace();
}
}
}