1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.server;
20
21 import java.io.IOException;
22 import java.util.concurrent.BlockingQueue;
23 import java.util.concurrent.TimeUnit;
24
25 import org.eclipse.jetty.util.BlockingArrayQueue;
26 import org.eclipse.jetty.util.log.Log;
27 import org.eclipse.jetty.util.log.Logger;
28
29
30
31
32
33
34 public class AsyncNCSARequestLog extends NCSARequestLog
35 {
36 private static final Logger LOG = Log.getLogger(AsyncNCSARequestLog.class);
37 private final BlockingQueue<String> _queue;
38 private transient WriterThread _thread;
39 private boolean _warnedFull;
40
41 public AsyncNCSARequestLog()
42 {
43 this(null,null);
44 }
45
46 public AsyncNCSARequestLog(BlockingQueue<String> queue)
47 {
48 this(null,queue);
49 }
50
51 public AsyncNCSARequestLog(String filename)
52 {
53 this(filename,null);
54 }
55
56 public AsyncNCSARequestLog(String filename,BlockingQueue<String> queue)
57 {
58 super(filename);
59 if (queue==null)
60 queue=new BlockingArrayQueue<>(1024);
61 _queue=queue;
62 }
63
64 private class WriterThread extends Thread
65 {
66 WriterThread()
67 {
68 setName("AsyncNCSARequestLog@"+Integer.toString(AsyncNCSARequestLog.this.hashCode(),16));
69 }
70
71 @Override
72 public void run()
73 {
74 while (isRunning())
75 {
76 try
77 {
78 String log = _queue.poll(10,TimeUnit.SECONDS);
79 if (log!=null)
80 AsyncNCSARequestLog.super.write(log);
81
82 while(!_queue.isEmpty())
83 {
84 log=_queue.poll();
85 if (log!=null)
86 AsyncNCSARequestLog.super.write(log);
87 }
88 }
89 catch (IOException e)
90 {
91 LOG.warn(e);
92 }
93 catch (InterruptedException e)
94 {
95 LOG.ignore(e);
96 }
97 }
98 }
99 }
100
101 @Override
102 protected synchronized void doStart() throws Exception
103 {
104 super.doStart();
105 _thread = new WriterThread();
106 _thread.start();
107 }
108
109 @Override
110 protected void doStop() throws Exception
111 {
112 _thread.interrupt();
113 _thread.join();
114 super.doStop();
115 _thread=null;
116 }
117
118 @Override
119 public void write(String log) throws IOException
120 {
121 if (!_queue.offer(log))
122 {
123 if (_warnedFull)
124 LOG.warn("Log Queue overflow");
125 _warnedFull=true;
126 }
127 }
128
129 }