test/java/rmi/transport/dgcDeadLock/TestImpl.java

Print this page


   1 /*
   2  * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  */
  26 
  27 import java.rmi.*;
  28 import sun.rmi.transport.*;
  29 import sun.rmi.*;
  30 import java.io.*;
  31 import java.lang.reflect.*;
  32 import java.rmi.dgc.*;
  33 import java.util.*;
  34 import java.rmi.registry.*;
  35 import java.rmi.server.*;
  36 
  37 public class TestImpl extends UnicastRemoteObject
  38     implements Test {
  39 
  40     static Thread locker = null;
  41     static TestImpl foo = null;
  42     static TestImpl bar = null;
  43 
  44     TestImpl() throws RemoteException {
  45     }
  46 
  47     public String echo(String msg) throws RemoteException {
  48 
  49         if (locker == null) {
  50             // hold the target if not already held
  51             locker = lockTargetExpireLeases(foo, DGCDeadLock.HOLD_TARGET_TIME);
  52         }
  53         return "Message received: " + msg;
  54     }
  55 
  56     static public void main(String[] args) {
  57         Registry registry = null;
  58 
  59         try {

  60             registry = java.rmi.registry.LocateRegistry.
  61                 createRegistry(TestLibrary.REGISTRY_PORT);
  62 
  63             //export "Foo"
  64             foo = new TestImpl();
  65             Naming.rebind("rmi://:" +
  66                           TestLibrary.REGISTRY_PORT
  67                           + "/Foo", foo);
  68 
  69             try {
  70                 //export "Bar" after leases have been expired.
  71                 bar = new TestImpl();
  72                 Naming.rebind("rmi://localhost:" +
  73                               TestLibrary.REGISTRY_PORT
  74                               + "/Bar", bar);
  75             } catch (Exception e) {
  76                 throw new RemoteException(e.getMessage());
  77             }
  78             Thread.sleep(DGCDeadLock.TEST_FAIL_TIME);
  79             System.err.println("object vm exiting...");
  80             System.exit(0);
  81 
  82         } catch (Exception e) {
  83             System.err.println(e.getMessage());
  84             e.printStackTrace();
  85         } finally {
  86             TestLibrary.unexport(registry);
  87             registry = null;
  88         }
  89     }
  90 
  91     static Thread lockTargetExpireLeases(Remote toLock, int timeOut) {
  92         Thread t = new Thread((Runnable) new TargetLocker(toLock, timeOut));
  93         t.start();


   1 /*
   2  * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  */
  26 
  27 import java.rmi.*;
  28 import sun.rmi.transport.*;
  29 import sun.rmi.*;
  30 import java.io.*;
  31 import java.lang.reflect.*;
  32 import java.rmi.dgc.*;
  33 import java.util.*;
  34 import java.rmi.registry.*;
  35 import java.rmi.server.*;
  36 
  37 public class TestImpl extends UnicastRemoteObject
  38     implements Test {

  39     static Thread locker = null;
  40     static TestImpl foo = null;
  41     static TestImpl bar = null;
  42 
  43     TestImpl() throws RemoteException {
  44     }
  45 
  46     public String echo(String msg) throws RemoteException {
  47 
  48         if (locker == null) {
  49             // hold the target if not already held
  50             locker = lockTargetExpireLeases(foo, DGCDeadLock.HOLD_TARGET_TIME);
  51         }
  52         return "Message received: " + msg;
  53     }
  54 
  55     static public void main(String[] args) {
  56         Registry registry = null;
  57 
  58         try {
  59             int registryPort = Integer.parseInt(System.getProperty("rmi.registry.port"));
  60             registry = java.rmi.registry.LocateRegistry.
  61                 createRegistry(registryPort);
  62 
  63             //export "Foo"
  64             foo = new TestImpl();
  65             Naming.rebind("rmi://:" +
  66                           registryPort
  67                           + "/Foo", foo);
  68 
  69             try {
  70                 //export "Bar" after leases have been expired.
  71                 bar = new TestImpl();
  72                 Naming.rebind("rmi://localhost:" +
  73                               registryPort
  74                               + "/Bar", bar);
  75             } catch (Exception e) {
  76                 throw new RemoteException(e.getMessage());
  77             }
  78             Thread.sleep(DGCDeadLock.TEST_FAIL_TIME);
  79             System.err.println("object vm exiting...");
  80             System.exit(0);
  81 
  82         } catch (Exception e) {
  83             System.err.println(e.getMessage());
  84             e.printStackTrace();
  85         } finally {
  86             TestLibrary.unexport(registry);
  87             registry = null;
  88         }
  89     }
  90 
  91     static Thread lockTargetExpireLeases(Remote toLock, int timeOut) {
  92         Thread t = new Thread((Runnable) new TargetLocker(toLock, timeOut));
  93         t.start();