Friday, July 13, 2012

Where is my cached object ? SpyMemcached client & Memcached example

A simple example which uses spymemcached client against to the memcache server.
Memcached test scenario-1


Install multiple memcache server on the different servers : 127.0.0.1 and 172.28.138.184.
Create a client using these hosts and cache some object with a timeout.
Then check whether is your object cached in server 1 or server 2 ??

Installation guide for ubuntu 
Download spymemcached from here .

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import net.spy.memcached.MemcachedClient;

public class TestScenario1 {

public static void main(String[] args) {
@SuppressWarnings("unchecked")
//compose all servers list
List<InetSocketAddress> allServers = new ArrayList<InetSocketAddress> ();
allServers.add(new InetSocketAddress("127.0.0.1", 11211));//server1 installed on localhost
allServers.add(new InetSocketAddress("172.28.138.184", 11211));// this is server 2 installed on another host


try {
//create a connection against to the all servers
final MemcachedClient memcacheClient2AllServers = createMemcacheClient(allServers);
//create an object and tell memcache to cache it on one of the servers which are given as a list
cacheAnObject(memcacheClient2AllServers);
// now we dont know where the object is actually cached ?? In server 1 or in server 2
// so i will check them in parellel with different threads

//create a connection against to the fist server only
final MemcachedClient memcacheClient2FirstServer = createMemcacheClient(allServers.subList(0, 1));
//create a connection against to the fist second server only
final MemcachedClient memcacheClient2SecondServer = createMemcacheClient(allServers.subList(1, 2));

//Check the cache from all clients by different threads
Thread thread4AllServers = createMemCacheCheckThread(memcacheClient2AllServers,"[1 & 2]");
Thread thread4FirstServerOnly = createMemCacheCheckThread(memcacheClient2FirstServer,"[1]");
Thread thread4SecondServerOnly = createMemCacheCheckThread(memcacheClient2SecondServer,"[2]");

thread4AllServers.start();
thread4FirstServerOnly.start();
thread4SecondServerOnly.start();

thread4AllServers.join();
thread4FirstServerOnly.join();
thread4SecondServerOnly.join();

} catch (Exception e) {
e.printStackTrace();
}
}

private static MemcachedClient createMemcacheClient(List<InetSocketAddress> allServers) throws IOException {
final MemcachedClient memcacheClient2AllServers = new MemcachedClient(allServers); ;
return memcacheClient2AllServers;
}

private static void cacheAnObject(final MemcachedClient memcacheClient2AllServers) {
SomeObject someObject = new SomeObject();
someObject.someField = "serkan sunel";
memcacheClient2AllServers.set("someKey", 60, someObject);
}

private static Thread createMemCacheCheckThread(final MemcachedClient memcacheClient, final String threadName) {
Thread thread = new Thread(){
@Override
public void run() {
while (true) {
try {
checkCache();
sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
private void checkCache() {

SomeObject myObject=null;
Future<Object> f=memcacheClient.asyncGet("someKey");
try {
myObject=(SomeObject) f.get(5, TimeUnit.SECONDS);
if(myObject != null){
System.out.println("Value from cached object :" + myObject.someField + " thread :{"+Thread.currentThread().getName()+"}");
}else{
System.out.println("Cache is sweeped ! : thread :{"+Thread.currentThread().getName()+"}");
}
} catch(TimeoutException e) {
   // Since we don't need this, go ahead and cancel the operation.  This
   // is not strictly necessary, but it'll save some work on the server.
   f.cancel(false);
   // Do other timeout related stuff
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.setName(threadName);
return thread;
}

}

Thursday, July 5, 2012

URLClassLoader and reloading a class from the web example


Reload jar from remote example
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class UrlJarReloader {

public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
while (true) {
reloadJarFile();
}
}

private void reloadJarFile() {
try {
System.out.println(" -- Reloading the jar file from the web !");
   String classToLoad = "org.apache.commons.lang3.StringUtils";

   URL jarUrl = new URL("jar:http://archiva/archiva/repository/internal/org/apache/commons/commons-lang3/3.1/commons-lang3-3.1.jar!/");
   URLClassLoader cl = new URLClassLoader(new URL[] {jarUrl}, null);
   Class loadedClass = cl.loadClass(classToLoad);
   
   Method method = loadedClass.getMethod("trim", new Class[] {java.lang.String.class});
   Object reloadMe = loadedClass.getConstructor().newInstance();
   String trimmedString = (String) method.invoke(reloadMe, new Object[]{"           1 2 3 4 5 6          "});
System.out.println(" -- Trimmed string is : {" + trimmedString + "}");
} catch (Exception e) {
e.printStackTrace();
}
}
};
startReloader(r);
}

private static void startReloader(Runnable r) {
try {
Thread reloader = new Thread(r);
reloader.start();
reloader.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Tuesday, July 3, 2012

Hot Deploy Example

Can be used to build a generic task-execution engine that can be used to load the code supplied by any remote client.


The most important point is to correctly set the parent class loader which is null here .
note: with windows custom urlClassLoader is necessary which is overrides the close method of the base class  (due to a jvm bug{may be fixed in jdk7})


import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;


import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;


public class JarReloader {


public static void main(String[] args) {

Runnable r = new Runnable() {

@Override
public void run() {
while (true) {
reloadJarFile();
watchDirectory();
}
}


private void reloadJarFile() {
try {
System.out.println("Reloading the jar file !");
URLClassLoader cl = null;
   File file = new File("/home/ssunel/jars/reload.jar");
   String classToLoad = "com.freetime.enjoy.ReloadMe";
   URL jarUrl = new URL("file:" + file.getAbsolutePath());
   cl = new URLClassLoader(new URL[] {jarUrl}, null);
   Class loadedClass = cl.loadClass(classToLoad);
   Method method = loadedClass.getMethod("giveAMessage", new Class[] {});
   Object reloadMe = loadedClass.getConstructor().newInstance();
   method.invoke(reloadMe, null);
} catch (Exception e) {
e.printStackTrace();
}
}


private void watchDirectory() {
try {
Path dir = Paths.get("/home/ssunel/jars");
WatchService watchService = FileSystems.getDefault().newWatchService();
dir.register(watchService, ENTRY_CREATE, ENTRY_MODIFY);
WatchKey watchKey = watchService.take();
for (WatchEvent<?> event : watchKey.pollEvents()) {
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path name = ev.context();
Path child = dir.resolve(name);
System.out.format("%s: %s\n", event.kind().name(), child);
}
Thread.sleep(1000L);
       } catch (Exception x) {
           return;
       }
}


};

startReloader(r);

}


private static void startReloader(Runnable r) {
try {
Thread reloader = new Thread(r);
reloader.start();
reloader.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}


package com.freetime.enjoy;

public class ReloadMe {
private final static String someMessage = "HELLO WORLD AGAIN";
public static void giveAMessage (){
System.out.println(someMessage);
}
}

Directory Change Listener Example


jdk 7 Watch Service Sample Usage
The java.nio.file package provides a file change notification API, called the Watch Service API. This API enables you to register a directory (or directories) with the watch service. When registering, you tell the service which types of events you are interested in: file creation, file deletion, or file modification. When the service detects an event of interest, it is forwarded to the registered process. The registered process has a thread (or a pool of threads) dedicated to watching for any events it has registered for. When an event comes in, it is handled as needed.



import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;


public class DirListener {


public static void main(final String[] args) {
try {
Runnable r = new Runnable() {
@Override
public void run() {
while (true) {
handleDirectoryChangeEvent(args[0]);
}
}
};


Thread t = new Thread(r);
t.start();
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}


private static void handleDirectoryChangeEvent(String path) {
try {
Path dir = Paths.get(path);
WatchService watchService = FileSystems.getDefault().newWatchService();
dir.register(watchService, ENTRY_CREATE, ENTRY_MODIFY);
WatchKey watchKey = watchService.take();
for (WatchEvent<?> event : watchKey.pollEvents()) {
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path name = ev.context();
Path child = dir.resolve(name);
System.out.format("%s: %s\n", event.kind().name(), child);
}
        } catch (Exception x) {
            return;
        }
}
}



program output...



ENTRY_CREATE: /home/ssunel/jars/reloadtes1.jar
ENTRY_CREATE: /home/ssunel/jars/reloadtest.jar
ENTRY_CREATE: /home/ssunel/jars/zi3iCChW
ENTRY_CREATE: /home/ssunel/jars/Untitled Document
ENTRY_CREATE: /home/ssunel/jars/ss.txt
ENTRY_CREATE: /home/ssunel/jars/.goutputstream-RVZAHW
ENTRY_MODIFY: /home/ssunel/jars/.goutputstream-RVZAHW
ENTRY_MODIFY: /home/ssunel/jars/.goutputstream-RVZAHW
ENTRY_CREATE: /home/ssunel/jars/ss.txt~
ENTRY_MODIFY: /home/ssunel/jars/reload.jar