Friday, November 16, 2012

Usage of JMS Message ID and Correlation ID

These two JMS message headers are used to link messages being exchanged between two applications.

Standard pattern is:
  • App1 sends a message to App2
  • App2 stores the JMSMsgID (say 123) and completes the processing
  • App2 generates response for App1 and sets above JMSMsgID (123) as JMSCorrelID
  • App1 receives the response, using the JMSCorrelID it matches with the original request
Setting/getting correlation ID is straight forward.  Tricky one is MsgID.

From Java EE doc:

void setJMSMessageID(String id) throws JMSException

Sets the message ID.

JMS providers set this field when a message is sent. This method can be used to change the value for a message that has been received
.

I am not sure what purpose it solves to change message ID after a message is received.  Key point to note is that JMS provider shall set/override this value when the message is published.  Which means even if you try to override by calling this method, the value will be lost!!

On Websphere default messaging, we can set message ID to any value, but on JBoss messaging it mandates that you prefix the value with ID: as per spec :-)  Anyways the value gets lost.  So how to use this property??

As highlighted above message ID is set after the message is sent/published.  We can get the same using getJMSMessageID method on Message.  We need to store this to link with the response we receive later.

So far good.  Then later I got to know from our client that he is sending his own message ID, back to square one thinking how is this possible?  Silly me...this restriction is imposed by JMS API !! so my client must be doing this using different API.  For IBM MQ, we have a Java API, which is seperate from JMS API.  It also offers API for C as well.  When you use this API, this restriction does not apply.  We can set the message ID value to whatever we want.  The doc of this API confirms this, below is the extract:

Tuesday, October 30, 2012

Run jar file in a secure way

Often we download java tools/applications that are either from known sources or to try out their features.  Mostly they would come with either a bat/sh file to setup the path for the application and launch the jar.  Say you are logged in with an elevated user and what if the application you want to try just wipes out C drive!!

Always run the jar files (tweak the shell/batch scripts if required) to enable java security manager.  All that we need to do is launch jvm as java -Djava.security.manager -jar <jarFile>

This will use the default policy file that comes with JDK at JAVA_HOME/lib/security/java.policy, which is good enough.

Monday, October 29, 2012

Oracle jdbc driver changed since 9i

Came across this while going through the tomcat docs.  Since Oracle 9i release the jdbc driver class oracle.jdbc.driver.OracleDriver is deprecated and oracle.jdbc.OracleDriver is the new one to be used.  We use 10g and have been using the old driver since last 3 years.  Not sure if this will make any difference interms of performance though.  Will update once we have some stats.

Friday, October 26, 2012

Intro to OSGi

Came across the nice book by Neil Bartlett on OSGi while reading news on Jigsaw getting delayed and plans to include it in Java 9.

His book is so nice and I finished reading the intro, below are quick facts we should look to design for OSGi

Version Dependency
METAINF.MF could be used to declare the dependencies, but can not enforce version.

1. For example, suppose we determine somehow that a JAR has a dependency on Log4J. Which version of Log4J do we need to supply to make the JAR work?

2. Versions cause other problems. Suppose our application requires two libraries, A and B, and both of these libraries depend in turn upon a third library, C.  But they require different versions: library A requires version 1.2 or greater of C, but library B requires version 1.1 of C, and will not work with version 1.2!

Lack of Information Hiding Across JARs
Classes within a JAR need to have access to the classes in other packages of the same JAR. That means we must make those classes public, because that is the only access modifier which makes classes visible across package boundaries.  As a consequence, all those classes declared public are accessible to clients outside the JAR as well.  Therefore the whole JAR is effectively public API, even the parts that we would prefer to keep hidden.

I hope to continue and complete this book.

Tuesday, May 22, 2012

Moving to IntelliJ and back

Post the release of IntelliJ ver 11, I have heard lot of good things and thought of giving it a try.  I saw it is open-source and free, but realised only the community edition is free and which is way limited in its capabilities compared to the paid version.  Somewhere I read that IntelliJ is a smart Java IDE where as eclipse is a general purpose IDE, Java being one of the languages it supports or rather famous for.  The download is an exe rather than a zip.  As I do not have admin rights on my lap, I usually prefer zip versions.  But I was happy as the exe is itself can be unzipped (7-zip).

You will find the exe to run in "ideaIC-11.1.2\bin" folder, and unlike eclipse it did not ask me to choose a folder to keep the profile/settings, uses APPDATA on XP to create a folder in your user profile.  As this adds to my profile size, I always move it to my D drive if possible.  There is a file in "ideaIC-11.1.2\bin\idea.properties", where you can change the locations and tell IntelliJ to store your preferences and plugin data.  I found in the forum which says zip version is provided only for ultimate edition, not sure why !!

Once it is up and running I really did struggle to move around and code as there is no shortcut to un learn eclipse shortcuts and get to used to IntelliJ quickly.  Download and keep the shortcuts reference aside, it will help a lot in making the ide usage a pleasant one.  As I keep using I can feel the intelligence, while doing auto-fill or auto-suggestions.  But I miss the eclipse feature to insert comma or semicolon automatically at the end, need to press Ctrl+Enter to do the same.  Another good thing is its seamless support for maven, autofilling the dependency info in your pom is really good, but couldn't figure out how to run any specific goal instead of build/run available in the menu.  As an exercise I started to learn Stripes and though using maven tomcat/jetty plugins I could deploy and test my web application slowly I felt the need to have support for the server from within IDE, I actually wanted to do remote debugging.  Also felt that it eats away my battery very quickly, not sure how fast, but you can feel it.

After playing around for some more time, I went back to netbeans/eclipse.  I keep switching between them.  So finally my experience is - the IntelliJ community edition is not worth dumping eclipse/netbeans.  But if you can get to the entriprise version, I do think it will be more fun as they promise.