Scala 使用 RMI 简单列子

这个列子是youtube上的一个视频,如果能FQ,可以去youtube上去看下。

首先建立客户端:

trait RemoteChat extends Remote {
  @throws(classOf[RemoteException]) def recviceMessage(message: String): Unit
  @throws(classOf[RemoteException]) def newUser(name: String): Unit
}

定义一个scala的trait(特质,相当于java的接口),含有2个方法recvice与newUser

编写一个客户端实现类:

class RemoteChatImpl extends UnicastRemoteObject with RemoteChat {
  @throws(classOf[RemoteException]) def recviceMessage(message: String): Unit = {
    println(message)
  }
  @throws(classOf[RemoteException]) def newUser(name: String): Unit = {
    println(name + "has arrived.")
  }
}

在这里会提示你导入几个RMI的包:

import java.rmi._
import java.rmi.server.UnicastRemoteObject

创建客户端连接服务器代码:

object RMIChat {
  def main(args: Array[String]): Unit = {
    val server =
      Naming.lookup("rmi://localhost/RMIChatServer") match {
        case s: RemoteChatServer => s
        case other => throw new RuntimeException("Bound object is wrong type." + other)
      }
    val client = new RemoteChatImpl()
    server.login("rmiClient", client)
    val strin = new BufferedReader(new InputStreamReader(System.in))
    while (true) {
      val str = strin.readLine()
      server.sendMessage(str)
    }
  }
}

客户端完整代码:

import java.rmi._
import java.rmi.server.UnicastRemoteObject
import java.io.BufferedReader
import java.io.InputStreamReader

trait RemoteChat extends Remote {
  @throws(classOf[RemoteException]) def recviceMessage(message: String): Unit
  @throws(classOf[RemoteException]) def newUser(name: String): Unit
}

class RemoteChatImpl extends UnicastRemoteObject with RemoteChat {
  @throws(classOf[RemoteException]) def recviceMessage(message: String): Unit = {
    println(message)
  }
  @throws(classOf[RemoteException]) def newUser(name: String): Unit = {
    println(name + "has arrived.")
  }
}

object RMIChat {
  def main(args: Array[String]): Unit = {
    val server =
      Naming.lookup("rmi://10.2.54.184/RMIChatServer") match {
        case s: RemoteChatServer => s
        case other => throw new RuntimeException("Bound object is wrong type." + other)
      }
    val client = new RemoteChatImpl()
    server.login("rmiClient", client)
    val strin = new BufferedReader(new InputStreamReader(System.in))

    while (true) {
      val str = strin.readLine()
      server.sendMessage(str)
    }
  }
}

编写服务器代码的时候也只需要继承UnicastRemoteObject类与Remote接口就可以了

服务器完整代码如下代码:

import java.rmi._
import java.rmi.server.UnicastRemoteObject
import java.rmi.registry.LocateRegistry

trait RemoteChatServer extends Remote {
  @throws(classOf[RemoteException]) def login(name: String, client: RemoteChat): Array[RemoteChat]
  @throws(classOf[RemoteException]) def sendMessage(message: String): Unit
}
class RemoteChatServerImpl extends UnicastRemoteObject with RemoteChatServer {

  var clients = Set[RemoteChat]()
  @throws(classOf[RemoteException]) def login(name: String, client: RemoteChat): Array[RemoteChat] =
    {
      println(name + ":Hello Client!")
      clients += client
      null
    }
  @throws(classOf[RemoteException]) def sendMessage(message: String) {
    clients.foreach { x => x.recviceMessage(message) }
  }
}

object RMIChatServer {

  def main(args: Array[String]): Unit = {
    LocateRegistry.createRegistry(1099)
    val server = new RemoteChatServerImpl
    Naming.rebind("RMIChatServer", server)
    println("RmI Server Start!")
  }

}

整体来说是有起来还是很方便,作为通讯来说不太复杂。

你可能感兴趣的:(scala)