gpt4 book ai didi

java - RMI 程序中缺少日志

转载 作者:太空宇宙 更新时间:2023-11-04 08:07:52 24 4
gpt4 key购买 nike

我设法编写了一个简单的程序,该程序应该模拟由三个组件组成的系统中的传递控制。但我不确定我是否做得正确,因为日志看起来不像我预期的那样。

我找不到错误。是在aspectJ部分还是在RMI部分?

RMI服务器

public class RMIServer {

public static void main(String[] args) throws RemoteException,
MalformedURLException {
LocateRegistry.createRegistry(1099);
HelloIF hello = new Hello();
Naming.rebind("server.Hello", hello);
System.out.println("Server is ready.");
}
}

RMI服务器2

public class RMIServer2 {

public static void main(String[] args) throws RemoteException,
MalformedURLException, NotBoundException, InterruptedException {

//LocateRegistry.createRegistry(1098);
LocateRegistry.getRegistry(1099);
ByeIF bye = new Bye();
Naming.rebind("server.Bye", bye);
System.out.println("Server-Client is ready.");

}

RMI客户端

public class RMIClient {
public static void main(String[] args) throws RemoteException,
MalformedURLException, NotBoundException, InterruptedException {

Registry registry = LocateRegistry.getRegistry("localhost");
ByeIF bye = (ByeIF) registry.lookup("server.Bye");
System.out.println(bye.farewell(Thread.currentThread().getName()));
}
}

你好

public class Hello extends UnicastRemoteObject implements HelloIF {

public Hello() throws RemoteException {
}

public String greeting(String c) throws RemoteException,
InterruptedException {
return "Good morning!";
}
}

再见

public class Bye extends UnicastRemoteObject implements ByeIF {

public Bye() throws RemoteException {
}

public String farewell(String c) throws RemoteException,
InterruptedException, NotBoundException {
Registry registry = LocateRegistry.getRegistry("localhost");
HelloIF hello = (HelloIF) registry.lookup("server.Hello");
System.out.println(hello.greeting(Thread.currentThread().getName()));
return "Bye bye!";
}

}

日志看起来:

[2012-08-03 11:06:18,582] [request1343984778582] [public static void hello.RMIClient.main(java.lang.String[])]
[2012-08-03 11:06:18,738] [request1343984778582] [public java.lang.String hello.Bye.farewell(java.lang.String)]
[2012-08-03 11:06:18,785] [request1343984778582] [public java.lang.String hello.Hello.greeting(java.lang.String)]
[2012-08-03 11:06:18,785] [request1343984778582] [public java.lang.String hello.Hello.greeting(java.lang.String)]
[2012-08-03 11:06:18,847] [request1343984778582] [public static void hello.RMIClient.main(java.lang.String[])]

因此缺少一个日志 - Bye.farewell()

我不知道为什么aspectJ看不到方法何时结束。

@Aspect
public class ReportingAspect {

// --------------------------------------LOGGER

static final Logger logger = Logger.getLogger(ReportingAspect.class);

// --------------------------------------POINTCUTS

@Pointcut("execution(public static void hello.RMIClient.main(String[]))")
public void requestStart() {
}

@Pointcut("(execution(String greeting(..)) && args(context)) || "
+ "(execution(String farewell(..)) && args(context))")
public void RMImethodStart(String context) {
}

@Pointcut("execution(String greeting(..)) || "
+ "execution(String farewall(..)) || "
+ "execution(public static void hello.RMIClient.main(String[]))")
public void general() {
}

@Pointcut("execution(public static void hello.RMIServer.main(String[])) || "
+ "execution(public static void hello.RMIServer2.main(String[]))")
public void setLoggingFile() {
}

// --------------------------------------ADVICES

@Before("requestStart()")
public void setLoggerAndThreadName(JoinPoint joinPoint) {
PropertyConfigurator.configure("log4j.properties");
Thread.currentThread().setName("request" + System.currentTimeMillis());
report(joinPoint);
}

@Before("RMImethodStart(context)")
public void setThreadName(JoinPoint joinPoint, String context) {
Thread.currentThread().setName(context);
report(joinPoint);
}

@Before("setLoggingFile()")
public void setProperties() {
PropertyConfigurator.configure("log4j.properties");
}

@After("general()")
public void generateReport(JoinPoint joinPoint) {
report(joinPoint);
}

/*//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@After("execution(public String hello.Bye.farewell(String))")
public void test(JoinPoint joinPoint)
{
report(joinPoint);
}*/

// ---------------------------------------REPORT
void report(JoinPoint jp) {
logger.error(jp.getSignature().toLongString());
}
}

最佳答案

如果您发现一个您不理解的错误,请尝试减少问题。就您而言,在实际代码中使用一些旧的 System.out.println() 看看到底发生了什么怎么样?

此外,我没有看到任何异常处理。根据您的 Java 版本,main() 中的异常可能会被吞掉。

关于java - RMI 程序中缺少日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11777062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com