Results 1 to 6 of 6
- 04-27-2011, 11:39 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
Single thread to handle queue based logging
I originally posted this in the new to java forum due to the fact that I have been working with java only since my join date here, but have received no responses on this, so I am re-posting here. Here is the original post: http://www.java-forums.org/new-java/...tml#post202783
Usually I can figure problems I run into on my own with a little reading and experimenting, but this one has me stuck. All I am doing is creating a thread subclass that loops, pulling items off queue and writing to file. The problem I run into is that absolutely nothing is being written to disk. I came up with this design from a link in this thread: http://www.java-forums.org/threads-s...eaded-app.html, using BlockingQueue. Here is the thread subclass which handles the queue and writing the items to disk:
Java Code:public class server_log extends Thread { static final String newline = System.getProperty("line.seperator"); final private String LOG_FILENAME = "searchserver.log"; private BlockingQueue<String> log_queue = new ArrayBlockingQueue<String>(50); private PrintWriter log_file = null; public server_log() throws IOException { log_file = new PrintWriter(new FileWriter(LOG_FILENAME, true), true); start(); } public void add_log(String log_data) { try { log_queue.put(log_data); } catch (InterruptedException e) { System.out.format("Problem adding to log queue\n"); } } public void shutdown() throws IOException, InterruptedException { log_queue.put("SHUTDOWN"); log_file.close(); } // ***Thread - Pulls items off queue and writes to disk @Override public void run() { String log_data; Date present; DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, new Locale("en","US")); do { try { log_data = log_queue.take(); System.out.format("'" + log_data + "' taken from queue\n"); } catch (InterruptedException e) { log_data = "SHUTDOWN"; } if(log_data.equals("SHUTDOWN")) return; else { present = new Date(); log_file.print("[" + dateFormatter.format(present) + "]: " + log_data + newline); System.out.format("'" + log_data + "' written to log\n"); } } while(! log_data.equals("SHUTDOWN")); } }
Java Code:public class searchserver implements Runnable { static server_log log;
Java Code:log = new server_log();
Java Code:'Index file loaded successfully.' taken from queue 'Index file loaded successfully.' written to log 'Server bound on port: 31337' taken from queue 'Server bound on port: 31337' written to log 'Server started successfully!' taken from queue 'Server started successfully!' written to log
- 04-28-2011, 02:46 AM #2
I'm in a hurry, so forgive me if I'm missing something... but I'm not seeing where you ever actually call shutdown(). The data is never flushed and the file is never closed, and therefore it never gets written.
- 04-28-2011, 04:24 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
Alright, so it turns out that indeed you must use close() before you see anything written to disk. I wasn't doing this. It's a server so it just loops, and I was killing javaw.exe using the task manager so effectively never closed it. Thanks kjkrum you solved it for me! Marking as solved..
- 04-28-2011, 10:41 PM #4
You could also call flush(). Glad it's working for you.
- 04-28-2011, 11:26 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
I thought I would not have to do flush(), since:
public PrintWriter(Writer out, boolean autoFlush)
I turned on autoflush so I assumed I didn't need to. Perhaps it only autoflushes after close()?
Either way, thanks again man
- 04-28-2011, 11:28 PM #6
Similar Threads
-
Moved Hijacked Thread: web based gps tracking assignment
By surya86mca in forum New To JavaReplies: 2Last Post: 05-09-2011, 03:22 PM -
Single thread to handle queue based logging
By impacted in forum New To JavaReplies: 4Last Post: 04-28-2011, 11:25 PM -
i need an example of JSR179 ((Location based Ser)implementation for CDC based device
By talk_to_vivekmishra in forum CDC and Personal ProfileReplies: 3Last Post: 12-30-2010, 11:07 AM -
Entity - Field-Based Access Vs Property Based Access
By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)Replies: 3Last Post: 11-02-2009, 08:18 PM -
difference between code based security and role based security
By boy22 in forum New To JavaReplies: 1Last Post: 07-24-2007, 12:59 AM
Bookmarks