JavaFX与后端服务器的交互(第一种方案)

本文中描述的内容在本文发表前未获得运行成功,其中原因复杂,比如本人下载的是JavaFX for Eclipse 3.2.2的plugin,但实际使用的是更高版本。但很多其他用户也同时遇到了这样的问题。据广大使用人群解释,可暂定为“JavaFX Complier”的BUG。但因JavaFX Script尚未正式release,所以我们相信在不久的将来这段代码就能够获得成功运行。

回归正文。JavaFX作为RIA的解决方案之一,最重要的功能之一就是要具备和服务器端的异步通信能力。如何获取与服务器端的通信能力呢?JavaFX有多种解决方案。我们今天在这里介绍的是其中的一种,使用RMI(远程方法调用)。

JavaFX Script与Java是进行了全面整合了的(不过现在compiler还有bug,要不然这个程序就能运行成功),所以使用JavaFX和Java的联合能够很方便地与服务器交互。

关于RMI的种种概念,在此就不多说了。先看服务器端的代码:

ServerInterface接口(Java代码):

package server;

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

public interface ServerInterface extends Remote {

    public String ping(String fileName) throws         RemoteException;

}

ServerImpl类(Java代码):

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;     }

}

 

ServerMain类(Java代码):

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();         }     } }

 

客户端代码如下:

ConnectionHelper类(Java代码):

package client;

import java.rmi.*; import server.ServerInterface;

public class ConnectionHelper {

    public static ServerInterface getConnection()         throws Exception     {         return (ServerInterface)             Naming.lookup("rmi://127.0.0.1:1099/Server");     }

}

 

MyClient.fx代码(JavaFX Script):

import java.lang.*; import javafx.ui.*; import java.rmi.*;

//接下来这两个import语句可能提示找不到Symbol,这就是我前面说的JavaFX Complier可能的bug。

import server.ServerInterface; import client.ConnectionHelper;

class ButtonClickModel {     attribute numClicks: Number; }

var model = new ButtonClickModel();

var win = Frame {     width: 200     content: GridPanel {         border: EmptyBorder {            top: 30            left: 30            bottom: 30            right: 30         }         rows: 3         columns: 1         vgap: 10         cells:           [  Button {                  text: "Click to make RMI connection!"                  mnemonic: I                  action: operation() {

                     try {

                         var remoteServer:ServerInterface =                              ConnectionHelper.getConnection();

                         var results = remoteServer.ping("Test");                          System.out.println("response: {results}");                          model.numClicks++;

                     } catch (e:Exception) {                          System.out.println("exception: {e}");                      }                  }              },

             Label {                  text: bind "Number of RMI connections: {model.numClicks}"

             }           ]     }     visible: true };

 

本文主要参考文档下载

你可能感兴趣的:(RIA技术)