How to Write Own Caching in Java | Improve your Java application Performance By Using Cache

Caching is very important when you want to improve your Java application performance
In My Project there was some performance issue during show huge date in screen. In that case
i have improve my java application performance by written own Cache Utility class, Which is below

public class UtilCache
{
 private UtilCache(String cacheName, int sizeLimit, int maxInMemory, long expireTimeMillis, boolean useSoftReference, boolean useFileSystemStore, String propName, String... propNames) {
        this.name = cacheName;
        this.sizeLimit = sizeLimit;
        this.maxInMemory = maxInMemory;
        this.expireTimeNanos = TimeUnit.NANOSECONDS.convert(expireTimeMillis, TimeUnit.MILLISECONDS);
        this.useSoftReference = useSoftReference;
        this.useFileSystemStore = useFileSystemStore;
        setPropertiesParams(propName);
        setPropertiesParams(propNames);
        int maxMemSize = this.maxInMemory;
        if (maxMemSize == 0) maxMemSize = sizeLimit;
        if (maxMemSize == 0) {
            memoryTable = new ConcurrentHashMap<Object, CacheLine<V>>();
        } else {
            memoryTable = new Builder<Object, CacheLine<V>>()
            .maximumWeightedCapacity(maxMemSize)
            .listener(this)
            .build();
        }
        if (this.useFileSystemStore) {
            // create the manager the first time it is needed
            jdbmMgr = fileManagers.get(fileStore);
            if (jdbmMgr == null) {
                Debug.logImportant("Creating file system cache store for cache with name: " + cacheName, module);
                try {
                    String ofbizHome = System.getProperty("ofbiz.home");
                    if (ofbizHome == null) {
                        Debug.logError("No ofbiz.home property set in environment", module);
                    } else {
                        jdbmMgr = new JdbmRecordManager(ofbizHome + "/" + fileStore);
                    }
                } catch (IOException e) {
                    Debug.logError(e, "Error creating file system cache store for cache with name: " + cacheName, module);
                }
                fileManagers.putIfAbsent(fileStore, jdbmMgr);
            }
            jdbmMgr = fileManagers.get(fileStore);
            if (jdbmMgr != null) {
                try {
                    this.fileTable = HTree.createInstance(jdbmMgr);
                    jdbmMgr.setNamedObject(cacheName, this.fileTable.getRecid());
                    jdbmMgr.commit();
                } catch (IOException e) {
                    Debug.logError(e, module);
                }
            }
        }
    }

    private static String getNextDefaultIndex(String cacheName) {
        AtomicInteger curInd = defaultIndices.get(cacheName);
        if (curInd == null) {
            defaultIndices.putIfAbsent(cacheName, new AtomicInteger(0));
            curInd = defaultIndices.get(cacheName);
        }
        int i = curInd.getAndIncrement();
        return i == 0 ? "" : Integer.toString(i);
    }

    public static String getPropertyParam(ResourceBundle res, String[] propNames, String parameter) {
        try {
            for (String propName: propNames) {
            if(res.containsKey(propName+ '.' + parameter)) {
                try {
                return res.getString(propName + '.' + parameter);
                } catch (MissingResourceException e) {}
            }
            }
            // don't need this, just return null
            //if (value == null) {
            //    throw new MissingResourceException("Can't find resource for bundle", res.getClass().getName(), Arrays.asList(propNames) + "." + parameter);
            //}
        } catch (Exception e) {
            Debug.logWarning(e, "Error getting " + parameter + " value from cache.properties file for propNames: " + propNames, module);
        }
        return null;
    }

    protected void setPropertiesParams(String cacheName) {
        setPropertiesParams(new String[] {cacheName});
    }

    public void setPropertiesParams(String[] propNames) {
        ResourceBundle res = ResourceBundle.getBundle("cache");

        if (res != null) {
            String value = getPropertyParam(res, propNames, "maxSize");
            if (UtilValidate.isNotEmpty(value)) {
                this.sizeLimit = Integer.parseInt(value);
            }
            value = getPropertyParam(res, propNames, "maxInMemory");
            if (UtilValidate.isNotEmpty(value)) {
                this.maxInMemory = Integer.parseInt(value);
            }
            value = getPropertyParam(res, propNames, "expireTime");
            if (UtilValidate.isNotEmpty(value)) {
                this.expireTimeNanos = TimeUnit.NANOSECONDS.convert(Long.parseLong(value), TimeUnit.MILLISECONDS);
            }
            value = getPropertyParam(res, propNames, "useSoftReference");
            if (value != null) {
                useSoftReference = "true".equals(value);
            }
            value = getPropertyParam(res, propNames, "useFileSystemStore");
            if (value != null) {
                useFileSystemStore = "true".equals(value);
            }
            value = getPropertyParam(res, new String[0], "cache.file.store");
            if (value != null) {
                fileStore = value;
            }
        }
    }

    private Object fromKey(Object key) {
        return key == null ? ObjectType.NULL : key;
    }

    @SuppressWarnings("unchecked")
    private K toKey(Object key) {
        return key == ObjectType.NULL ? null : (K) key;
    }

    private void addAllFileTableKeys(Set<Object> keys) throws IOException {
        FastIterator<Object> iter = fileTable.keys();
        Object key = null;
        while ((key = iter.next()) != null) {
            keys.add(key);
        }
    }

    public Object getCacheLineTable() {
        throw new UnsupportedOperationException();
    }

    public boolean isEmpty() {
        if (fileTable != null) {
            try {
                synchronized (this) {
                    return fileTable.keys().next() == null;
                }
            } catch (IOException e) {
                Debug.logError(e, module);
                return false;
            }
        } else {
            return memoryTable.isEmpty();
        }
    }

    /** Puts or loads the passed element into the cache
     * @param key The key for the element, used to reference it in the hastables and LRU linked list
     * @param value The value of the element
     */
    public V put(K key, V value) {
        return putInternal(key, value, expireTimeNanos);
    }

    public V putIfAbsent(K key, V value) {
        return putIfAbsentInternal(key, value, expireTimeNanos);
    }

    CacheLine<V> createSoftRefCacheLine(final Object key, V value, long loadTimeNanos, long expireTimeNanos) {
        return tryRegister(loadTimeNanos, new SoftRefCacheLine<V>(value, loadTimeNanos, expireTimeNanos) {
            @Override
            CacheLine<V> changeLine(boolean useSoftReference, long expireTimeNanos) {
                if (useSoftReference) {
                    if (differentExpireTime(expireTimeNanos)) {
                        return this;
                    } else {
                        return createSoftRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos);
                    }
                } else {
                    return createHardRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos);
                }
            }

            @Override
            void remove() {
                removeInternal(key, this);
            }
        });
    }

    CacheLine<V> createHardRefCacheLine(final Object key, V value, long loadTimeNanos, long expireTimeNanos) {
        return tryRegister(loadTimeNanos, new HardRefCacheLine<V>(value, loadTimeNanos, expireTimeNanos) {
            @Override
            CacheLine<V> changeLine(boolean useSoftReference, long expireTimeNanos) {
                if (useSoftReference) {
                    return createSoftRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos);
                } else {
                    if (differentExpireTime(expireTimeNanos)) {
                        return this;
                    } else {
                        return createHardRefCacheLine(key, getValue(), loadTimeNanos, expireTimeNanos);
                    }
                }
            }

            @Override
            void remove() {
                removeInternal(key, this);
            }
        });
    }

    private CacheLine<V> tryRegister(long loadTimeNanos, CacheLine<V> line) {
        if (loadTimeNanos > 0) {
            ExecutionPool.addPulse(line);
        }
        return line;
    }

    private CacheLine<V> createCacheLine(K key, V value, long expireTimeNanos) {
        long loadTimeNanos = expireTimeNanos > 0 ? System.nanoTime() : 0;
        if (useSoftReference) {
            return createSoftRefCacheLine(key, value, loadTimeNanos, expireTimeNanos);
        } else {
            return createHardRefCacheLine(key, value, loadTimeNanos, expireTimeNanos);
        }
    }
    private V cancel(CacheLine<V> line) {
        // FIXME: this is a race condition, the item could expire
        // between the time it is replaced, and it is cancelled
        V oldValue = line.getValue();
        ExecutionPool.removePulse(line);
        line.cancel();
        return oldValue;
    }

    /** Puts or loads the passed element into the cache
     * @param key The key for the element, used to reference it in the hastables and LRU linked list
     * @param value The value of the element
     * @param expireTimeMillis how long to keep this key in the cache
     */
    public V put(K key, V value, long expireTimeMillis) {
        return putInternal(key, value, TimeUnit.NANOSECONDS.convert(expireTimeMillis, TimeUnit.MILLISECONDS));
    }

    public V putIfAbsent(K key, V value, long expireTimeMillis) {
        return putIfAbsentInternal(key, value, TimeUnit.NANOSECONDS.convert(expireTimeMillis, TimeUnit.MILLISECONDS));
    }

    V putInternal(K key, V value, long expireTimeNanos) {
        Object nulledKey = fromKey(key);
        CacheLine<V> oldCacheLine = memoryTable.put(nulledKey, createCacheLine(key, value, expireTimeNanos));
        V oldValue = oldCacheLine == null ? null : cancel(oldCacheLine);
        if (fileTable != null) {
            try {
                synchronized (this) {
                    if (oldValue == null) oldValue = fileTable.get(nulledKey);
                    fileTable.put(nulledKey, value);
                    jdbmMgr.commit();
                }
            } catch (IOException e) {
                Debug.logError(e, module);
            }
        }
        if (oldValue == null) {
            noteAddition(key, value);
            return null;
        } else {
            noteUpdate(key, value, oldValue);
            return oldValue;
        }
    }


}

Create WSDL in Apache obfiz | Create web services in Apache ofbiz | Apache ofbiz web services tutorial.

In my project there is requirement of web services integration with Microsoft BizTalk i.e. Through WSDL
Biztalk will communicate with Apache ofbiz Services.

Following below step to follow for exposed ofbiz service to as web Services

1. Create a Apache ofbiz Services as below
   package org.ofbiz.party.party;

import java.util.Map;

import javolution.util.FastMap;

import org.ofbiz.service.DispatchContext;

public class TestWebService {
   
   
      public static Map<String, Object> testWebSerice(DispatchContext ctx, Map<String, ? extends Object> context) {
         
          Map<String, Object> result = FastMap.newInstance();
        try
        {
          String username=(String) context.get("userName");
         
          if(username.equals("kumud"))
          {
              result.put("returnMsg", "Hi Kumud") ;
          }
          else
          {
              result.put("returnMsg", "You are not kumud") ;
          }
         
          return result;
         
      }
     
      catch(Exception ex)
      {
          ex.fillInStackTrace();
          result.put("returnMsg", "Hi Kumud") ;
          return result;
      }
}
}


2. Register service in services.xml file as below

 <service name="TestWebSerice" engine="java" auth="false"
            location="org.ofbiz.party.party.TestWebService" invoke="testWebSerice"  export="false">
        <description>Test Web Service  by kumud</description>
       
        <attribute name="userName" type="String" mode="IN" optional="false"/>
        <attribute name="returnMsg" type="String" mode="OUT" optional="false"/>
       
    </service>

3. For expose above service as Web Services by making  export="true" in above configuration

   <service name="TestWebSerice" engine="java" auth="false"
            location="org.ofbiz.party.party.TestWebService" invoke="testWebSerice"  export="true">
        <description>Test Web Service  by kumud</description>
       
        <attribute name="userName" type="String" mode="IN" optional="false"/>
        <attribute name="returnMsg" type="String" mode="OUT" optional="false"/>
       
    </service>
   
4. Now start server hit by url http://localhost:8080/webtools/control/SOAPService/TestWebSerice?wsdl in web browser
   there show WSDL file

difference between return; and return (xx); in Java | Java concept

java return keyword is one of the most frequently used keyword in java and also one of the concept, which most programmers say they are well aware of. But, I still meet programmers who say that they are little confused when they come down to return statement. Especially with respect to the method which has void as their return type. So, this post would help you in clearing those doubts and help you understand the clear picture of it... in the most simplest way.
We all know that Java return keyword is used to return the control back to calling method with a certain data-type which was promised during the method declaration. As many wanted to know if we can use just the return keyword(like this: return;) but not return any value(like this: return obj;), then I have to say yes to that. Se the below java programs which will help you understanding the difference better,

public class ReturnTypes {
    public static void main(String args[]){
        functionVoid();
       
functionType();
    }
   
    public static int
functionType(){
        return 1;
    }

    private static void
functionVoid() {
        System.out.println("1");
        if(true){
            return;
        }
        // return;  // This line if UnCommented, would result in Compilation error
        System.out.println("2");
    }
}

If you see in the method functionType() the return keyword is used to return an primitive int value, which is quiet normal and know to almost every java programmer.

But, If you see the method functionVoid() it in fact uses the controversial return keyword(just "return;") which doesn't return anything at-all. Yes this is absolutely valid to use it that way... Ahaaaa hold on, I am not yet done completely... and now imagine if you can use the same return type outside the scope of if statement, as It's already available in program but commented out... That will certainly thorough you an compilation error.

Now why so.. There is nothing wrong with the syntax of return; statement but the error is something different as the compiler is just complaining about "Unreachable Code"... 

By now you know that we can use both of these conventions and are very much valid. But you can ask yourself a question on why actually java needs a return; statement in the first, when we already know that the method is not meant to return any thing back to the calling function as we have declared it void...??

The answer is, you can treat return keyword as a form of goto statement. As it will directly move the control to the line where the method is called. And if you want to accomplish that in an void method then what other option you have to do that other than return; statement.

How string comparison works in java | Java interview question for experinced developer

explain "how String comparison works in Java?" is the most common question interviewers ask these days, and today this post is going to explain the answer in detail. String is one of Java's most admired feature and most used class in the whole Java for sure and I hope you all won't deny that fact. But, sadly most programmers fail to answer this question. Reason is either learners or programmers don't take String seriously or they just don't take Java seriously. So, let's start our learning.....

There are two thing into consideration when we talk about String comparison
1. "==" normal Java comparison operator
2. ".equals()" method of Object class

"=="  in Java is used to compare two values, when I say values... I mean, they can be of any primitive types or any object types. Now when our focus is specifically on String class, we take objects alone into consideration. So, what really happens when you compare any two objects using == operator..? It's simple.. They just compare the two objects and see if they both are pointing to same memory location. If Java found that both strings are pointing to same memory location, then it returns boolean true else if both objects are pointing to different memory locations then it returns boolean false. Now, at this point of time don't worry about the implementation as I will explain with example in detail.

".equals()" in Java on the other hand is actually used to compare the contents of two objects. When I say contents of the objects, I mean value within the string and not the memory location to which the objects point to. In this kind of comparison if Java finds that the value in two different string objects are same, they return boolean true else boolean false. As simple as that isn't it..! Just give this below example a try in your favorite editor,

public class StringTestOne {
    public static void main(String[] args) {
        String s1 = "AAA";
        String s2 = "AAA";
        String s3 = "BBB";
       
        if(s1==s2){
            System.out.println("s1 == s2 : TRUE");
        } else {
            System.out.println("s1 == s2 : FALSE");
        }
        if(s1.equals(s2)){
            System.out.println("s1.equals(s2) : TRUE");
        } else {
            System.out.println("s1.equals(s2) : FALSE");
        }
       
        if(s1==s3){
            System.out.println("s1 == s3 : TRUE");
        } else {
            System.out.println("s1 == s3 : FALSE");
        }
        if(s1.equals(s3)){
            System.out.println("s1.equals(s3) : TRUE");
        } else {
            System.out.println("s1.equals(s3) : FALSE");
        }
    }
}
now let us trace the program,

-first the fourth if statement : it is resulting FALSE as we know that .equals() method compares the content of two object i.e "AAA" and "BBB"(no human on earth can prove that "AAA" is equal to "BBB"),  thus it's very clear that it has returned FALSE.

-then the third if statement : it returns FALSE as again we know that == operator will compare the memory location to which the objects s1 and s3 refers to, but not the values . As of now it makes sense that Java would create a separate memory location for every object created on heap. Thus, even in this case Java would have created two different objects s1 and s3 which would be pointing to two different memory places in memory. So, expected result FALSE. hmmm  you are smart..!

-then the second if statement : again as expected the values of s1 and s2 are same and hence the .equals() method will and should return TRUE.

-Now comes the first if statement : Why on earth is it returning TRUE while there are two different object s1 and s2 into consideration. Yes I agree to the point that they both have same values, but Java needs to compare the object handles while using == operator... isn't it what wee have learned till now..? Yes very much true and that's the reason we have this post today. 

This TRUE thing in the first if statement is due to the concept of  String class being immutable. You might have certainly learned in you college that your professors saying String is immutable. That's the reason Strings are so special in Java and that's the reason why few programmers still feel String comparison a confusing task. Don't worry as it would be explained clearly now, 

Internally Java uses the concept of String pool in order to make use of strings  efficiently across Java. Thus, whenever you create new string objects in your program, it will first search that string pool to see if there is any existing string value which is already created having same value. And if Java thinks that an value already exists in pool, then it takes no permission from you(as string is already final and programmer has no control on it) to point your new string object to the same memory location to which earlier object was pointing to. If it thinks there is not such value in the pool, they make a new entry into the pool. Let's take the earlier examples,

String s1 = "AAA"; //This will first search the String pool to see if there is any such value already existing in the pool. Knowing that there is no such string value, it will add the value into Java's String pool for the first time and assigns the memory location to object handle s1.
String s2 = "AAA"; //This will also first search the String pool to see if there is any such entry in the pool with the value "AAA", As you it finds one such entry it will just make use of that memory location and assigns the same to string object s2 as well.

Which means that, even though we have two objects s1 and s2 created, Java has done some internal optimization to ensure that it doesn't waste memory unnecessarily and thus s1 and s2 will be pointing to the same memory location where the value "AAA" is actually stored. That is the reason why we got TRUE in the first if statement earlier. And that is the reason why string is called immutable.

Difference between left and outer joins in database | database interview question

 There are two kinds of OUTER joins in SQL, LEFT OUTER join and RIGHT OUTER join. Main difference between RIGHT OUTER join and LEFT OUTER join, as there name suggest, is inclusion of non matched rows. Sine INNER join only include matching rows, where value of joining column is same, in final result set, but OUTER join extends that functionality and also include unmatched rows in final result. LEFT outer join includes unmatched rows from table written on left of join predicate. On the other hand RIGHT OUTER join, along with all matching rows, includes unmatched rows from right side of table. In short result of LEFT outer join is INNER JOIN + unmatched rows from LEFT table and RIGHT OUTER join is INNER JOIN + unmatched rows from right hand side table. Similar to difference between INNER join and OUTER join, difference between LEFT and RIGHT OUTER JOIN can be better understand by a simple example, which we will see in next section. By the way joins are very popular in SQL interviews, and along with classic questions like finding second highest salary of employee, Inner join vs outer join or left outer join vs right outer join is commonly asked.


LEFT and RIGHT OUTER Join Example in SQL
LEFT vs RIGHT OUTER JOIN in SQL, MySQL databaseIn order to understand difference between LEFT and RIGHT outer join, we will use once again use classical Employee and Department relationship. In this example, both of these table are connected using dept_id, which means both have same set of data in that column, let's see data on these two table.

mysql> select * from employee;
+--------+----------+---------+--------+
| emp_id | emp_name | dept_id | salary |
+--------+----------+---------+--------+
|    103 | Jack     |       1 |   1400 |
|    104 | John     |       2 |   1450 |
|    108 | Alan     |       3 |   1150 |
|    107 | Ram      |    NULL |    600 |
+--------+----------+---------+--------+
4 rows in set (0.00 sec)

mysql> select * from department;
+---------+-----------+
| dept_id | dept_name |
+---------+-----------+
|       1 | Sales     |
|       2 | Finance   |
|       3 | Accounts  |
|       4 | Marketing |
+---------+-----------+
4 rows in set (0.00 sec)

If you look closely, there is one row in employee table which contains NULL, for which there is no entry in department table. Similarly department table contains a department (row) Marketing ,for which there is no employee in employee table. When we do a LEFT or RIGHT outer join it includes unmatched rows from left or right table. In this case LEFT OUTER JOIN should include employee with NULL as department and RIGHT OUTER JOIN should include Marketing department. Here is example of LEFT and RIGHT OUTER Join in MySQL database :

mysql> select e.emp_id, e.emp_name, d.dept_name from employee e LEFT JOIN department d on e.dept_id=d.dept_id;
+--------+----------+-----------+
| emp_id | emp_name | dept_name |
+--------+----------+-----------+
|    103 | Jack     | Sales     |
|    104 | John     | Finance   |
|    108 | Alan     | Accounts  |
|    107 | Ram      | NULL      |
+--------+----------+-----------+
4 rows in set (0.01 sec)

As I said unmatched rows, i.e. row with dept_id as NULL has included in final result and dept_name for that row is NULL, as there is no corresponding row for NULL dept_id in department table. But note that Marketing department is not included in this result. Now, let's see example of RIGHT OUTER JOIN in MySQL, this should include Marketing department but leave out employee with NULL dept_id.

mysql> select e.emp_id, e.emp_name, d.dept_name from employee e RIGHT JOIN department d on e.dept_id=d.dept_id;
+--------+----------+-----------+
| emp_id | emp_name | dept_name |
+--------+----------+-----------+
|    103 | Jack     | Sales     |
|    104 | John     | Finance   |
|    108 | Alan     | Accounts  |
|   NULL | NULL     | Marketing |
+--------+----------+-----------+
4 rows in set (0.00 sec)

As I said, final result set has Marketing department and emp_id, emp_name is NULL in that row because there is no employee with dept_id=4 in employee table.

Difference between LEFT and RIGHT OUTER JOIN in SQL
In short, following are some notable difference between LEFT and RIGHT outer join in SQL :

1) LEFT OUTER join includes unmatched rows from left table while RIGHT OUTER join includes unmatched rows from right side of table.

2) Result of LEFT OUTER join can be seen as INNER JOIN + unmatched rows of left able while result of RIGHT OUTER join is equal to INNER JOIN + unmatched rows from right side table.

3) In ANSI SQL, left outer join is written as LEFT JOIN while right outer join is written as RIGHT JOIN in select sql statements.

4) In Transact-SQL syntax left outer join is written as *= and right outer join is written as =*, Sybase database supports both syntax and you can write join queries in both ANSI and T-SQL syntax.

That's all on difference between LEFT and RIGHT OUTER JOIN in SQL. We have seen example of RIGHT and LEFT join in MySQL database but since we have used ANSI syntax of OUTER joins, it's for other databases e.g. Oracle, Sybase, SQL Server and PostgreSQL as well. JOIN is a one of the most important and common concept in SQL and you should be good enough to figure out which rows will be included as a result of JOIN statement before actually running that SELECT query against any table. Some time erroneous JOIN query can bring loads of data and potentially may hang your database so beware of it.

Producer-Consumer Problem in Java | Java Thread interview question for Experinced developer

The producer-consumer problem is one of the most frequently encountered problems when we attempt multi threaded programming. While not as challenging as some of the other problems in multi-threaded programming, an incorrect implementation of this problem can create a mess of your application. Produced items will be left unconsumed, starting items will be skipped, consumption depends on whether the production began earlier or later than consumption attempts etc. To add to this you might notice the anomalies long after it has actually happened and most importantly like almost all multi-threaded programs, this one is hard to debug and reproduce too.
So in this post I thought I would attempt to solve this problem in Java with the help of Java' awesome java.util.concurrent package and its classes.
First of all, let us see the characteristics of the producer-consumer problem:
  • Producer(s) produce items.
  • Consumer(s) consume the items produced by the producer(s).
  • Producer(s) finish production and let the consumers know that they are done.
Note that in this producer-consumer problem the producer is running on a different thread than the ones on consumer. This setup makes sense in two cases:
  • The steps to do the consumption of the item produced in independent and not dependent on other items.
  • The time to process the items is larger that the time to produce them.
The term "larger" in the second point is used a bit loosely. Consider the case where producer reads a line from a file and the "consumption and processing" is just to log the line in a special format back to a file then the use of a producer consumer problem solution can be considered a case of over-engineering a solution. However if for each of those lines the "consumption and processing" step is to make a HTTP GET/POST request to a web-server and then dump the result somewhere then we should opt for a producer-consumer solution. In this case I am assuming that all the data to do a GET/POST is available in the line (item) itself and we are not dependent on previous/next lines.
So let us first take a look at the characteristics of the producer-consumer problem solution that I have posted below:
  • There can be multiple producer.
  • There will be multiple consumers.
  • Once the production of new items is done the producer(s) will let the consumers know so that the consumer will exit after the last item is consumed and processed.
It is interesting to note that to solve this problem at a generic level we can address only the consumer side and not the producer side. This is because the production of items might be done at any time and there is very little that we can do in a generic way to control the production of items. We can, however control the consumer's behaviour while accepting items from producer(s). Having laid out the rules let us take a look at the consumer contract:


  1. package com.maximus.producerconsumer;  
  2.   
  3. public interface Consumer  
  4. {  
  5.  public boolean consume(Item j);  
  6.    
  7.  public void finishConsumption();  
  8. }   

Here the consumer can be shared between multiple producers of similar items; by similar items I mean producer that produces objects of type "Item". The definition if Item is as follows:


  1. package com.maximus.consumer;  
  2.   
  3. public interface Item  
  4. {  
  5.  public void process();  
  6. }  
Now we take a look at an implementation of the Consumer interface:

  1. package com.maximus.consumer;  
  2.   
  3. import java.util.LinkedList;  
  4. import java.util.List;  
  5. import java.util.concurrent.BlockingQueue;  
  6. import java.util.concurrent.ExecutorService;  
  7. import java.util.concurrent.Executors;  
  8. import java.util.concurrent.LinkedBlockingQueue;  
  9.   
  10. public class ConsumerImpl implements Consumer  
  11. {  
  12.  private BlockingQueue< Item > itemQueue =   
  13.   new LinkedBlockingQueue< Item >();  
  14.    
  15.  private ExecutorService executorService =   
  16.   Executors.newCachedThreadPool();  
  17.    
  18.  private List< ItemProcessor > jobList =   
  19.   new LinkedList< ItemProcessor >();  
  20.    
  21.  private volatile boolean shutdownCalled = false;  
  22.     
  23.  public ConsumerImpl(int poolSize)  
  24.  {  
  25.   for(int i = 0; i < poolSize; i++)  
  26.   {  
  27.    ItemProcessor jobThread =   
  28.     new ItemProcessor(itemQueue);  
  29.      
  30.    jobList.add(jobThread);  
  31.    executorService.submit(jobThread);  
  32.   }  
  33.  }  
  34.    
  35.  public boolean consume(Item j)  
  36.  {  
  37.   if(!shutdownCalled)  
  38.   {  
  39.    try  
  40.    {  
  41.     itemQueue.put(j);  
  42.    }  
  43.    catch(InterruptedException ie)  
  44.    {  
  45.     Thread.currentThread().interrupt();  
  46.     return false;  
  47.    }  
  48.    return true;  
  49.   }  
  50.   else  
  51.   {  
  52.    return false;  
  53.   }  
  54.  }  
  55.    
  56.  public void finishConsumption()  
  57.  {  
  58.   shutdownCalled = true;  
  59.     
  60.   for(ItemProcessor j : jobList)  
  61.   {  
  62.    j.cancelExecution();  
  63.   }  
  64.     
  65.   executorService.shutdown();  
  66.  }  
  67. }  

Now the only point of interest is the ItemProcessor that the consumer internally uses to process the incoming items. ItemProcessor is coded as follows:

  1. package com.maximus.consumer;  
  2.   
  3. import java.util.concurrent.BlockingQueue;  
  4. import java.util.concurrent.TimeUnit;  
  5.   
  6. public class ItemProcessor implements Runnable  
  7. {  
  8.  private BlockingQueue< Item> jobQueue;  
  9.    
  10.  private volatile boolean keepProcessing;  
  11.     
  12.  public ItemProcessor(BlockingQueue< Item > queue)  
  13.  {  
  14.   jobQueue = queue;  
  15.   keepProcessing = true;  
  16.  }  
  17.    
  18.  public void run()  
  19.  {  
  20.   while(keepProcessing || !jobQueue.isEmpty())  
  21.   {  
  22.    try  
  23.    {  
  24.     Item j = jobQueue.poll(10, TimeUnit.SECONDS);  
  25.       
  26.     if(j != null)  
  27.     {  
  28.      j.process();  
  29.     }  
  30.    }  
  31.    catch(InterruptedException ie)  
  32.    {  
  33.     Thread.currentThread().interrupt();  
  34.     return;  
  35.    }  
  36.   }  
  37.  }  
  38.    
  39.  public void cancelExecution()  
  40.  {  
  41.   this.keepProcessing = false;  
  42.  }  
  43. }   
The only challenge above is the condition in the while loop. The while loop is so written to support the continuation of the consumption of items even after the producer(s) have finished production and has notified the consumer that production is finished. The above while loop ensures that consumption of all the items is done before the threads exit.This will be the case when producers run faster that consumers.

The above consumer is thread-safe and can be shared multiple producers such that each producer may concurrently call consumer.consume() without bothering about synchronization and other multi-threading caveats. Producers just need to submit an implementation of the Item interface whose process() method will contain the logic of how the consumption will be done.

As a bonus for reading the post I put forward a test program that demonstrates how to use the above classes:



  1. package com.maximus.consumer;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.InputStreamReader;  
  7.   
  8. public class Test  
  9. {  
  10.  public static void main(String[] args) throws Exception  
  11.         {  
  12.          Consumer consumer = new ConsumerImpl(10);  
  13.            
  14.          BufferedReader br =   
  15.           new BufferedReader(  
  16.           new InputStreamReader(  
  17.           new FileInputStream(  
  18.           new File(args[0]))));  
  19.            
  20.          String line = "";  
  21.            
  22.          while((line = br.readLine()) != null)  
  23.          {  
  24.           System.out.println(  
  25.            "Producer producing: " + line);  
  26.           consumer.consume(new PrintJob(line));  
  27.          }  
  28.            
  29.          consumer.finishConsumption();  
  30.         }  
  31. }  
  32.   
  33. class PrintJob implements Item  
  34. {  
  35.  private String line;  
  36.    
  37.  public PrintJob(String s)  
  38.  {  
  39.   line = s;  
  40.  }  
  41.    
  42.  public void process()  
  43.  {  
  44.   System.out.println(  
  45.    Thread.currentThread().getName() +   
  46.    " consuming :" + line);  
  47.  }  
  48. }  
The above consumer can be tweaked in a host of different ways to make it more flexible. We can define what the consumer will do when production is done. It may be tweaked to allow batch processing but I leave that to the user. Feel free to use it and twist it in whatever way you want.

Most common error : java.lang.UnsupportedClassVersionError: when is it thrown by a JVM & how to resolve it | Experinced level java interview question

UnsupportedClassVersionError is (evidently a descendant of the Error class in Java) which subclasses java.lang.ClassFormatError which in turn subclasses java.lang.LinkageError (which is a direct subclass of java.lang.Error). This error is thrown in the cases when the JVM (Java Virtual Machine) attempts to read a class file and finds that the major and minor version numbers in the particular class file are not supported. This happens in the cases when a higher version of Java Compiler is used to generate the class file than the JVM version which is used to execute that class file. Please do notice that this is not true vice-versa which means you can comfortably use a higher version of JVM to run a class file compiled using an earlier version of Java Compiler. Let’s understand why? We'll see below a valid Java program and subsequently an alert and a stack trace of the error which are thrown as a result of the above explained situation which causes UnsupportedClassVersionError. FYI: The output

public class TestFinally {

      public static void main(String[] args) {
             
            try{
                  try{
                        throw new NullPointerException ("e1");
                  }
                  finally{
                        System.out.println("e2");
                  }
            }
            catch(Exception e){
                        e.printStackTrace();
            }
      }
}



The error alert and the stack trace if the right JVM is not used



java.lang.UnsupportedClassVersionError: TestFinally (Unsupported major.minor version 49.0)
      at java.lang.ClassLoader.defineClass0(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
      at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
      at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
      at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)


The minor and major version numbers are represented by the major_version and minor_version items respectively in a Java Class File structure. A combination of these two unsigned integers (both of length 2 bytes each) determines the particular version of the class file format. If a class file has major version number M and minor version number m, we denote the version of its class file format as M.m.

As per Java VM Spec, “A Java virtual machine implementation can support a class file format of version v if and only if v lies in some contiguous range Mi.0 v Mj.m. Only Sun can specify what range of versions a Java virtual machine implementation conforming to a certain release level of the Java platform may support.” For example: Implementations of version 1.2 of the Java 2 platform can support class file formats of versions in the range 45.0 through 46.0 inclusive.

Major version number of the class file formats of the popular Java versions are: J2SE 6.0 = 50, J2SE 5.0 = 49, JDK 1.4 = 48, JDK 1.3 = 47, JDK 1.2 = 46, JDK 1.1 = 45. Now you understand what in our example “major.minor version 49.0” signifies? It simply means we have used J2SE 5.0 for compiling the source code as the class file format is 49.0 where major_version is ‘49’ and minor_version is ‘0’.

Errors are never detected at compile time. This is quite easier to understand in this case – how can the compiler have the information at the time of compilation about which version of JVM would be used execute the compiled class file? It can't, right? So is the case with other errors as well. This is the reason why all the Errors are unchecked. The error alert also rightly shows that it's a JVM Launcher error.

How to resolve UnsupportedClassVersionError?


Whenever you encounter this error, do check if you’re using an earlier version of JVM to execute the class file than the corresponding version of compiler you used to compile the source code. The example shown here was compiled using Java 5.0 compiler, but when I tried to run using JVM 1.4, I got the above error. I just needed to switch either to a JVM version 5.0 or above OR needed to switch to a Java Compiler of JDK 1.4 or below (you of course need to make sure the source code is compatible with the corresponding version of the compiler otherwise you’ll start getting other compiler errors).

A higher JVM version doesn’t cause a problem in most of cases unless the class file format is quite old (and hence doesn’t lie in the supported range as specified by Sun for that particular JVM version ... as discussed above). But, it’s always a good practice to have both the Compiler and the JVM of the same version.