2

我目前正在开发一个在 Apache Tomcat 7 上运行的 Java Web 应用程序。由于我想将一些信息记录到数据库中,我使用以下配置文件信息来启动我的 Logger:

log4j.rootLogger = DEBUG, DB
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DB.URL=jdbc:mysql://localhost:3306/cap_recommender_log
log4j.appender.DB.driver=com.mysql.jdbc.Driver
log4j.appender.DB.user=log_user
log4j.appender.DB.password=some_password
log4j.appender.DB.sql=INSERT INTO notify_service_log(date, logger, level, message) VALUES('%d{YYYY-MM-dd}','%C','%p','%m')
log4j.appender.DB.layout=org.apache.log4j.PatternLayout

另外,notify_service_log 表如下:

CREATE TABLE `notify_service_log` ( 
    `date` date NOT NULL,
    `logger` varchar(256) NOT NULL,
    `level` varchar(10) NOT NULL,
    `message` text NOT NULL,
    KEY `index_level` (`level`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1

最后,web服务代码如下:

package com.imis.cap.service.notify;

import com.imis.cap.module.etl.EtlModuleClient;
import gr.aia.cap.eventbroker.v1.ArrayOfServiceMessage;
import gr.aia.cap.eventbroker.v1.BooleanResponse;
import gr.aia.cap.eventbroker.v1.ServiceMessage;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jws.WebService;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

@WebService(serviceName = "NotifyService", portName = "HttpBinding_NotifyService", endpointInterface = "gr.aia.cap.eventbroker.v1.NotifyService", targetNamespace = "http://www.aia.gr/cap/eventbroker/v1/", wsdlLocation = "WEB-INF/wsdl/NotifyService/NotifyService.wsdl")
public class NotifyService {

    private String username;

    private String password;

    private String log_properties_file;

    private DataSource registration_db;

    private static org.apache.log4j.Logger notifyServiceLogger = 
        Logger.getLogger(NotifyService.class);

    public NotifyService() {
        Context context;
        try {
            context = (Context) new InitialContext().lookup("java:comp/env");
            this.username = (String) context.lookup("CAP_RECOMMENDER_UNAME");
            this.password = (String) context.lookup("CAP_RECOMMENDER_PASS");
            this.registration_db = (DataSource) context.lookup("jdbc/cap_registration_db");
            this.log_properties_file = (String) context.lookup("NOTIFY_SERVICE_LOG_PROPERTIES_FILE");
        }catch(NamingException e) {
            System.err.println(e.getMessage());
            notifyServiceLogger.error("NotifyService: NamingException occured during construction. Message: " +
                e.getMessage());
        }
        PropertyConfigurator.configure(this.log_properties_file);
        notifyServiceLogger.info("NotifyService: Object constructor completed successfully.");
   }

   public gr.aia.cap.eventbroker.v1.BooleanResponse notify(gr.aia.cap.eventbroker.v1.NotifyRequest request) throws ParseException {
    ArrayOfServiceMessage arrayOfServiceMsg = new ArrayOfServiceMessage();
    ServiceMessage msg = new ServiceMessage();
    BooleanResponse response = new BooleanResponse();
    EventTypeReader eventType = new EventTypeReader(request);
    String regCode = request.getRegistrationCode();
    String dbRegCode = "";
        **notifyServiceLogger.info("NotifyService.notify(): Called with reg-code: " + request.getRegistrationCode() + ".");**

    /**Some more code**/
    return new BooleanResponse(); //Sample response
    }
}

执行通知过程调用的客户端代码如下:

package notifyclient;

import gr.aia.cap.eventbroker.v1.ArrayOfAttribute;
import gr.aia.cap.eventbroker.v1.BooleanResponse;
import gr.aia.cap.eventbroker.v1.EventType;
import gr.aia.cap.eventbroker.v1.NotifyRequest;

public class NotifyClient {

    public static void main(String[] args) {
        NotifyRequest request = new NotifyRequest();
        request.setRegistrationCode("blasdadasd");
        request.setAttributes(new ArrayOfAttribute());
        request.setEventType(EventType.USER);
        BooleanResponse response = notify(request);
        System.out.println("Output: " + response.getErrors().getServiceMessage().toString());
    }

    public static gr.aia.cap.eventbroker.v1.BooleanResponse notify(gr.aia.cap.eventbroker.v1.NotifyRequest request) {
        gr.aia.cap.eventbroker.v1.NotifyService_Service service = new gr.aia.cap.eventbroker.v1.NotifyService_Service();
        gr.aia.cap.eventbroker.v1.NotifyService port = service.getHttpBindingNotifyService();
        return port.notify(request);
    }
}

我必须在这一点上通知您,执行 Web 服务调用所需的类是由 Netbeans 7.2 自动生成的。

问题在于构造函数的消息被记录到数据库中,但通知函数中的信息消息从未被记录。为什么会这样?有任何想法吗?

4

1 回答 1

1

如评论PropertyConfigurator.configure(this.log_properties_file);所述,在方法中记录器调用上方的行中添加notify()解决了该问题。

于 2012-12-20T12:59:40.920 回答