Java ResultSet in BlockingQueue

2015-01-28T22:45:49

I trying to write some blob from database to disk by passing an InputStream object to the queue. It creates the file with size 0. When I have tried write the InputStream to the disk without using a BlockingQueue, it worked without problem. Can you help me? My code is like below:

public class ExtractPicture implements Runnable{
    private BlockingQueue<InputStream> queue;
    public ExtractPicture(BlockingQueue<InputStream> queue){
        this.queue = queue;
    }    
    @Override
    public void run() {
        try(Connection conn = new DatabaseConnection().getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select mypicture from testpicture")){            
            while(rs.next()){
                queue.put(rs.getBinaryStream("mypicture"));
            }  
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
class Consumer implements Runnable{
    private BlockingQueue<InputStream> queue;
    public Consumer(BlockingQueue<InputStream> queue){
        this.queue = queue;
    }
    @Override
    public void run() {
        try{            
            int z = 0;
            InputStream is = null;
//            (is = queue.take()) != null
            while((is = queue.take()) != null){                
                System.out.println("consumer aa" + is.toString());
                writeToDisk(is, "c:\\temp\\p" + z + ".jpeg");
                z++;
            }
            is.close();
        }catch(Exception e){
            e.printStackTrace();
        } 
    }
   private void writeToDisk(InputStream is, String fna){
       OutputStream f = null;
        try {
            f = new FileOutputStream(new File(fna));
            int c = 0;
            while((c = is.read()) > -1){
                f.write(c);
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {

        } finally {
            try {
                f.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
   }
}
class RunService{
     public static void main(String[] args) {
        BlockingQueue<InputStream> queue = new ArrayBlockingQueue(10);
        ExtractPicture ep = new ExtractPicture(queue);
        Consumer c = new Consumer(queue);
        new Thread(ep).start();
        new Thread(c).start();
    }    
}

Copyright License:
Author:「coco」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/28195103/java-resultset-in-blockingqueue

About “Java ResultSet in BlockingQueue” questions

I trying to write some blob from database to disk by passing an InputStream object to the queue. It creates the file with size 0. When I have tried write the InputStream to the disk without using a
I am currently reading about Java BlockingQueue and this example is given on many websites for a simple implementation of the BlockingQueue. The code is simple but I am a bit confused. For example ...
Does anybody know why java's BlockingQueue does not have a putAll method? Is there a problem with such a method? Any good ways around this problem without having to completely re-implement Blocking...
In my Java Application I have BlockingQueue&lt;HashMap&lt;Integer, double[]&gt;&gt; q How do I clone it?
I know that concurrent adds to an stl queue in c++ can cause issues, and the way to solve this is adding a mutex lock around all add/remove calls. But I am programming in Java at the moment, and ...
This is the code snippt of my input spout for emmiting tuple to a processing noded for stream processing over a cluster. The problem is The BlockingQueue is throwing InterruptedException . private
I have a Java program that sent messages to an external Rest API. This API can handle a payload with a max size of 500KB only. My messages are stored in a BlockingQueue. I was wondering if there i...
I'd like to have one BlockingMap data structure which is very similar to BlockingQueue. The take method of BlockingQueue will wait there until element is available. I'd like the get method of Block...
I'd like to have one BlockingMap data structure which is very similar to BlockingQueue. The take method of BlockingQueue will wait there until element is available. I'd like the get method of Block...
I was trying to make a java program to copy a file with the reader in one thread and the writer in another thread. The program works without showing any errors but the copied file has a different

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.