Creating a Java heapdump in JBoss AS 6.x

Submitted by Jochus on Wed, 12/12/2012 - 23:12 | Posted in: Java
Posted in

If you want to create a heapdump from your application running in a JBoss AS 6.X J2EE container, you can easily perform the dump operation by some MBean in the JMX console.

  • Browse to: http://localhost:8080/jmx-console/
  • Search for and click on type=HotSpotDiagnostic
  • Scroll to the bottom of the page and provide the parameter /tmp/heap.dump in the textfield p0, which is actually the location to which the dump will be written. If you want to perform a garbage collection before dumping, leave p1 on value true
  • Push the button Invoke. A dump file will be available at /tmp/heap.dump. This file can easily be viewed by a tool like Java VisualVM

Also, remember that you can easily create a heap dump automatically when JBoss is going OutOfMemory. Just add the following Java arguments in your run.conf file

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heap.dump

How to re-enable the Apple-provided Java SE 6 applet plug-in

Submitted by Jochus on Mon, 10/12/2012 - 15:08 | Posted in: Mac
Posted in

After the latest Java update on Mac OS X 10.7, I was unable to use the Java plugin in my Chrome browser (I'm not really into Java applets, but the Java applet in JIRA to attach screenshots is very easy and useful to use in combination with Cmd + Shift + 4 buttons).

To fix this issue, follow the steps written below:

  • Open Terminal, located in the Utilities folder.
  • Enter this command, then press the Return or Enter key:
    $ sudo mkdir -p /Library/Internet\ Plug-Ins/disabled
  • Enter this command, then press the Return or Enter key:
    $ sudo mv /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin /Library/Internet\ Plug-Ins/disabled
  • Enter this command, then press the Return or Enter key:
    $ sudo ln -sf /System/Library/Java/Support/Deploy.bundle/Contents/Resources/JavaPlugin2_NPAPI.plug


FCCU virus verwijderen

Submitted by Jochus on Sat, 08/12/2012 - 19:29 | Posted in: Windows
Posted in

Gisterenavond bij iemand langs geweest omdat die het bovenstaande scherm kreeg. De PC zit volledig blok, je kan nergens meer naar toe. Zelfs via "Veilige modus" lukte het niet meer.

Hardnekkig virus/malware, maar ik heb het er op deze manier afgekregen:

  • Download Windows Offline Defender en maak een startup cd/dvd of usb stick
  • Start daarna de pc op met de Windows Offline Defender disk
  • Scan u computer op malware met de Windows Offline Defender software
  • Herstart uw PC en kies voor een normale startup. Het virus zal nu niet meer opkomen, maar het is wel nog steeds aanwezig op de PC
  • Scan daarna u computer met MalwareBytes AntiMalware om het FCCU virus te verwijderen of te detecteren
  • Scan nogmaals met ComboFix! om deze malware nog dieper te detecteren en te verwijderen

Windows Offline Defender en MalwareBytes hebben er voor gezorgd dat de PC terug "vrij" kwam.
De ComboFix! heeft de "Veilig modus" terug hersteld.

... geef mij toch Mac OS X :-) ...

Discovering long running queries/dead lock situation in SQL Server

Submitted by Jochus on Mon, 26/11/2012 - 22:54 | Posted in: Java
Posted in

I recently came across this situation on a M$ SQL Server 2008 database:

I was doing an update operation on a certain table. While doing this update, a clustered index was being read. To be able to read from that index, a lock was obtained on a non-clustered index. The update operation went perfectly.
Now, in the same transaction, I was doing a select query which needed a lock on a non-clustered index. Unfortunately for me, this lock was already taken by the update statement and it would only be released when the transaction completed.

More details on this issue can be found on this blogpost.

At first, I knew there was a deadlock, but I didn't know on which resources/queries. This query helped me a lot while debugging:

SELECT DTL.resource_type,  
       WHEN DTL.resource_type IN ('DATABASE', 'FILE', 'METADATA') THEN DTL.resource_type  
       WHEN DTL.resource_type = 'OBJECT' THEN OBJECT_NAME(DTL.resource_associated_entity_id, SP.[dbid])  
       WHEN DTL.resource_type IN ('KEY', 'PAGE', 'RID') THEN   
           SELECT OBJECT_NAME([object_id])  
           FROM sys.partitions  
           WHERE sys.partitions.hobt_id =   
       ELSE 'Unidentified'  
   END AS requested_object_name, DTL.request_mode, DTL.request_status,  
   DEST.TEXT, SP.spid, SP.blocked, SP.status, SP.loginame 
FROM sys.dm_tran_locks DTL  
   INNER JOIN sys.sysprocesses SP  
       ON DTL.request_session_id = SP.spid   
   --INNER JOIN sys.[dm_exec_requests] AS SDER ON SP.[spid] = [SDER].[session_id] 
   CROSS APPLY sys.dm_exec_sql_text(SP.sql_handle) AS DEST  
WHERE SP.dbid = DB_ID()  
   AND DTL.[resource_type] <> 'DATABASE' 
ORDER BY DTL.[request_session_id];

It lists the queries which are being blocked and it also tells you on which queries they are blocked (see columns spid and blocked).

Eventually, I solved the issue by handling the transaction at bean level (my bean was container managed and update / select queries where in the same bean). Before the update, I manually started a transaction. After the update was successful, I committed the transaction. Finally, I got a resultdata (select query) - without any transaction, as I didn't need a transaction for this query.

Creating a TIF file with a resolution of X ppi from a PDFBox PDPage

Submitted by Jochus on Sun, 25/11/2012 - 22:54 | Posted in: Java
Posted in

In order to have the correct ppi in the TIF file, you need to specify IIOMetadata for the TIF file and pass it to the constructor of the IIOImage.
In the example below, you can see the creation of this metadata in the function: createMetadata()

PDDocument document = null;
try {
	// Set main options
	int imageType = BufferedImage.TYPE_BYTE_BINARY;
	int resolution = X;
	int maxPage = Integer.MAX_VALUE;
	// Create TIFF writer
	ImageWriter writer = null;
	ImageWriteParam writerParams = null;
	try {
		TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi();
		writer = tiffspi.createWriterInstance();
		writerParams = writer.getDefaultWriteParam();
		writerParams.setCompressionType("CCITT T.6");
	} catch (Exception ex) {
		throw new BusinessException("Could not load the TIFF writer", ex);
	// Create metadata for Java IO ImageWriter
	IIOMetadata metadata = createMetadata(writer, writerParams, resolution);
	// Load the pdf file
	document = PDDocument.load(file.getFile());
	List<PDPage> pages = (List<PDPage>) document.getDocumentCatalog().getAllPages();
	// Loop over the pages
	for (int i = 0; i < pages.size() && i <= maxPage; i++) {
		// Write page to image file
		File outputFile = null;
		ImageOutputStream ios = null;
		try {
			outputFile = new File(file.getFile().getCanonicalPath() + "." + i);
			ios = ImageIO.createImageOutputStream(outputFile);
			writer.write(null, new IIOImage(pages.get(i).convertToImage(imageType, resolution), null, metadata), writerParams);
		} catch (Exception ex) {
			throw new BusinessException("Could not write the TIFF file", ex);
		} finally {
		// do something with TIF file
} catch (Exception ex) {
	throw new BusinessException("Error while converting PDF to TIFF", ex);
} finally {
	try {
		if (document != null) {
	} catch (Exception ex) {
		log.error("Error while closing PDF document", ex);
// Internal methods
private IIOMetadata createMetadata(ImageWriter writer, ImageWriteParam writerParams, int resolution) throws IIOInvalidTreeException {
	// Get default metadata from writer
	ImageTypeSpecifier type = writerParams.getDestinationType();
	IIOMetadata meta = writer.getDefaultImageMetadata(type, writerParams);
	// Convert default metadata to TIFF metadata
	TIFFDirectory dir = TIFFDirectory.createFromMetadata(meta);
	// Get {X,Y} resolution tags
	BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
	TIFFTag tagXRes = base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION);
	TIFFTag tagYRes = base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION);
	// Create {X,Y} resolution fields
	TIFFField fieldXRes = new TIFFField(tagXRes, TIFFTag.TIFF_RATIONAL, 1, new long[][] { { resolution, 1 } });
	TIFFField fieldYRes = new TIFFField(tagYRes, TIFFTag.TIFF_RATIONAL, 1, new long[][] { { resolution, 1 } });
	// Add {X,Y} resolution fields to TIFFDirectory
	// Return TIFF metadata so it can be picked up by the IIOImage
	return dir.getAsMetadata();

JRebel constantly reloading every class file and every configuration file in our Java project

Submitted by Jochus on Mon, 19/11/2012 - 23:04 | Posted in: Java
Posted in

We are using JRebel at work now for a couple of weeks, and this product is really a timesaver for every Java developer. No need to redeploy/restart your application server: just code and push refresh in the browser.

But, we sometimes had the issue the whole project was getting reloaded and we only changed 1 line of code in 1 class file. After a bit of research, we found out Eclipse just cleans the whole bin folder. Every class file was recompiled.

The reason why is still unknown. I couldn't figure this out. Anyways, it was really frustrating as the only solution to stop the reloading process, was to restart the application server. And if you have to restart the application server, why would you use JRebel?

The first thing I tried is stopping the "Automatic build" option in Eclipse. Disabling build automatically option does not break JRebel. JRebel depends on the results of the compilation, so if you disable the automatic build you will have to just compile the changed classes yourself, that's it.

But I didn't like that option either, so we found an option in JRebel which is actually a VM argument: -Drebel.check_class_hash=true which will check the checksum before reloading the classes. It may slow down the reloading time from a few milliseconds to some milliseconds extra. But it is still better than the situation when all the project classes have to be reloaded.

To summarize, this is my JRebel configuration in $JBOSS_HOME/bin/run.conf:

# JRebel
JAVA_OPTS="$JAVA_OPTS -javaagent:$JREBEL_HOME/jrebel.jar -Drebel.log=false -Drebel.check_class_hash=true"
JAVA_OPTS="$JAVA_OPTS -D##PRODUCT_NAME##.root=/Users/jochen/Documents/workspace/##PRODUCT_NAME##"

CSS3 spin animation

Submitted by Jochus on Tue, 13/11/2012 - 16:56 | Posted in: Website
Posted in

I just learned this cool CSS3 animation on Devoxx :-)! Hover the Chrome logo!
Not sure it's gonna work on Internet Explorer ... :p ...



Logging all queries executed on MySQL database

Submitted by Jochus on Mon, 12/11/2012 - 23:23 | Posted in: Database
Posted in

I wanted to debug some SQL queries which are coming from my data access layer (JPA with Hibernate) and which are being send to my MySQL server. The following steps are needed:

  • Create the file /etc/my.cnf (I installed MySQL manually from a .pkg installer, so the file is not there by default - you have to create it manually)
  • Add the following lines:

  • Create the .log file:
    $ sudo touch /tmp/mysql-query.log
  • Allow MySQL daemon to write in file:
    $ sudo chown _mysql /tmp/mysql-query.log
  • Restart MySQL daemon:
    ~ $ cd /usr/local/mysql/support-files/
    /usr/local/mysql/support-files $ sudo ./mysql.server restart