どのスレッドが特定のメソッドを実行するかを理解するのにいくつかの困難があります。サーバー側に方法はありますか?私は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();
}
}
}
こんにちは:
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..." );
}
}
私はRMIを使用していますか?
AspectJクラスを追加しました
@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;
}
}
すべてのログはクライアントスレッドから取得されます。魔女のスレッドが私のメソッドを実行することを説明してもらえますか?