Showing posts with label Troubleshooting. Show all posts
Showing posts with label Troubleshooting. Show all posts

Wednesday, April 23, 2014

Passing System Property Resource from JNLP file to Java Webstart

Java have changed the way how system properties are passed from jnlp file to the javaws (Java Webstart).

Since the update of Java to 1.7.0_45, getting the system property using the following code will no longer work:
System.getProperty("myproperty");
There are two solution for this issue:
1. You need to sign the jnlp files

     See this link for instruction:
     http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/signedJNLP.html

2. Update the JNLP files to have additional prefix in the resource string variable

     Example: For log4j.configuration, here is what you need to do:
<resources>
<j2se version="1.7"/>
<jar href="lib/log4j-1.2.16.jar"/>
<property name="jnlp.log4j.configuration" value="file:///C:/log4j.properties "/>
</resources>

---------------------------------------------------------------------------------------------------------------------------------

I haven't tried the #1 yet because it is quite complex. Option #2 is the quickest solution. However, you need to update your program to also add a prefix when reading the system property.
System.getProperty("jnlp.myproperty");
For backward compatibility, it is recommended to always check for the old variable before using the new variable.

       String property = System.getProperty("jnlp.myproperty");
       if (property.isEmpty())
       {
             property = System.getProperty("myproperty");
       }

Feel free comment if you still encounter any issue related to this.

Credits to:
https://community.oracle.com/message/11239354

Tuesday, April 8, 2014

Changing log level in log4j without restarting your application

For instance,  you have an application deployed in production and you need to set the log level to debug to investigate an issue. Normally, you will edit the log4j.properties and restart the application in order to have the changes take effect.

What if there are many users currently logged in to the application and you are not allowed to restart?

Will you wait for every user to logoff? What if there are 24/7 operations running on the application?

In that case, restart is not an option. Reverting the log level back to original will also require another restart.

Solution
Implement Log4j file watchdog


Although you will still need to deploy code changes to implement the watcher, you only do it at first then the succeeding changes on your log4j configuration (log4j.properties) will no longer need application restart to take effect.

Here's how it's done
org.apache.log4j.Logger logger = null;
org.apache.log4j.PropertyConfigurator.configureAndWatch("C:\\log4j.properties");
logger = org.apache.log4j.Logger.getLogger("");
logger.info("Info Message");
logger.error("Error Message");
logger.warn("Warn Message");
logger.trace("Trace Message");



No idea on what is log4j and how to use it?
Visit https://logging.apache.org/log4j/1.2/

Wednesday, April 2, 2014

ConcurrentModificationException when you see nothing wrong in the code!

You may sometimes encounter exception such as ConcurrentModificationException and tried to look at the code with the given stacktrace and see nothing wrong. 

java.util.ConcurrentModificationException
 at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
 at java.util.HashMap$KeyIterator.next(Unknown Source)
 at c2.server.SessionManager.expireSessions(SessionManager.java:130)
 at c2.server.SessionExpiry.runThread(SessionExpiry.java:49)
 at c2.prm.IThreadAdmin.run(IThreadAdmin.java:276)

When you look at the code in SessionMager.expireSessions(), this is how it looks like:

                Set activeSessions = new HashSet();
                for (Iterator iterator = activeSessions.iterator(); iterator.hasNext();)
               {
                 Session session = (Session) iterator.next();

                 if (isSessionExpired(session))
                 {
                 activeSessions.remove(session);
                 }
             }

The exception is thrown in the highlighted line. On first look, were you able to pinpoint what's wrong in this code?

I admit. I didn't see what's wrong at first. I did a lot of research on google and tried few suggestions but nothing worked. My project is built on Java 1.3 therefore it's not easy to apply a fix especially if the code is not compatible.

After asking someone to have a second look and while explaining the situation, I finally realized what was wrong. It is the following line:

               activeSessions.remove(session);

So what it is trying to remove was an object from the collection itself!

By now you should have guessed the solution to this?

If you still haven't realized, the following is the solution.

                Set activeSessions = new HashSet();
                 for (Iterator iterator = activeSessions.iterator(); iterator.hasNext();)
                  {
                 Session session = (Session) iterator.next();
                
                 if (isSessionExpired(session))
                 {
                 iterator.remove();
                 }
                 }

Highlight to see the answer :)
AOL Directory Sumbit Url
Submit Site to Yahoo