RMI & NetBeans RCP

I've seen a few questions recently about how to use RMI with NetBeans RCP. As always, I think the best way to solve any problem on NetBeans RCP is to start by forgetting about NetBeans RCP. Just get something small working outside the NetBeans RCP and then, once that's all working, move it to NetBeans RCP.

So, I followed these handy steps:

http://www.uwplatt.edu/csse/courses/prev/s05/se333/labs/lab2.html

And now I have a small scenario in a standard Java SE application, with RMI:

Above, there are 4 classes, created via the steps in the link above.

Here is Names.java:

package demo;

import java.io.Serializable;

public class Names implements Serializable {

    protected String[] theNames = new String[1000];
    protected int nameCount = 0;

    public void add(String name) {
        theNames[nameCount] = name;
        ++nameCount;
    }

    public String[] getList() {
        return theNames;
    }
 
}

And here is NamesService.java:

package demo;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface NamesService extends Remote {

    public void addName(String name) throws RemoteException;

    public Names getNames() throws RemoteException;

}

Here is NamesServiceImpl.java:

package demo;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class NamesServiceImpl extends UnicastRemoteObject implements NamesService {

    public Names names = new Names();

    public static void main(String[] args) {
        System.out.println("Initializing NamesService...");
        try {
            NamesService svc = new NamesServiceImpl();
            Naming.rebind("rmi://localhost:13330/Names", svc);
            System.out.println("NamesService running.");
        } catch (Exception ex) {
            System.out.println("Exception: " + ex.getMessage());
        }
    }

    public NamesServiceImpl() throws RemoteException {
        super();
    }

    @Override
    public void addName(String name) throws RemoteException {
        names.add(name);
        System.out.println("Added '" + name + "'");
    }

    @Override
    public Names getNames() throws RemoteException {
        return names;
    }
 
}

And, finally, I have a JFrame, named NamesClient.java, with the GUI components shown in the screenshot above. Here is the start of that class:

package demo;

import java.rmi.Naming;

public class NamesClient extends javax.swing.JFrame {

    public NamesService remoteNames;

    public NamesClient() {
        initComponents();
        try {
            System.out.println("Connecting...");
            remoteNames = (NamesService) Naming.lookup("rmi://localhost:13330/Names");
            refresh();
        } catch (Exception ex) {
            System.out.println("Exception: " + ex.getMessage());
            System.exit(1);
        }
    }

    private void refresh() {
        try {
            NamesList.setListData(remoteNames.getNames().getList());
        } catch (Exception ex) {
            System.out.println("Exception: " + ex.getMessage());
            System.exit(1);
        }
    }
    ...
    ...
    ...

There's also an Add button in that class, like this:

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
    try {
        remoteNames.addName(NameField.getText());
        refresh();
    } catch (Exception ex) {
        System.out.println("Exception: " + ex.getMessage());
        System.exit(1);
    }
    NameField.setText("");
}

And that's all. Once I have the above in my "demo" package, I compile the application. Then, I go to the command prompt, all the way into the "build/classes" package, and type "rmic demo.NamesServiceImpl", which creates a stub class. I then, in the same "build/classes" folder, run "rmiregistry 13330". Finally, back in the IDE, I first run the NamesServiceImpl.java file, which initializes and starts the NamesService. Then I run the NamesClient.java file, which connects to the NamesService.

Then everything is working correctly.

Is this what anyone wants to see working on NetBeans RCP? Or is this not a scenario that anyone cares about? And if anyone already knows how to optimally port the above to the NetBeans Platform, feel free to add a comment about that at the end of this blog entry or, better still, write about it in your own blog.

Comments:

I think the larger issue with RMI is dealing with marshalling/unmarshalling custom types and how that happens in NetBeans Platform where visibility restrictions are enforced via hierarchical classloaders.

The last time I needed to do anything with RMI was probably 1999, but I have seen people asking about it on the dev@platform list over the years. Usually classloading is at the root of those questions, even if the people asking don't realize it.

One trick (or hack, depending on your perspective) is to put JARs containing custom types in the platformNN/lib directory so the classes are always loaded by the system classloader and thus accessible regardless of predefined module dependencies.

Posted by guest on August 28, 2011 at 05:35 AM PDT #

For the record, I am the 'guest' who wrote the first comment.

It seems your blog replaces the name typed with 'guest' when one previews the comment before posting it. I'd guess that's not the intended behavior, but probably explains why you get so many 'guest' comments on your blog.

Posted by Tom Wheeler on August 28, 2011 at 06:50 AM PDT #

I am using a Server/Client application that uses RMI, I can get it to work locally (using localhost), however I can not connect my client to the server from outside my home network. I am losing my mind trying to get this to work. Is there a step-by-step guide to getting RMI to connect a remote client to a server using Netbeans?

Posted by guest on January 24, 2012 at 06:22 PM PST #

Glassfish3 and RMI. We have old client(swing)/server application which used RMI old fashion way (Stabs/skeletons,etc). It was running on tomcat. We need to Manvenize it first and make it run on Glassfish3. What would be your advise? Is it even possible? I spend several days with no luck. What would be your advise? should we give up on RMI and switch to build EJB application instead? Do you have any examples or posts which I maybe missed regarding "RMI on Glassfish3 build using Netbeans" subject?
Thanks in advance.

Posted by galina on January 23, 2013 at 12:07 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
19
21
22
23
24
25
26
27
28
29
30
   
       
Today