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

    Default 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
    impacted is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    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

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

    Default

    Solved in the other post!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

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

    Default

    Shall I request it be moved to another forum instead? My apologies, maybe was a bit impatient after working on this for a couple days and getting nothing :p

Similar Threads

  1. Replies: 2
    Last Post: 05-09-2011, 03:22 PM
  2. 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
  3. Queue
    By DCY in forum New To Java
    Replies: 6
    Last Post: 05-04-2010, 09:07 PM
  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
  •