Tuesday, July 12, 2011
Wednesday, June 15, 2011
JVM shutdown hook (Surviving abrupt shotdown)
Project I am working currently requires the ability to detect shutdown on JVM. Its basically required to be detected so that active connections to the JMS server to be destroyed. I wrote code to destroy connection on ServletContextListener which worked fine for some scenario, but failed if I kill web server processes explicitly.
The solution is a little known feature of the language that lets you register JVM shut down hooks. A shut down hook is simply a Thread that is registered with the JVM and run just before the JVM shuts down.
Here is code snippet to register shutdown hook with JVM:
The solution is a little known feature of the language that lets you register JVM shut down hooks. A shut down hook is simply a Thread that is registered with the JVM and run just before the JVM shuts down.
Here is code snippet to register shutdown hook with JVM:
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
// your code here which needs be executed before shutdown
}
});
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
try
{
Thread.sleep(10000);
}
catch (InterruptedException ex)
{
}
// halt will bail out without calling further shutdown hooks or
// finalizers
Runtime.getRuntime().halt(1);
}
});
Monday, June 13, 2011
Database encryption using Jasypt (Java Simplified Encryption)
Here are required jars for using Jasypt
commons-lang
commons-codec
jasypt
Here is example of string encryptor with Spring and Hibernate.
Define encryptor in spring configuration file:
PBEWithSHA1AndDESede jasypt 4 strongHibernateStringEncryptor
In hibernate mapping file define type as follows:
@TypeDef(
name="encryptedString",
typeClass=EncryptedStringType.class,
parameters={@Parameter(name="encryptorRegisteredName",
value="strongHibernateStringEncryptor")}
)
And then specify type as encryptedString for the columns you want to encrypt.
e.g.
@Column(name = "QO_OPTION", length = 4000, nullable = false) @Type(type="encryptedString") private String optionText;
Here hibernate will take care of encrypting while saving string to database and decrypting while loading object from database.
Subscribe to:
Comments (Atom)