Results 1 to 6 of 6
  1. #1
    impacted is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default 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: 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:

    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"));
    	}
    }
    In my main thread, I wrote:
    Java Code:
    public class searchserver implements Runnable {
    	static server_log log;
    And when I wanted to start the logging thread, in main() method:
    Java Code:
    log = new server_log();
    I added a few lines to print output to console for debugging purposes, and this is the output I'm receiving:
    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
    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!

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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.

  3. #3
    impacted is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    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..

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    You could also call flush(). Glad it's working for you.

  5. #5
    impacted is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    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

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    The docs say autoflush applies to println. You're using print.

Similar Threads

  1. Replies: 2
    Last Post: 05-09-2011, 03:22 PM
  2. Single thread to handle queue based logging
    By impacted in forum New To Java
    Replies: 4
    Last Post: 04-28-2011, 11:25 PM
  3. i need an example of JSR179 ((Location based Ser)implementation for CDC based device
    By talk_to_vivekmishra in forum CDC and Personal Profile
    Replies: 3
    Last Post: 12-30-2010, 11:07 AM
  4. Entity - Field-Based Access Vs Property Based Access
    By CatchSandeepVaid in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 11-02-2009, 08:18 PM
  5. Replies: 1
    Last Post: 07-24-2007, 12:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •