RMI Program Execution - java

============Server Interface:Server.java================
import java.rmi.*;
public interface Server extends Remote
{
void register (Client cl) throws RemoteException;
void broadcastCord () throws RemoteException;
}
=============Server Class:ServerImpl.java===============
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
import java.net.*;
public class ServerImpl extends UnicastRemoteObject implements Server
{
public ServerImpl() throws RemoteException
{
}
public synchronized void register (Client cl) throws RemoteException
{
System.out.println("===>register Method of Process:");
cl.receiveInq();
}
//=========Broadcast Inquiry==================
public synchronized void broadcastCord () throws RemoteException
{
System.out.println("===>broadcastCord Method of Process:");
}
//==============Main Function===================
public static void main (String[] args)
{
try
{
System.out.println(InetAddress.getLocalHost().toString());
Naming.rebind("Server", new ServerImpl());
}
catch(Exception e)
{
System.err.println("Problem..."+e) ;
}
}
}
============Client InterfaceClient.java:=============
import java.rmi.*;
public interface Client extends Remote
{
void receiveInq() throws RemoteException;
void receiveGrt() throws RemoteException;
}
==============Client Class:ClientImpl.java==============
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
public class ClientImpl extends java.rmi.server.UnicastRemoteObject implements Client
{
private Server mycs;
public ClientImpl(Server cs) throws RemoteException
{
mycs=cs;
mycs.register(this);
}
//=====Greater List========
public synchronized void receiveGrt() throws RemoteException
{
System.out.println("===>recieveGRT Method of Process:");
}
//=====RECIEVE INQUIRY========
public synchronized void receiveInq () throws RemoteException
{
System.out.println("===>receiveInq Method of Process:");
mycs.broadcastCord();
}
public static void main(String args[])
{
String url = "rmi://localhost:1099/Server";
try{
Server cs= (Server) Naming.lookup(url);
new ClientImpl(cs);
}
catch(Exception e)
{
System.err.println("Problem..\n"+e) ;
}
}
}
it does not executes completely.......If we close the client program the execution
completes....but i don't know why its not executing properly....it does not throw any
exception

Remove 'synchronized'. You are deadlocked. RMI callbacks execute on a different thread from the original call.

Related

socket - Emitter.Listener(){} must override a superclass method

I am having trouble with socket.io java.
I am using "Compiler Compliance Level 11" in ubuntu 20.04
This is my code:
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import java.net.URISyntaxException;
public class main {
protected Socket getSocket(){
Socket mSocket;
try {
mSocket = IO.socket(".....uri.....");
mSocket.on("hello", new Emitter.Listener() {
#Override
public void call(Object... args) {
System.out.println(args[0]); // world
}
});
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
return mSocket;
}
public static void main(String[] args) {
}
}
And this is error:
The method call(Object...) of type new Emitter.Listener(){} must override a superclass method

Java RMI has different instantiations on client variables from client itself and from remote calls from server

We are trying to create a system using Javas RMI. The problem is that a maintained list on the client cannot be accessed from the server using Java RMI. It seems that the RMI connection is handling a copy of the initialized list.
Below is a minimal example using an integer that the client increments every second until it equals 10. The server receives 0 all the time though.
Anyone have any idea what we are doing wrong?
Just run server and the client as a java application.
ServerDefaultImpl.java
package rmi;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class ServerDefaultImpl implements EIServerRemote, Runnable {
ClientRemote client;
private boolean running = true;
public ServerDefaultImpl() {
try {
LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
ServerDefaultImpl server = this;
EIServerRemote stub = (EIServerRemote) UnicastRemoteObject.exportObject(server, 0);
Registry registry = LocateRegistry.getRegistry();
registry.rebind("test", stub);
} catch (RemoteException e) {
e.printStackTrace();
}
new Thread(this).start();
}
public static void main(String[] args) {
new ServerDefaultImpl();
}
#Override
public void run() {
while (true == running) {
try {
Thread.sleep(1000);
if (null != client) { //Client not connected yet.
int test = client.test();
System.out.println(test);
running = test <= 10;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
public void attachClientListener(ClientRemote client) throws RemoteException {
this.client = client;
}
}
EIServerRemote.java
package rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface EIServerRemote extends Remote {
void attachClientListener(ClientRemote client) throws RemoteException;
}
ClientRemote.java
package rmi;
import java.io.Serializable;
import java.rmi.Remote;
public interface ClientRemote extends Remote,Serializable {
int test();
}
ClientDefaultImpl.java
package rmi;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class ClientDefaultImpl implements Runnable,
ClientRemote {
private static final long serialVersionUID = 4846141863099303590L;
protected EIServerRemote server = null;
public int test;
public boolean running = true;
public ClientDefaultImpl(String serverName) {
test = 0;
try {
connect(serverName);
} catch (RemoteException | NotBoundException e) {
e.printStackTrace();
}
new Thread(this).start();
}
public static void main(String[] args) {
new ClientDefaultImpl("test");
}
public void connect(String serverName) throws RemoteException,
NotBoundException {
Registry registry = LocateRegistry.getRegistry();
EIServerRemote s = (EIServerRemote) registry.lookup(serverName);
server = s;
s.attachClientListener((ClientRemote) this);
}
#Override
public void run() {
while (true == running) {
try {
Thread.sleep(1000);
System.out.println(test++);
running = test <= 10;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
public int test() {
return test;
}
}
It seems that the RMI connection is handling a copy of the initialized list.
That's correct. The list isn't a remote object, so it is passed and returned via serialization.

RMI Exception RemoteException nested exception

Trying to run RMI example but facing the following exception:
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is:
java.lang.ClassNotFoundException: hejsan.RemoteBuffer
I have started the rmiregristry and it seems to be running fine. But the server is not running, it just builds and stops.
/Library/Java/JavaVirtualMachines/jdk1.7_45.jdk/Contents/Home/jre/bin/rmiregistry
Project name: hej
Package name: hejsan
Files: MyBuffer.java and RemoteBuffer.java
MyBuffer.java:
package hejsan;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.LinkedList;
#SuppressWarnings("serial")
public class MyBuffer extends UnicastRemoteObject implements RemoteBuffer {
LinkedList<Integer> list = new LinkedList<Integer>();
public MyBuffer() throws RemoteException, MalformedURLException {
super();
Naming.rebind("rmi://localhost/buffer", this);
}
public synchronized void put(Integer i) throws RemoteException {
list.addLast(i);
notifyAll();
}
public synchronized Integer get() throws RemoteException {
while (list.size() == 0) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return (Integer) list.removeFirst();
}
public static void main(String[] args) {
try {
new MyBuffer();
} catch (RemoteException re) {
System.out.println(re);
System.exit(1);
} catch (MalformedURLException me) {
System.out.println(me);
System.exit(1);
}
}
}
RemoteBuffer.java:
package hejsan;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface RemoteBuffer extends Remote {
void put(Integer i) throws RemoteException;
Integer get() throws RemoteException;
}
I discovered that I had to be on netbeans project path: ~/build/classes and runs the rmiregistry from while on that path.
The rmiregistry path can be found under Tools-> Java Platforms in netbeans. Caution! Change to jre instead of jdk following this below example:
/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre/bin/rmiregistry

the UnsupportedOperationException in RMI

I worked a simple program
But when you run the client at the command
This error appears
HelloClient exception: java.lang.UnsupportedOperationException: Not supported yet.
this my coded
Interface class
import java.rmi.*;
public interface HelloInterface extends Remote {
public String say() throws RemoteException;
}
implement class
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/**
*
* #author x
*/
public class HelloServerImpl extends UnicastRemoteObject implements HelloInterface {
private String message;
public HelloServerImpl(String msg)throws RemoteException{
message = msg;
}
#Override
public String say() throws RemoteException {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Server class
import java.rmi.Naming;
/**
*
* #author x
*/
public class HelloServer {
public static void main (String []args ){
try {
Naming.rebind("HELLOSERVER", new HelloServerImpl("Hello word"));
System.out.println("Hello Server is ready.");
} catch (Exception ex) {
System.out.println("Hello server failed: "+ ex);
}
}
}
Client class
import java.rmi.Naming;
/**
*
* #author x
*/
public class HelloClient {
public static void main(String[]args){
HelloInterface hello;
String url = "rmi://localhost/HELLOSERVER";
try {
hello = (HelloInterface)Naming.lookup(url);
System.out.println(hello.say());
} catch (Exception ex) {
System.err.println("HelloClient exception: " + ex);
}
}
}
I am prepared to write the steps but still the same error
why??
Well you wrote this yourself:
#Override
public String say() throws RemoteException {
throw new UnsupportedOperationException("Not supported yet.");
}
Of course it throws an exception. Try to actually return a string:
#Override
public String say() throws RemoteException {
return "hello";
}

RMI ClassNotFoundException

I've been building an RMI application over the past week and I've hit a roadblock that no amount of googling can seem to help with.
The following code is used to send an object from the server to the client via RMI:
Server code:
import rocks.Rock;
import rocks.squareRock;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server extends UnicastRemoteObject
implements RemInterface {
public Server() throws RemoteException {
super();
}
public static void main(String argv[]) {
try {
Server serv = new Server();
Naming.rebind("RockServer", serv);
} catch (Throwable t) {
t.printStackTrace();
}
}
public Rock getRock() {
return new squareRock();
}
}
Client code:
import rocks.Rock;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class Client {
RemInterface reminterface = null;
public Client() {
String strName = "rmi://127.0.0.1/RockServer";
try {
reminterface = (RemInterface) Naming.lookup(strName);
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public Rock loadRock() {
try {
return reminterface.getRock();
} catch (Throwable t) {
return null;
}
}
}
Interface:
public interface RemInterface {
public Rock getRock() throws RemoteException;
}
In this situation:
The "Rock" class is available in both the Client and Server classpath.
The "Rock" class implements serializable.
The "squareRock" extends class rock and is only available in the server's classpath.
The error I get when trying to call a method using a Rock from loadRock() on the client is as follows:
STDERR: java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: SquareRock
Any help would be appreciated.
You are returning an object of Type rocks.squareRock from the Server. During the de-serialization process at the client, this class will be required in order to create an instance of this class to represent the response from the server. As you've already indicated that the class is available only in the server's classpath, the failure to locate and load the said class causes the exception.
The resolution will be to make the rocks.squareRock class available in the client as well.

Categories

Resources