Saturday, July 23, 2011

checking long running task and timing out using java thread


public boolean checkInBackground() throws InterruptedException {
final StringBuffer st = new StringBuffer();
Thread thread = new Thread() {
public void run() {
try {
check();
st.append("done");
} catch (Exception e) {
e.printStackTrace();
}
}

public void check() {
//long running task
while(true) {}
};
};
thread.setDaemon(true);
thread.start();
thread.join(2 * 1000);
if (st.toString().equals("done")) {
return true;
}
return false;
}

check long running task status using callable, future, ExecutorService in java


public boolean checkInBackground() {
ExecutorService pool = Executors.newFixedThreadPool(1);
Callable callable = new Callable() {
@Override
public Boolean call() throws Exception {
check();
return Boolean.TRUE;
}
};
final Future future = pool.submit(callable);
pool.shutdown();
Boolean bool = Boolean.FALSE;
try {
bool = future.get(1, TimeUnit.SECONDS);
} catch (Exception e) {
future.cancel(true);
bool = Boolean.FALSE;
}
return bool;
}
public boolean check() {
//long running task
return false;
}

Friday, July 22, 2011

eclipselink refresh cache

If JPA 2.0 is used, this can be done using the JPA Cache interface:

em.getEntityManagerFactory().getCache().evictAll();

In JPA 1.0 this can be done using the EclipseLink IdentityMapAccessor:

((JpaEntityManager)em.getDelegate()).getServerSession().getIdentityMapAccessor().invalidateAll();

Source: http://wiki.eclipse.org/EclipseLink/Examples/JPA/Caching

Thursday, July 21, 2011

Encrypt Decrypt in java


import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EncryptDecrypt {
String passPhrase = "";

public EncryptDecrypt() {
}

public EncryptDecrypt(String passPhrase) {
super();
this.passPhrase = passPhrase;
}

public String decrypt(String encrypted) throws Exception {
DESKeySpec desKeySpec = new DESKeySpec(passPhrase.getBytes("UTF8"));
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] encrypedBytes = base64decoder.decodeBuffer(encrypted);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = (cipher.doFinal(encrypedBytes));
String decrypted = new String(decryptedBytes);
return decrypted;
}

public String encrypt(String encrypt) throws Exception {
DESKeySpec desKeySpec = new DESKeySpec(passPhrase.getBytes("UTF8"));
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
BASE64Encoder base64encoder = new BASE64Encoder();
byte[] bytes = encrypt.getBytes("UTF8");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
String encryped = base64encoder.encode(cipher.doFinal(bytes));
return encryped;
}

public String getPassPhrase() {
return passPhrase;
}

public void setPassPhrase(String passPhrase) {
this.passPhrase = passPhrase;
}
}