您的位置:首页 > 运维架构

作为lead developer写给组员的一封邮件

2012-01-31 21:25 288 查看
I prepared this in case some of you not very understand RMI, so if you are very familiar with it, you can just ignore this email.

wishing Dear Prof.Kevin can help to point mistakes out.

Here's an sample code of RMI with 4 java class: interface, remote method going to be called, client and server including almost everything we need (well, except org.w3c and sql), nothing but few lines!

This is from a Chinese website.

I just translate the explanation from Chinese into English, so that all of you can read.^_^

Since we are database subsystem, most likely, other subsystem are our "clients" so, 1 and 2 may be the things that we need to implement:

1. Create remote interface(HelloInterface.java)

/*Everyone of us will need this!*/

java code:

package com.unmi;

import java.rmi.*;

/**
* this interface must extends java.rmi.Remote

*/
public interface HelloInterface extends Remote
{
/**
* and it must throws java.rmi.RemoteException

*/
public String say() throws RemoteException;
}

2.the class who actually implement remote method!

/*Everyone of us need this!! is't just the sub_database!*/

package com.unmi;

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

/**
* extends UnicastRemoteObject,implements HelloInterface

*/
public class Hello extends UnicastRemoteObject implements HelloInterface
{
private String message;

/**
* you must explicit constructor as you must notify it to throws RemoteException
*/
public Hello(String msg) throws RemoteException
{
message = msg;
}

/**
* Actually method we will made for others to use
* Here is the only hard part that we must consider how to handle sql and xml
but again I will be there to help! and don't you think it's so funny to learn those magic things~ haha

*/
public String say() throws RemoteException
{
System.out.println("Called by HelloClient");
return message;
}
}

If you want to know how to set up a RMI sever pls continue: nothing but few methods!

package com.unmi;

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

public class HelloServer
{
/**
* Start RMI and Register the remote method!
*/
public static void main(String[] argv)
{
try
{
//Start RMI registration,Point as 1099 (1099 is default value)

//you can also do it in this way: $java_home/bin/rmiregistry 1099
//however it will open another dos windowsand a stub may be needed.

//Thus the alternative way is not suggest.
LocateRegistry.createRegistry(1099);

//Create instance of remote object,following is an instance of Hello.java

//you can use different name of course.
HelloInterface hello = new Hello("Hello, world!");

//register Hello into the server and name it as hello

// to register this method into another computer! according to its url //address!

Naming.rebind("//192.168.1.105:1099/Hello",hello);

System.out.println("Hello Server is ready.");
}
catch (Exception e)
{
System.out.println("Hello Server failed: " + e);
}
}
}

simple?^_^

now let's take a look at what the client will do?

4. client find the remote object,and call its method(HelloClient)

java code

package com.unmi;

import java.rmi.Naming;

public class HelloClient
{
/**
*
*/
public static void main(String[] argv)
{
try
{
//
HelloInterface hello = (HelloInterface)Naming.lookup("//192.168.1.105:1099/Hello");

//remember? Hello.java has a method called say()
System.out.println(hello.say());
}
catch (Exception e)
{
System.out.println("HelloClient exception: " + e);
}
}
}

oh my god. Amazing!? that's all thing we are going to made, simple~?^_^

now again, I must emphasize to you all that, the technical part is very easy for this course, hard part is you must get connection with the corresponding subsystem closely! and do not forget your documentation!!

all technical problem, (RMI, MYSQL,DOM,SQL)I believe all of us can handle them easily!

I'm try to learn org.w3c.dom now, so next time I will provide an sample code of how to handle xml using DOM(one of the mechanism to handle xml in java).

again If anyone is also very interesting to learn those technical problem, please do so, and then share your experience to all of us.

okay, after read through this, If anyone wish to be the lead developer of RMI, please inform Prof Kevin and me.^_^

Thanks!

Best wishes!

Zhang Shu Hao (Mr.)

Nanyang Technological University

Computer Engineering- Year 2

HP: (+65) 98702513 or (+86) 13688017816

Email: shzhang1@e.ntu.edu.sg
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: