4

当出现异常时,我将对象发送到服务器(大约 90 个),例如端点关闭,Logback 触发自定义 STMP 附加程序,它只发送 1 封电子邮件,最大。10 个日志,然后有一个延迟,其他日志被发送。是否有可能,这start()被调用得太频繁了?

问题:
scheduler.scheduleAtFixedRate(this::sendEmail, 1, 1L, TimeUnit.HOURS);
执行一次后不延迟,直接执行另一次,使得许多邮件在一个小时内发送出去。

我必须改变调度程序的延迟工作吗?

调试后,我注意到Attaching appender named [EMAIL] to Logger[ROOT]发生了两次可能是问题所在,如果是,我该如何解决?

洪水的图像(如您所见,两封电子邮件在一小时内发送...)
地板

Logback.xml 配置:

<configuration debug="true">

<!-- Logging per console and per email -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            <!-- sets the format of the output -->
            %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
        </Pattern>
    </layout>
</appender>

<appender name="EMAIL" class="com.konverto.phonebillasaj.appenders.ScheduledSMTPAppender">
    <subject>TESTING: %logger{20} - %m</subject>
    <layout class="ch.qos.logback.classic.html.HTMLLayout" />

    <smtpHost>smtp.xxx.net</smtpHost>
    <smtpPort>587</smtpPort>
    <STARTTLS>true</STARTTLS>
    <username>xxx@domain.net</username>
    <password>myPass</password>
    <to>yyy@domain.net</to>
    <from>xxx@domain.net</from>
    <maxMessages>10</maxMessages>

    <!-- for testing , comment in production, default 256 -->
    <cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
        <!-- Send just one log entry per email, ready for a lot of emails if you put one. -->
        <bufferSize>1</bufferSize>
    </cyclicBufferTracker>

    <!-- for testing , comment in production, default asynchronousSending = true -->
    <asynchronousSending>false</asynchronousSending>
</appender>


<logger name="com.konverto.phonebillasaj" level="error" additivity="false">
    <appender-ref ref="EMAIL"/>
    <appender-ref ref="CONSOLE" />
</logger>

<root level="error">
    <appender-ref ref="EMAIL" />
    <appender-ref ref="CONSOLE" />
</root>

附加程序代码:

public class ScheduledSMTPAppender extends SMTPAppender {

private final ThreadFactory tf = r -> {
    Thread t = new Thread(r, "ScheduledSMTPAppender Thread");
    t.setDaemon(true); //make daemon or it will prevent your program to exit
    return t;
};
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, tf);


private final List<ILoggingEvent> events = new ArrayList<>();

private int maxMessages = 10;

public ScheduledSMTPAppender() {
    super();
}

public ScheduledSMTPAppender(EventEvaluator<ILoggingEvent> eventEvaluator) {
    super(eventEvaluator);
}

@Override public void start() {
    super.start();
    scheduler.scheduleAtFixedRate(this::sendEmail, 1, 1L, TimeUnit.HOURS);
}

@Override protected void sendBuffer(CyclicBuffer<ILoggingEvent> cb, ILoggingEvent lastEventObject) {
    events.add(lastEventObject);

    if (events.size() > maxMessages)
    {
        sendEmail();
    }
}

//needs to be synchronized for thread safety
private synchronized void sendEmail() {
    try {
        if (events.isEmpty()){
            return;
        }
        ILoggingEvent lastEvent = events.get(events.size() - 1);
        events.remove(events.size() - 1);
        CyclicBuffer<ILoggingEvent> cb;

        if (events.isEmpty()) {
            cb = new CyclicBuffer<>(1);
        } else {
            cb = new CyclicBuffer<>(events.size());
            for (ILoggingEvent e : events){
                cb.add(e);
            }

        }
        super.sendBuffer(cb, lastEvent);
        events.clear();
    } catch (Exception e) {
        //Important to have a catch all here or the scheduled task will die
        addError("Error occurred while sending e-mail notification.", e);
    }
}

//this allows to make "maxMessages" a parameter of your appender
public int getMaxMessages() {
    return maxMessages;
}

public String getContentType() {
    return layout.getContentType();
}

public void setMaxMessages(int maxMessages) {
    this.maxMessages = maxMessages;

}
4

1 回答 1

3

这似乎是问题

@Override 
public void start() {
    super.start();
    scheduler.scheduleAtFixedRate(this::sendEmail, 1, 1L, TimeUnit.HOURS);
}

此调度程序将为每个已初始化的附加程序排队 sendEmail 任务。您可以将对象的实例或某个 id 与每个日志行一起记录,并且每次调用 start 吗?那应该澄清这种情况。

为了清楚起见 - 我使用 Sysout 而不是记录器(并在 main 方法中初始化)尝试了相同的代码,它按预期工作。这是代码

public class ScheduledSMTPAppender /*extends SMTPAppender*/ {

    private static final ThreadFactory tf = r -> {
        Thread t = new Thread(r, "ScheduledSMTPAppender Thread");
        t.setDaemon(true); //make daemon or it will prevent your program to exit
        return t;
    };
    private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, tf);


    private final List<String> events = new ArrayList<>();

    private int maxMessages = 10;

    public static void main(String args[]) {
        ScheduledSMTPAppender app = new ScheduledSMTPAppender();
        System.out.println("started");
        Thread t = new Thread(() -> {
            try {
                scheduler.awaitTermination(1, TimeUnit.HOURS);
            } catch (Exception e) {
            }
        });
        System.out.println("awaiting termination");
        t.start();
        System.out.println("thread initiated");
        new Thread(() -> {
            // Thread to add events to check logs
            for (int i =0;i<10;i++)
            {app.addEvent("asd");
            try {Thread.sleep(1000);
            } catch(Exception e) {};}
        }).start();
    }


    public ScheduledSMTPAppender() {
        super();
        System.out.println("starting");
        scheduler.scheduleAtFixedRate(this::sendEmail, 1, 1L, TimeUnit.SECONDS);
    }

    /*
    public ScheduledSMTPAppender(EventEvaluator<String> eventEvaluator) {
        super(eventEvaluator);
    }

    @Override
    public void start() {
        super.start();
        scheduler.scheduleAtFixedRate(this::sendEmail, 1, 1L, TimeUnit.HOURS);
    }*/

    //@Override
    protected void sendBuffer(List<String> cb, String lastEventObject) {
        //System.out.println("sending email");
        events.add(lastEventObject);

        if (events.size() > maxMessages) {
            sendEmail();
        }
        System.out.println(new Date() + "sent email" + lastEventObject);
    }

    //needs to be synchronized for thread safety
    private synchronized void sendEmail() {
        try {
            if (events.isEmpty()) {
                return;
            }
            String lastEvent = events.get(events.size() - 1);
            events.remove(events.size() - 1);
            List<String> cb;

            if (events.isEmpty()) {
                cb = new ArrayList<>(1);
            } else {
                cb = new ArrayList<>(events.size());
                for (String e : events) {
                    cb.add(e);
                }

            }
            sendBuffer(cb, lastEvent);
            events.clear();
        } catch (Exception e) {
            //Important to have a catch all here or the scheduled task will die
            //addError("Error occurred while sending e-mail notification.", e);
        }
    }

    //this allows to make "maxMessages" a parameter of your appender
    public int getMaxMessages() {
        return maxMessages;
    }

//    public String getContentType() {
//        return layout.getContentType();
//    }

    public void setMaxMessages(int maxMessages) {
        this.maxMessages = maxMessages;

    }

    public void addEvent(String s) {
        events.add(s);
    }
}
于 2019-07-29T10:15:32.027 回答