Tuesday, March 31, 2015

Webshpere MQ SSL Errors in Java - Solved

If you are working in Java application which is using WebSphere MQ for messaging over SSL then you are bound to face some setup, certificate and keystore vs truststore related error. For first timers understanding SSL and MQ errors is nightmare, forget about solving them. I have gone through that and after spending hours on Google and Websphere MQ documentations, I managed to solve some of the problems we face while connecting to MQ over SSL from our Java application. In this article, I am listing down these errors and exceptions and their cause and solution for everyone's benefit. Next time you face any MQ SSL issue, hopefully you will find the right solution to solve these tricky errors. In this tutorial we will mainly looked at three errors :
  • Unable to find valid certification path to requested target
  • JMSWMQ2020: Failed to connect to queue manager
  • Remote SSL peer name error for channel 'ABC.XYZ'

I have mostly encountered these while working with Java application which was connecting to other legacy system using MQ for sending and receiving XML files, but useful to anyone who is using over MQ over SSL.

Monday, March 30, 2015

What is Effectively Final variable of Java 8

Apart from big three, Lambda expression, Stream API and new Date and Time API, Java 8 has also introduced a new concept called "effectively final" variable. A non final local variable or method parameter whose value is never changed after initialization is known as effectively final. Its very useful in the context of lambda expression. If you remember, prior to Java 8, we cannot use a non final local variable in anonymous class. If you have to access a local variable ( a method parameter or a variable decreed in the method itself) in Anonymous class, you have to make it final. This restriction is lessen a bit with lambda coming up. Java designers have take the need to make local variable final if its not changed once initialized. This is really a good proactive step from them, they must have anticipated frequent use of lambda expressions as compared to minimal use of Anonymous class and realized the pain to declare a local variable final every time in order to access it inside lambda. The rule has not changed, any local variable still has to be effectively final to be used inside lambda expression or anonymous inner class, its just that you don't need to use final keyword anymore, saving a few keystroke. If you don't follow this then you will get following error : "local variables referenced from a lambda expression must be final or effectively final". Let's see some examples of effectively final variables with lambdas and anonymous class and how it make life of Java developer easy.

Friday, March 27, 2015

Parsing Large JSON Files using Jackson Streaming API Example

In last couple of JSON tutorials for Java programmers, we have learned how to parse JSON using JSON-Simple library, parsing JSON array to Java array using GSon, and in this tutorial we will learn how to parse a large JSON file in Java using Jackson's Streaming API. Jackson is one of the most popular JSON processing framework and provides three main model to parse and process JSON data including Streaming API, data binding and tree model. Out of these three, Streaming works at lowest level and can be used to parse huge JSON response upto even giga bytes of size. If you are familiar with XML parsing, then you know that how difficult it is to parse huge XML files with DOM parser because it fully loads the file in memory before you can process it. In case you have low memory e.g. Android devices you can't use that to parse XML. Thankfully, XML provides SAX and StAX parsers which are streaming based and can be used to process huge files without loading them completely in memory. Out of these two, StAX is even better because it allows pull based processing where client pulls data from parser instead of parser pushing data, which is the case with SAX parser. Jackson's Streaming API is similar to StAX parser. You can pull the data you want and ignore what you don't want. Though performance doesn't come without cost, using Streaming API is little difficult then using other Jackson model which provides direct mapping between Java and Jackson objects. You have to handle all JSON data by yourself while using Streaming API.