0

I'm working through the proxy pattern section in Head First Design Patterns 2nd edition, by Freeman and Robson. I'm running Windows 10 and I've determined that localhost is functioning properly as the Windows IIS screen comes up when i navigate to localhost or http://127.0.0.1/. rmiregistry is started beforehand from the cmd line with no issue. I'm running the test with the firewall turned off and I'm still getting the error stack trace listed below. I'm executing the GumballMachineTestDrive class directly in Eclipse by right clicking on the .java file and selecting "Run As" "Java Application". The screenshot below shows the run configurations for GumballMachineTestDrive, where args[0] == "localhost" and args 1 == 112. I've pinged localhost and 127.0.0.1, both of which show no issues. I previously tried to set the rmi server hostname as can be seen in the line "System.setProperty("java.rmi.server.hostname", "127.0.0.1");", however this did not make a difference. Any guidance in the right direction would be greatly appreciated.

GumballMachineTestDrive-Run Configurations

Service code:

package GumballMachineStatePattern;

import java.rmi.Naming;

public class GumballMachineTestDrive {

    public static void main(String[] args) {
        GumballMachineRemote gumballMachine = null;
        
    //  System.setProperty("java.rmi.server.hostname", "127.0.0.1");
        
        int count;
        
        if (args.length < 2) {
            System.out.println("GumballMachine <name> <inventory>");
            System.exit(1);
        }
        
        try {
        
        count = Integer.parseInt(args[1]);
        
        gumballMachine = new GumballMachine(args[0], count);
        Naming.rebind("//" + args[0] + "/gumballmachine", gumballMachine);
        
        } catch (Exception e) {
            e.printStackTrace();
        }
}
}

Code for GumballMachine class:

package GumballMachineStatePattern;
import java.rmi.*;
import java.rmi.server.*;

public class GumballMachine extends UnicastRemoteObject implements GumballMachineRemote {
    
    private static final long serialVersionUID = 2L;

State soldOutState;
State noQuarterState;
State hasQuarterState;
State soldState;
State winnerState;

State state = soldOutState;
int count = 0;

String location;
    
    public GumballMachine(String location, int count) throws RemoteException {
        soldOutState = new SoldOutState(this);
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldState = new SoldState(this);
        winnerState = new WinnerState(this);
        
        this.location = location;
        
        this.count = count;
        if (count > 0) {
            state = noQuarterState;
        } else {
            state = soldOutState;
        }
    }
    
    public void insertQuarter() {
        state.insertQuarter();
    }
    
    public void ejectQuarter() {
        state.ejectQuarter();
    }
    
    public void turnCrank() {
        state.turnCrank();
        state.dispense();
    }
    
    void setState(State state) {
        this.state = state;
    }
    
    void releaseBall() {
        System.out.println("A gumball comes rolling out of the slot...");
        if (count > 0) {
            count = count - 1;
        }
    }
    
    public int getCount() {
        return count;
    }
    
    void refill(int count) {
        this.count += count;
        System.out.println("The gumball machine was just refilled; its new count is: " + this.count);
        state.refill();
    }
    
    public State getState() {
        return state;
    }
    
    public State getSoldOutState() {
        return soldOutState;
    }
    
    public State getNoQuarterState() {
        return noQuarterState;
    }
    
    public State getHasQuarterState() {
        return hasQuarterState;
    }
    
    public State getSoldState() {
        return soldState;
    }
    
    public State getWinnerState() {
        return winnerState;
    }
    
    public String getLocation() {
        return location;
    }
        
    public String toString() {
        StringBuffer result = new StringBuffer();
        result.append("\nMighty Gumball, Inc.");
        result.append("\nJave-enabled Standing Gumball Model #2004\n");
        result.append("Inventory: " + count + " gumball");
        
        if (count != 1) {
            result.append("s");
        }
        result.append("\n");
        result.append("Machine is " + state + "\n");
        
        return result.toString();
    }
}

GumballMachineTestDrive stack trace where args[0] == "localhost" and args1 = 112

java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
    java.net.ConnectException: Connection refused: connect
    at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:626)
    at java.rmi/sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:217)
    at java.rmi/sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:204)
    at java.rmi/sun.rmi.server.UnicastRef.newCall(UnicastRef.java:344)
    at java.rmi/sun.rmi.registry.RegistryImpl_Stub.rebind(RegistryImpl_Stub.java:150)
    at java.rmi/java.rmi.Naming.rebind(Naming.java:177)
    at GumballMachineStatePattern.GumballMachineTestDrive.main(GumballMachineTestDrive.java:24)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.base/sun.nio.ch.Net.connect0(Native Method)
    at java.base/sun.nio.ch.Net.connect(Net.java:579)
    at java.base/sun.nio.ch.Net.connect(Net.java:568)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:633)
    at java.base/java.net.Socket.connect(Socket.java:583)
    at java.base/java.net.Socket.<init>(Socket.java:507)
    at java.base/java.net.Socket.<init>(Socket.java:287)
    at java.rmi/sun.rmi.transport.tcp.TCPDirectSocketFactory.createSocket(TCPDirectSocketFactory.java:40)
    at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:620)
    ... 6 more
3
  • You're trying to access localhost at port 112. Is your IIS running on port 112? Commented May 10, 2022 at 2:02
  • Java and RMI are unrelated to IIS. You should read Getting Started Using Java™ RMI. Especially Start the Java RMI registry, server, and client. Commented May 10, 2022 at 2:07
  • ewong, the integer 112 is passed to args[1], which is passed as the second argument in the "GumballMachine" constructor. It represents the initial count of gumballs in the GumballMachine. Elliot, my thinking was that with IIS enabled and showing at "localhost", I would be able to rule out firewall issues. I will review the recommended documentation. Commented May 10, 2022 at 8:25

2 Answers 2

0

I solved the problem. I needed to re-do the project as a fresh java project to clear up what were probably some dependency conflicts. It also served as a good learning lesson rmi and xampp local server configuration as well.

Sign up to request clarification or add additional context in comments.

Comments

0

I experienced the same issue today when trying to reproduce the proxy pattern's Gumball Machine Remote monitoring example. I'm using JDK 21. I resolved it with following steps. I am using intellij. so few steps are quite simple. This method should work for all jdk versions though!
You need three terminals.

1. Perform maven clean from from the side bar in intellij and then do maven compile

2. In terminal 1, navigate to the root directory's target/classes and start rmiregistry using the command rmiregistry &

3. In terminal 2, get to the root directory of your project (from where you will have access to target directory) and run the following command java -cp target/classes org.example.proxyPattern.step2_gumballWithProxy.GumballMachineTestDrive localhost 25 Here, localhost is the location and 25 is the gumball count.

4.In terminal 3, get to the root directory and we can run the monitor class using the following command java -cp target/classes org.example.proxyPattern.step2_gumballWithProxy.GumballMonitorTestDrive

This should resolve your error and you get your expected output from terminal 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.