Path: blob/master/test/jdk/java/rmi/reliability/juicer/ApplicationServer.java
41153 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.NotBoundException;24import java.rmi.RemoteException;25import java.rmi.registry.Registry;26import java.rmi.registry.LocateRegistry;27import java.util.logging.Logger;28import java.util.logging.Level;2930/**31* The ApplicationServer class provides the other server side of the "juicer"32* stress test of RMI.33*/34public class ApplicationServer implements Runnable {3536/** number of remote Apple objects to export */37private static final Logger logger = Logger.getLogger("reliability.orange");38private static final int LOOKUP_ATTEMPTS = 5;39private static final int DEFAULT_NUMAPPLES = 10;40private static final String DEFAULT_REGISTRYHOST = "localhost";41private static final int DEFAULT_REGISTRYPORT = -1;42private final int numApples;43private final String registryHost;44private final int registryPort;45private final Apple[] apples;46private AppleUser user;4748ApplicationServer(int registryPort) {49this(DEFAULT_NUMAPPLES, DEFAULT_REGISTRYHOST, registryPort);50}5152ApplicationServer(int numApples, String registryHost, int registryPort) {53this.numApples = numApples;54this.registryHost = registryHost;55this.registryPort = registryPort;56apples = new Apple[numApples];57}5859/*60* On initialization, export remote objects and register61* them with server.62*/63@Override64public void run() {65try {66int i = 0;6768/*69* Locate apple user object in registry. The lookup will occur70* every 5 seconds until it is successful or timeout 50 seconds.71* These repeated attempts allow the ApplicationServer72* to be started before the AppleUserImpl.73*/74Exception exc = null;75long stopTime = System.currentTimeMillis() + LOOKUP_ATTEMPTS * 10000;76while (System.currentTimeMillis() < stopTime) {77try {78Registry registry = LocateRegistry.getRegistry(79registryHost, registryPort);80user = (AppleUser) registry.lookup("AppleUser");81user.startTest();82break; //successfully obtained AppleUser83} catch (RemoteException | NotBoundException e) {84exc = e;85Thread.sleep(5000); //sleep 5 seconds and try again86}87}88if (user == null) {89logger.log(Level.SEVERE, "Failed to lookup AppleUser:", exc);90return;91}9293/*94* Create and export apple implementations.95*/96try {97for (i = 0; i < numApples; i++) {98apples[i] = new AppleImpl("AppleImpl #" + (i + 1));99}100} catch (RemoteException e) {101logger.log(Level.SEVERE,102"Failed to create AppleImpl #" + (i + 1) + ":", e);103user.reportException(e);104return;105}106107/*108* Hand apple objects to apple user.109*/110try {111for (i = 0; i < numApples; i++) {112user.useApple(apples[i]);113}114} catch (RemoteException e) {115logger.log(Level.SEVERE,116"Failed to register callbacks for " + apples[i] + ":", e);117user.reportException(e);118}119} catch (InterruptedException | RemoteException e) {120logger.log(Level.SEVERE, "Unexpected exception:", e);121}122}123124private static void usage() {125System.err.println("Usage: ApplicationServer [-numApples <numApples>]");126System.err.println(" [-registryHost <host>]");127System.err.println(" -registryPort <port>");128System.err.println(" numApples The number of apples (threads) to use.");129System.err.println(" The default is 10 apples.");130System.err.println(" host The host running rmiregistry " +131"which contains AppleUser.");132System.err.println(" The default is \"localhost\".");133System.err.println(" port The port the rmiregistry is running" +134"on.");135System.err.println();136}137138public static void main(String[] args) {139int num = DEFAULT_NUMAPPLES;140int port = -1;141String host = DEFAULT_REGISTRYHOST;142143// parse command line args144try {145for (int i = 0; i < args.length ; i++ ) {146String arg = args[i];147switch (arg) {148case "-numApples":149i++;150num = Integer.parseInt(args[i]);151break;152case "-registryHost":153i++;154host = args[i];155break;156case "-registryPort":157i++;158port = Integer.parseInt(args[i]);159break;160default:161usage();162break;163}164}165166if (port == -1) {167usage();168throw new RuntimeException("Port must be specified.");169}170} catch (Throwable t) {171usage();172throw new RuntimeException("TEST FAILED: Bad argument");173}174175// start the client server176Thread server = new Thread(new ApplicationServer(num,host,port));177server.start();178// main should exit once all exported remote objects are gc'd179}180}181182183