Results 1 to 5 of 5
- 04-27-2011, 05:28 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
Single thread to handle queue based logging
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: Writing into a File in a Multi Threaded app., using BlockingQueue. Here is the thread subclass which handles the queue and writing the items to disk:
In my main thread, I wrote: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")); } }
And when I wanted to start the logging thread, in main() method:Java Code:public class searchserver implements Runnable { static server_log log;
I added a few lines to print output to console for debugging purposes, and this is the output I'm receiving:Java Code:log = new server_log();
I'm guessing it is something elementary, but I have read up on multithreading from numerous resources, from books to the Oracle.com site, and spent some time brushing up on the file i/o operations as well, and cannot seem to figure out what is causing nothing to be written to the log file. Any help would be greatly appreciated!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-27-2011, 10:39 PM #2
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
Due to lack of any responses I am re-posting this in the Threads and Synch forum. Here is a link to this post on that forum: Single thread to handle queue based logging
- 04-28-2011, 03:25 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
Solved in the other post!
-
- 04-28-2011, 10:25 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Moved Hijacked Thread: web based gps tracking assignment
By surya86mca in forum New To JavaReplies: 2Last Post: 05-09-2011, 02:22 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, 10:07 AM -
Queue
By DCY in forum New To JavaReplies: 6Last Post: 05-04-2010, 08:07 PM -
Entity - Field-Based Access Vs Property Based Access
By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)Replies: 3Last Post: 11-02-2009, 07:18 PM -
difference between code based security and role based security
By boy22 in forum New To JavaReplies: 1Last Post: 07-23-2007, 11:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks