I have some difficulties to understand which thread execute particular method. Are there any methods on server side? I am new to RMI.
HelloClient:
public class HelloClient
{
Random rand = new Random();
public static void main(String[] args)
{
Thread.currentThread().setName("Thread of a client");
if(System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
}
HelloClient hc = new HelloClient();
hc.methodToMeasureEnglish();
}
void methodToMeasureEnglish()
{
try
{
Thread.sleep(Math.abs(rand.nextInt()%4000));
Registry reg = LocateRegistry.getRegistry("localhost", 6666);
HelloIF hello = (HelloIF) reg.lookup("HELLO");
System.out.println(hello.sayHelloEnglish());
}
catch( Exception e )
{
e.printStackTrace();
}
}
}
Hello:
public class Hello extends UnicastRemoteObject implements HelloIF
{
public Hello(String name) throws RemoteException
{
try
{
Registry registry = LocateRegistry.createRegistry(6666);
registry.rebind(name, this);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public String sayHelloEnglish()
{
return "GOOD MORNING";
}
}
HelloIF
public interface HelloIF extends Remote
{
public String sayHelloEnglish() throws RemoteException;
}
HelloServer
public class HelloServer
{
public static void main(String[] args) throws Exception
{
Thread.currentThread().setName("Server Thread");
if(System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
}
Hello myObject = new Hello("HELLO");
System.out.println( "Server is ready..." );
}
}
Am I using RMI right?
I added AspectJ class
@Aspect
public class MeasureAspect
{
private static Logger logger = Logger.getLogger(MeasureAspect.class);
@Around("call(void method*())")
public Object condition2(ProceedingJoinPoint joinPoint) throws Throwable
{
PropertyConfigurator.configure("log4j.properties");
Object res = joinPoint.proceed();
logger.info("Thread method " + Thread.currentThread().getName());
return res;
}
@Around("call(String say*())")
public Object condition1(ProceedingJoinPoint joinPoint) throws Throwable
{
PropertyConfigurator.configure("log4j.properties");
Object res = joinPoint.proceed();
logger.info("Thread say " + Thread.currentThread().getName());
return res;
}
}
All logs come from Client Thread. Could you explain me witch threads execute my methods?
The RMI Specification says you cannot make any assumptions about which thread remote methods execute on. Specifically this implies several things:
In practice, subject to thread pooling if any at the server (Oracle JVMs don’t do that but I believe IBM’s does), and/or collection pooling at the client (Oracle JVMs do that, don’t know about IBM), each method invocation is executed on its own thread.