Sunday, December 15, 2013

Connect to remote db with SQLPLUS

sqlplus usernamee/passwordd@'(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=123.123.123.123)(PORT=1521)))(CONNECT_DATA=(SID=MY_REMOTE_DB_SID)))'

sqlplus usernamee/passwordd@'(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=123.123.123.123)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=MY_REMOTE_DB_SERVICE_NAME)))'

Tuesday, December 10, 2013

MongoDB export import example

EXPORT EXAMPLE

root@ubuntu:/var# mongodump --db test
connected to: 127.0.0.1
DATABASE: test to dump/test
test.system.indexes to dump/test/system.indexes.bson
7 objects
test.profiles to dump/test/profiles.bson
8 objects
test.fs.chunks to dump/test/fs.chunks.bson
3 objects
test.fs.files to dump/test/fs.files.bson
3 objects
test.mys to dump/test/mys.bson
1 objects
test.myContents to dump/test/myContents.bson
95 objects
test.yourContents to dump/test/yourContents.bson
23 objects


IMPORT EXAMPLE

root@ubuntu:/var# mongorestore --db test dump/test
connected to: 127.0.0.1
dump/test/system.indexes.bson
going into namespace [test.system.indexes]
7 objects
dump/test/yourContents.bson
going into namespace [test.yourContents]
23 objects
dump/test/fs.files.bson
going into namespace [test.fs.files]
3 objects
dump/test/myContents.bson
going into namespace [test.myContents]
95 objects
dump/test/fs.chunks.bson
going into namespace [test.fs.chunks]
3 objects
dump/test/profiles.bson
going into namespace [test.profiles]
8 objects
dump/test/mys.bson
going into namespace [test.mys]
1 objects
root@ubuntu:/var#

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);
}
}