Pages

Install CouchDB 1.3 in Ubuntu 12.04 64bit

Here are the steps to install couchDB 1.3 in Ubunutu 12.04 64bit
Step 1
 execute following command set to install required dependencies


 sudo apt-get install g++  
 sudo apt-get install erlang-base erlang-dev erlang-eunit erlang-nox  
 sudo apt-get install libmozjs185-dev libicu-dev libcurl4-gnutls-dev libtool  

Step 2
Then go to couchDB site and download source.

Step 3
Extract it and execute following commands.

 ./configure  
 make  
 sudo make install  

Step 4
To start the couchDB service run the following command.

 sudo couchdb  

Producer Consumer Problem with Blocking Queue

Producer Consumer problem is a popular problem domain in SE industry. Its hard find anyone who doesn't have heard it.
So in this post I will solve this producer consumer problem by using Blocking Queue in java.

Before that I will give a quick introduction of Blocking Queue.

Blocking Queue is an interface locate in java concurrent package.It mainly support operations that wait for the queue to become non empty when retrieving and removing element and wait for space become available when adding an element. All the blocking queue implementation are thread-safe and methods are atomic.

In this demo I will use ArrayBlockingQueue as the implementation of BlockQueue.

So first we create domain model for this demo.
Message.java

 package rd.domain;  
 public class Message {  
   private String description;  
   public String getDescription() {  
     return description;  
   }  
   public void setDescription(String description) {  
     this.description = description;  
   }  
 }  

then we create Producer.java
create 100 message and finish message and add in to the queue.

 package rd.concurent;  
 import rd.domain.Message;  
 import java.util.concurrent.BlockingQueue;  
 public class Producer implements Runnable {  
   private BlockingQueue<Message> queue;  
   public Producer(BlockingQueue<Message> queue) {  
     this.queue = queue;  
   }  
   @Override  
   public void run() {  
     // create messages and adding to queue  
     for (int i = 1; i <= 100; i++) {  
       Message message = new Message();  
       message.setDescription(" Message " + i);  
       try {  
         Thread.sleep(i);  
         queue.put(message);  
         System.out.println("Produced " + message.getDescription());  
       } catch (InterruptedException e) {  
         e.printStackTrace();  
       }  
     }  
     // adding exit message  
     Message message = new Message();  
     message.setDescription("finish");  
     try {  
       queue.put(message);  
     } catch (InterruptedException e) {  
       e.printStackTrace();  
     }  
   }  
 }  

Consumer.java


 package rd.concurent;  
 import rd.domain.Message;  
 import java.util.concurrent.BlockingQueue;  
 public class Consumer implements Runnable {  
   private BlockingQueue<Message> queue;  
   public Consumer(BlockingQueue<Message> queue) {  
     this.queue = queue;  
   }  
   @Override  
   public void run() {  
     Message message = null;  
     try {  
       while (!(message = queue.take()).getDescription().endsWith("finish")){  
         Thread.sleep(10);  
         System.out.println("Consumed " + message.getDescription());  
       }  
     } catch (InterruptedException e) {  
       e.printStackTrace();  
     }  
   }  
 }  

So the basic implementation are done. Now its time to test it. Here I create Main.java and create producer consumer thread and start those threads.

Main.java


 package rd;  
 import rd.concurent.Consumer;  
 import rd.concurent.Producer;  
 import rd.domain.Message;  
 import java.util.concurrent.ArrayBlockingQueue;  
 import java.util.concurrent.BlockingQueue;  
 public class Main {  
   public static void main(String[] args) {  
     BlockingQueue<Message> blockingQueue = new ArrayBlockingQueue<>(10);  
     Producer producer = new Producer(blockingQueue);  
     Consumer consumer = new Consumer(blockingQueue);  
     new Thread(consumer).start();  
     new Thread(producer).start();  
     System.out.println("Started.................");  
   }  
 }  

So that's it. We just solved the producer consumer problem.

Resolve “java.lang.OutOfMemoryError: PermGen space” error in Maven

If your project have more than 10 modules and your building tool is maven you would face this exception that comes into your console.

 java.lang.OutOfMemoryError: PermGen space  
     at java.lang.ClassLoader.defineClass1(Native Method)  
     at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)  
     at java.lang.ClassLoader.defineClass(ClassLoader.java:616)  

to avoid this just execute the following command in console.

 set MAVEN_OPTS=-XX:MaxPermSize=512m  

But sometime you have to provide the double quotes as well.

 set MAVEN_OPTS="-XX:MaxPermSize=512m"