maandag 15 april 2013

ImageMagick, jMagick and Glassfish on a EC2 machine


Recently I had to roll out a REST-Webservice on Glassfish which used jMagick as a library. Now, ImageMagick is a C++ library and is available in Java through JNI. The installation on a standard EC2-Ubuntu AMI is pretty straightforward once you know the tricks. I executed the following steps to get things up and running:
  1. Make an EC2-Instance, attach an EBS and install JDK7 and GFv3.2.1 (these are the version which I installed)
     
  2. Install ImageMagick using sudo yum install ImageMagick. You can't use apt-get or aptitude since Amazon prefers yum.
     
  3. Next thing is to install jMagick. You can download the latest from ftp://ftp.imagemagick.org/pub/ImageMagick/java/. Use wget to get the RPM (32 or 64 bit - I used the 64bit version).
    wget ftp://ftp.imagemagick.org/pub/ImageMagick/java/jmagick-6.4.0-3.x86_64.rpm
  4. Install the RPM using sudo yum  jmagick-6.4.0-3.x86_64.rpm
     
  5. When all goes well, you should be able to see a handful of Magick-files using ls /usr/lib64/libM* (which were installed in step 2) and of course the Java library ls /usr/lib64/libJMagick.so
  6. There should also be an accompagning JAR file for the library. Check if /usr/lib64/jmagick-6.4.0.jar is there.
     
  7. Now we get to the Glassfish part. Suppose your domain is called "domain1". Copy the JAR into the /lib directory of your domain so it will get loaded during the startup of Glassfish.
     
  8. Point your browser to the admin-console or open the domain.xml. You need to add a JVM-parameter to get this running. In the console go to the configurations / server-config / JVM-settings and then the JVM-options tab and add the following line:

    -Djmagick.systemclassloader=no


    or add in the domain.xml the line

    <jvm-options>-Djmagick.systemclassloader=no</jvm-options>
    This line prevents jMagick of using the system-class-loader as you might have expected. Not adding this line leads to errors.
     
  9. Restart your domain and deploy the application. The System.loadLibrary("JMagick"); in Magick.java runs as planned. (http://jmagick.svn.sourceforge.net/viewvc/jmagick/trunk/src/magick/Magick.java?revision=91&view=markup
We can now redeploy our application in Glassfish at will because the class is loaded by Glassfish during the startup. When not, you can expect exception when redeployen when your jMagick.JAR is in your project. The classloader will tell you that the classes are already loaded when you deploy a second time.
PS: when running Maven tests outside the container, do not forget to specifiy the -Djava.library.path=/your-path-to/libJMagick.so. 
PS2: if you install JMagick using a repository, then the JMagick.so files are stored in the /usr/lib64/jni directory. Copy the libJMagick.so file to /usr/lib64 and restart your domain. There is no need to add java.library.path to the JVM parameters in the domain.xml (something which apparently doesn't function well).

Geen opmerkingen:

Een reactie posten