Monday, November 12, 2007

RMI with NetBeans 6 Beta 2

Please don't let mislead you because of this post's title. NetBeans doesn't have any special features for RMI support. I've only chosen this title because I'm going to develop my RMI-Service in NetBeans 6 Beta 2 which is my favourite IDE.

I won't explain every detail of RMI because you will find a lot of good tutorials on the web anyway. So, the first step is to open your IDE and develop the Interface for your RemoteService:



Second step: Write your service implementation class.





package at.ac.tuwien.ds.lab2.rmi.server;

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

/**
* Implementation of ICustomRemot Interface.
* @author Claus Polanka
*/
public class CustomRemoteImpl extends UnicastRemoteObject implements ICustomRemote {

/**
* Necessary because Super-Class constructor throws RemoteException.
* @throws java.rmi.RemoteException Will be thrown if there are some network
* errors.
*/
public CustomRemoteImpl() throws RemoteException {
}

/**
* Returns a string which say 'Hello World with NetBeans 6 Beta2 and RMI.'
* @return the string which holds the value.
* @throws java.rmi.RemoteException Will be thrown if there are some network
* problems.
*/
public String sayHello() throws RemoteException {
return "Hello World in NetBeans 6 Beta 2 with RMI.";
}
}


Next step ist to implement a class who starts your remote service:





package at.ac.tuwien.ds.lab2.rmi.server;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Starts the CustomRemote Service.
* @author Claus Polanka
*/
public class StartRemoteService {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
ICustomRemote service = new CustomRemoteImpl();
Naming.rebind("HelloWorldService", service);
} catch (RemoteException ex) {
Logger.getLogger(StartRemoteService.class.getName())
.log(Level.SEVERE, null, ex);
} catch (MalformedURLException ex) {
Logger.getLogger(StartRemoteService.class.getName())
.log(Level.SEVERE, null, ex);
}
}

}


Now, after you have finished the serverside of your RMI application, it's time to develop the client who calls the service.





package at.ac.tuwien.ds.lab2.rmi.client;

import at.ac.tuwien.ds.lab2.rmi.server.ICustomRemote;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Calls my RemoteService.
* @author Claus Polanka
*/
public class Client {

/**
* Makes a lookup and gets the String of the RemoteServcie.
*/
public void run() {
try {
ICustomRemote remoteService = (ICustomRemote)
Naming.lookup("rmi://127.0.0.1/HelloWorldService");

// Calls remote method.
String remoteString = remoteService.sayHello();

System.out.println(remoteString);

} catch (NotBoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (MalformedURLException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (RemoteException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Client client = new Client();
client.run();
}
}



Now, compile everything and you should see something like the following output:





So, if you have the same environment as I have (only one machine ;-) you don't have to copy anything to another location. Just generate the Service Stub-Class, start the rmiregistry and start your service. Then your client should be able to execute the Service and print out it's result which is in my case a simple string.

Generate Stub-Class:




Now, you should be able to see a new generated class in corresponding package where you have generated the Stub.



Start the RMI-Registry to register the Service.



Start your service and bind it to your registry.



Let's run the Client.



And see the result.



Hope you liked it.

Cheers

11 comments:

Glenn said...

Thanks! This also works on mac os x 10.5 with netbeans 6 ;) But there's one thing I don't understand, shouldn't the stub be on the client side?

Sageniuz said...

Hi Glenn, you're completely right. In the older JDK's you had to generate the 'Server STUB' for the client-side and the 'Server SKELETON' for the server-side. This isn't necessary in the newer JDK's (I'm not sure but I think JDK's > 1.4) anymore. Now you only have to generate the Stub to bind your implementation object (of your RemoteService) to your rmi-registry. I hope this is an answer to your question. Cheers

Glenn said...

Ah, yeah I see :) I also spoke with an classmate today, he confirmed that stubs don't need to be generated by hand anymore and noted me that since JDK 1.5 it is also possible to start the rmiregistry with java code:

int port = 1099;
LocateRegistry.createRegistry(port);


Thanks again for your explaination, this RMI "tutorial" really helped me :)

Suraj Chhetry said...

Nice Tutorial.

Suraj Chhetry said...

Hi,
I have implemented some methods of other interface which is in another.jar . Can you help me how to compile that CustomerServiceImpl. I have tried
rmic service.CustomerServiceImpl
but it unable to find CustomerService interface which is in another jar file.Please help me out.

Thanks
suraj

Anonymous said...

I do all you've done, I generate the stub, run the server but when I try to run the client in Netbeans I get this error:

Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at prueba.server.CustomRemoteImpl.SayHello(CustomRemoteImpl.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
at prueba.server.CustomRemoteImpl_Stub.SayHello(Unknown Source)
at prueba.client.Client.run(Client.java:30)
at prueba.client.Client.main(Client.java:48)

Anonymous said...

bikini lesbian fetish hard lesbian sex pictures index of images lesbians amateur lesbians lesbian beauties 3 asian beauties indian lesbian capital hot young japanese lesbians

Anonymous said...

Nackte Amateure

Cam Girls

Sexcam Amateur Fotzen

Anonymous said...

Hed been brought to his old suite of rooms and laid out on his bed. Hyles hand came up to brace on his shoulder, encouraging him to look up again. She shook her head. Rhaes markings stood out on his black skin, almost glowing in the amply lit arena. Let it go for now, he soothed. Rocks and debris filled them still, but she had done what shed come to do. Im eager to see this spell. Eyrhaen glanced over her shoulder to meet Radins triumphant smile. Her heart stopped at seeing him close after over a moon of avoiding him. Eyrhaen bobbed her head, loose hair falling forward over her shoulders. Let me go, you oaf. She couldnt help that her laugh was a tad nervous. I didnt say we werent mad. His body shook, his thrusts gone ragged. She screamed, unconsciously tearing at his hair. Behind her, Tykir nestled close, his cheek resting on the back of her shoulder. Polite isnt a description Ive often heard for you. That would have been wonderful. Tears spilled from the eyes she closed. Mmmmm, she purred, licking the side of his lips as well as her own fingers.

Anonymous said...

Brevin stared at the far wall, Tykir at the table, Lanthan at Brevins back. His hands came over her eyes, which wouldnt see then the black truly took over. Colors bled, and the stone trees melted into the floating water of the stream. Tykir and Brevin stepped aside, and Lanthan sank onto the couch with her. The stone beneath her clutching fingers started to crumble.
[url=http://cumonshot.1sthost.org/index11.html]free facial cum shots[/url]

Anonymous said...

vegas strip club couples