If you are copying a file in java and then calling a program that uses that file, you must use a FileChannel. The File.flush() command only ensures that the direction is sent to the operating system, not that the file is actually written to disk. So if you create/copy a file, and then have another program that references that program (typically through a java.lang.Process call), sometimes the process call will fail as it does not see the file. Instead use a FileChannel.force() command this guarantees that the call is issues to the OS and that the file has been written to disk. One note: this guarantee only applies to the local disk. If you are writing to a remote disk like a NAS, you’ll have to make sure that those are synchronized or are configured for immediate writes.
Here’s some code for a very simple copy:
(more…)
When providing a pause in the program flow, you use Thread.currentThread.sleep(). But this call possibly throws an InterruptedException. It happens very rarely that your code would be interrupted, but it is possible in which case you will not have paused as long as you wished. Also, including the try/catch logic is bulky and distracting. So its good to have utility classes for your application and you may wish to include a ThreadUtils.sleep method as follows:
(more…)
java.lang.ProcessBuilder allows a developer to redirect the stderr and stdout of an external program to the logger. Here’s some code to show how to do that:
(more…)
Use log4j email appenders to notify operations of problems in your application. Below is sample xml code. Be sure to include activation.jar and mail.jar on your classpath. One note, unless you do some coding, out of the box log4j will only send emails for messages logged at ERROR or FATAL even if you specify otherwise.
Also, you should add the hostname to the subject by including ${hostname} in the appender and adding -Dhostname=myhostname as an environment variable passed to the jvm when starting your application.
(more…)
Here are some configuration hints for spring jms. This is backed by activemq jms provider.
A few comments:
(more…)
This was a pain to set up. But here are a few hints for non-transactional configurations.
A few notes about this setup:
(more…)
jconsole is a cool tool part of 1.6 that can be found in <jdk>/bin. You can connect to local running processes or connect to remote applications usually on port 1099. You can monitor CPU, memory, threads. To perform a heap dump, go to MBeans tab > com.sun.management > HotSpotDiagnostic > Operations.
Input a file location plus the file name in p0 parameter. ex. d:\temp\mydumps\heap.dump.out.
To read this dump you’ll use the built in jhat tool. ex: jhat heap.dump.out to browse through class usage.