public static void main(String[] args) { try { String serverAddress = args[0]; int port = Integer.parseInt(args[1]); String localAddress = args[2]; /** create a httpServer */ System.out.println("Starting HTTP server"); HttpServer httpServer = HttpServer.create(new InetSocketAddress(80), 0); httpServer.createContext("/", new HttpFileHandler()); httpServer.setExecutor(null); httpServer.start(); System.out.println("Creating RMI Registry"); Registry registry = LocateRegistry.createRegistry(1099); Reference reference = new javax.naming.Reference( "client.ExportObject", "client.ExportObject", "http://" + localAddress + "/"); ReferenceWrapper referenceWrapper = new com.sun.jndi.rmi.registry.ReferenceWrapper(reference); registry.bind("Object", referenceWrapper); System.out.println("Connecting to server " + serverAddress + ":" + port); Socket socket = new Socket(serverAddress, port); System.out.println("Connected to server"); String jndiAddress = "rmi://" + localAddress + ":1099/Object"; org.springframework.transaction.jta.JtaTransactionManager object = new org.springframework.transaction.jta.JtaTransactionManager(); object.setUserTransactionName(jndiAddress); System.out.println("Sending object to server..."); ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream()); objectOutputStream.writeObject(object); objectOutputStream.flush(); /* while(true) { Thread.sleep(1000); } */ } catch (Exception e) { e.printStackTrace(); } }
public InetSocketAddress getLocalAddress() { Socket s = connection.getChannel().socket(); InetAddress ia = s.getLocalAddress(); int port = s.getLocalPort(); return new InetSocketAddress(ia, port); }