Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/rmi/reliability/juicer/ApplicationServer.java
38828 views
/*1* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.rmi.RemoteException;24import java.rmi.registry.Registry;25import java.rmi.registry.LocateRegistry;26import java.util.logging.Logger;27import java.util.logging.Level;2829/**30* The ApplicationServer class provides the other server side of the "juicer"31* stress test of RMI.32*/33public class ApplicationServer implements Runnable {3435/** number of remote Apple objects to export */36private static final Logger logger = Logger.getLogger("reliability.orange");37private static final int LOOKUP_ATTEMPTS = 5;38private static final int DEFAULT_NUMAPPLES = 10;39private static final String DEFAULT_REGISTRYHOST = "localhost";40private static final int DEFAULT_REGISTRYPORT = -1;41private final int numApples;42private final String registryHost;43private final int registryPort;44private final Apple[] apples;45private AppleUser user;4647ApplicationServer(int registryPort) {48this(DEFAULT_NUMAPPLES, DEFAULT_REGISTRYHOST, registryPort);49}5051ApplicationServer(int numApples, String registryHost, int registryPort) {52this.numApples = numApples;53this.registryHost = registryHost;54this.registryPort = registryPort;55apples = new Apple[numApples];56}5758/*59* On initialization, export remote objects and register60* them with server.61*/62public void run() {63try {64int i = 0;6566/*67* Locate apple user object in registry. The lookup will68* occur until it is successful or fails LOOKUP_ATTEMPTS times.69* These repeated attempts allow the ApplicationServer70* to be started before the AppleUserImpl.71*/72Exception exc = null;73for (i = 0; i < LOOKUP_ATTEMPTS; i++) {74try {75Registry registry = LocateRegistry.getRegistry(76registryHost, registryPort);77user = (AppleUser) registry.lookup("AppleUser");78user.startTest();79break; //successfully obtained AppleUser80} catch (Exception e) {81exc = e;82Thread.sleep(10000); //sleep 10 seconds and try again83}84}85if (user == null) {86logger.log(Level.SEVERE, "Failed to lookup AppleUser:", exc);87return;88}8990/*91* Create and export apple implementations.92*/93try {94for (i = 0; i < numApples; i++) {95apples[i] = new AppleImpl("AppleImpl #" + (i + 1));96}97} catch (RemoteException e) {98logger.log(Level.SEVERE,99"Failed to create AppleImpl #" + (i + 1) + ":", e);100user.reportException(e);101return;102}103104/*105* Hand apple objects to apple user.106*/107try {108for (i = 0; i < numApples; i++) {109user.useApple(apples[i]);110}111} catch (RemoteException e) {112logger.log(Level.SEVERE,113"Failed to register callbacks for " + apples[i] + ":", e);114user.reportException(e);115return;116}117} catch (Exception e) {118logger.log(Level.SEVERE, "Unexpected exception:", e);119}120}121122private static void usage() {123System.err.println("Usage: ApplicationServer [-numApples <numApples>]");124System.err.println(" [-registryHost <host>]");125System.err.println(" -registryPort <port>");126System.err.println(" numApples The number of apples (threads) to use.");127System.err.println(" The default is 10 apples.");128System.err.println(" host The host running rmiregistry " +129"which contains AppleUser.");130System.err.println(" The default is \"localhost\".");131System.err.println(" port The port the rmiregistry is running" +132"on.");133System.err.println();134}135136public static void main(String[] args) {137int num = DEFAULT_NUMAPPLES;138int port = -1;139String host = DEFAULT_REGISTRYHOST;140141// parse command line args142try {143for (int i = 0; i < args.length ; i++ ) {144String arg = args[i];145if (arg.equals("-numApples")) {146i++;147num = Integer.parseInt(args[i]);148} else if (arg.equals("-registryHost")) {149i++;150host = args[i];151} else if (arg.equals("-registryPort")) {152i++;153port = Integer.parseInt(args[i]);154} else {155usage();156}157}158159if (port == -1) {160usage();161throw new RuntimeException("Port must be specified.");162}163} catch (Throwable t) {164usage();165throw new RuntimeException("TEST FAILED: Bad argument");166}167168// start the client server169Thread server = new Thread(new ApplicationServer(num,host,port));170server.start();171// main should exit once all exported remote objects are gc'd172}173}174175176