2012-12-19 77 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);解決了該問題。