Технологии Java
Сериализация и RMI
public class Data implements Serializable { private List<Integer> data; ... }
public class Data2 extends Data { private transient String toStringCache; ... }
private static final long serialVersionUID
serialver <имя класса>
class Rectangle { int x1, y1, x2, y2 // toString String.format("%d %d %d %d", x1, y1, x2, y2); }
class Rectangle { int x1, y1, w, h; // toString String.format("%d %d %d %d", x1, y1, x1+w, y1+h); }
ObjectOutputStream oos = ... oos.writeObject(new Rectangle(1, 2, 10, 20));
ObjectInputStream ois = ... System.out.println(ois.readObject());
private static final long serialVersionUID = 6729985638487173456L;
w == h == 0
private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[]{ new ObjectStreamField("x1", int.class), new ObjectStreamField("y1", int.class), new ObjectStreamField("w", int.class), new ObjectStreamField("h", int.class) };
private void readObject(final ObjectInputStream in) throws IOException { ObjectInputStream.GetField fields = in.readFields(); x1 = fields.get("x1", 0); y1 = fields.get("y1", 0); w = fields.get("x2", 0) - x1; h = fields.get("y2", 0) - y1; }
ObjectOutputStream oos = ... oos.writeObject(new Rectangle(1, 2, 10, 20));
ObjectInputStream ois = ... System.out.println(ois.readObject());
private void writeObject(final ObjectOutputStream out) throws IOException { ObjectOutputStream.PutField fields = out.putFields(); fields.put("x1", x1); fields.put("y1", y1); fields.put("x2", x1 + w); fields.put("y2", y1 + h); out.writeFields(); }
private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[]{ new ObjectStreamField("x1", int.class), new ObjectStreamField("y1", int.class), new ObjectStreamField("x2", int.class), new ObjectStreamField("y2", int.class) };
public interface Bank extends Remote { // Создает счет public Account createAccount(String id) throws RemoteException; // Возвращает счет public Account getAccount(String id) throws RemoteException; }
public interface Account extends Remote { // Идентификатор public String getId() throws RemoteException; // Количество денег public int getAmount() throws RemoteException; // Изменить количество денег public void setAmount(int amount) throws RemoteException; }
class RemoteAccount implements Account { public String getId() { return id; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } }
class RemoteBank implements Bank { public Account createAccount(String id) { Account account = new RemoteAccount(id); accounts.put(id, account); return account; } public Account getAccount(String id) { return accounts.get(id); } }
Bank bank = new RemoteBank(); try { UnicastRemoteObject.exportObject(bank); Naming.rebind("//localhost/bank", bank); } catch (RemoteException e) { System.out.println("Cannot export object: " + e.getMessage()); } catch (MalformedURLException e) { System.out.println("Malformed URL"); }
Bank bank; try { bank = (Bank) Naming.lookup("//localhost/bank"); } catch (NotBoundException e) { System.out.println("Bank is not bound"); return; } catch (MalformedURLException e) { System.out.println("Bank URL is invalid"); return; }
Account account = bank.getAccount("geo"); if (account == null) { System.out.println("Creating account"); account = bank.createAccount("geo"); } else { System.out.println("Account already exists"); }
System.out.println("Money: " + account.getAmount()); System.out.println("Adding money"); account.setAmount(account.getAmount() + 100); System.out.println("Money: " + account.getAmount());
rmic RemoteAccount RemoteBank
rmiregistry