Learn JavaFX Script, Part II: Remote communication using RMI __java

Source: Internet
Author: User
Tags netbeans
Original address: http://java.sun.com/developer/technicalArticles/scripting/javafxpart2/

The first part of this series introduces Java programmers to the syntax and semantics of the JavaFX scripting language. Based on your knowledge of JavaFX scripting, this article invokes the Java platform's remote method invocation (Invocation,java RMI) class library, enabling the JavaFX graphical user interface (GUI) to communicate remotely. RMI is very straightforward to use in JavaFX scripts and can be done in a simple and quick way to demonstrate and test client-server functionality.

Create a new JavaFX scripting project in NetBeans (creating a JavaFX script project in the NetBeans IDE)
First, create a new JavaFX scrip project in the NetBeans Integrated development Environment (IDE) 5.5.1, and if you've never done so, be sure to read this excellent article, "Getting Started with JavaFX Script Language (for Swing programmers), which has a detailed description. However, if you understand these methods and have downloaded and installed the JavaFX module from NetBeans Beta Update Center, you will understand the key points here.
1, select the File menu from NetBeans's main menu, and then select Net Project.
2. In the New Project Wizard dialog box, select General in the Categories Panel and select Java application in the Project panel. Click Next.
3, enter the Javafxclient in the Project Name field.
4. In Project location, click Browse, and then specify the location of the storage project.
5. Deselect the check box for set as main project and create main class, then click Finish.
Now, use the IDE to create a JavaFX script file named HelloServer.
1, in the Projects window, right click Javafxclient > Source Packages node, select New > File/folder.
2, in the Categories panel, select Other, then in the File Types panel, select JavaFX file. Then click Next. Now, when you invoke the new or source packages node of the main engineering node in the Projects window, the JavaFX file type should appear in the list of available file types.
3. In the name and Location page of the New JavaFX File Wizard, enter src/client in the Myclient,folder field in file name. Note that the new file name now appears in the Created file field.
4, click Finish to complete the new project.
5, in the Projects window, right click on the Javafxclient node, select Properties.
6, in the Properties dialog box in the Categories panel, select Run.
7, enter the name of the script in the arguments domain: client. MyClient, click OK.
A null JavaFX script file named Myclient.fx will be created in the client directory of the Javafxclient project. At this point, leave the file blank, and then set another part of the project--and then return to it once the RMI server is set up.

client-server communication using remote method call (RMI) (Client-server communication with remote methods Invocation (RMI))
Java Remote method Invocation (Java RMI) is a Java application interface (API) used to implement remote equivalence object invocation. Using RMI to develop distributed programs is simpler than using sockets, because developers don't need to define protocols, and defining protocols is often time-consuming and error-prone work.
Using RMI, the developer has the illusion of invoking the local method from the local class file, in fact, the parameter is sent to the remote target and interpreted, and the result is sent back to the caller. Because JavaFX script can invoke Java objects, all RMI is a quick and easy tool for JAVAFX clients to communicate remotely.
Developing a distributed JavaFX script using RMI includes steps:
1, define a remote interface.
2, realize the remote interface.
3, Development server.
4. Use Java to develop a connection assistant for JavaFX script.
5, create a JavaFX script client that invokes the connection assistant above.
The authors of this article compress the first three steps from the Qusay Mahmoud's excellent article "Distributed Java Programming with RMI and CORBA" because they are indistinguishable from the traditional Java implementation of RMI. The last two steps are modified for JavaFX script. However, as you go deep into each step, you need to create a new NetBeans IDE project for the server.
In NetBeans, follow the steps below:
1, select New Project from the File menu. You should see a dialog box similar to Figure 1.

Figure 1 Creating a new project in NetBeans5.5.1
2, in the Categories list, select General. In the Projects list, select the Java application. Click Next. You should see a dialog box similar to Figure 2.

Figure 2 Customizing the new project in the NetBeans IDE
3, enter Javarmiserver in Project name. Select the appropriate folder to save the project, and then cancel the selection of the set as Main project. Finally, click the Finish button. This creates a Java project called Javarmiserver.

Define a remote interface (Define a sqlremote Interface)
Now, create a Java interface named Serverinterface in the server package, as shown in code example 1. The interface Serverinterface contains a method, ping (), which takes a string parameter and returns a string of Hello plus this parameter.
code Example 1
Package server;

Import Java.rmi.Remote;
Import java.rmi.RemoteException;

Public interface Serverinterface extends Remote ... {

Public String Ping (string fileName) throws
RemoteException;

As with any RMI program, Serverinterface must meet the following requirements:
1, must inherit remote interface.
2. For the client to be able to load a remote object that implements the Sqlremote interface, it must be declared public.
3. Each method in the interface (in this case, which contains only one method) must throw a java.rmi.RemoteException.

implement remote interface (implement the Interface)
The next step is to create a class that implements the Serverinterface. An example implementation is shown in code example 2. Note that in addition to implementing Serverinterface, the Serverimpl class also inherits Unicasremoteobject. This means that the Serverimpl class is used to create a single, not replicable, remote object that communicates using the RMI default TCP transport. This may be everything that you need to do whenever you use RMI.
Example Code 2
Package server;

Import java.io. * ;
Import Java.rmi. * ;
Import Java.rmi.server.UnicastRemoteObject;

public class Serverimpl extends UnicastRemoteObject
Implements Serverinterface ... {

private String name;

Public Serverimpl () throws RemoteException ... {
Super ();
}

public string Ping (string s) ... {
Return "Hello" + S;
}

}
Development servers (develop the server)
The third step is the development server. This class will be the portal to the server program because it contains the main () method. The server must complete the following three tasks:
1, create RMI Registry (RMI Registry).
2, to create a remote instance--in this case, Serverimpl.
3. Register to use the object created by RMI registry.
An example implementation is shown in code example 3.
code Example 3
Package server;

Import Java.rmi. * ;
Import Java.rmi.registry.LocateRegistry;

public class Servermain ... {

public static void Main (String argv[]) ... {

Try ... {

Locateregistry.createregistry (1099);

Serverinterface s = new Serverimpl ();
Naming.rebind ("//127.0.0.1/server", s);

catch (Exception e) ... {
System.out.println ("Server:" +e.getmessage ());
E.printstacktrace ();
}
}
The Naming.rebind ("//127.0.0.1/server", s) statement assumes that the RMI registry is running on the default port, which is the 1099 port defined in the Locateregistry.createregistry () statement in code example 3. However, if the RMI registry is running on a different port-for example, to change the registration location creation statement is locateregistry.createregistry (4500)-then you must change the binding statement to:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.