2012-12-19 50 views
2

我目前正在研究运行在Apache Tomcat 7上的Java Web应用程序。由于我想将某些信息记录到数据库中,因此我使用以下配置文件信息来启动我的记录仪:在java web服务上记录事件

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提供。

问题在于构造函数的消息已登录到数据库中,但通知函数中的信息消息从不记录。这是为什么发生?有任何想法吗?

+0

被通知过吗?还是这个类只是构造? – Lipongo

+0

是的。为了执行对Web服务的请求,我开发了一个测试客户端。构造函数日志消息被插入到数据库中,但通知调用不是。 –

+0

你可以发布客户端代码,这样我们就可以看到它被调用,如果问题出现在客户端而不是此代码中? – Lipongo

回答

1

由于意见状态,在notify()方法的记录器调用上方添加PropertyConfigurator.configure(this.log_properties_file);解决了该问题。