Uncategorized

Java Threads for Browser Hits

The following java program will open an URL repeatedly through a browser.

How it works?

1. Creates a constant thread pool of size 10.

2. 15 tasks will be created and handed over to executor service to execute these task.

3. all ten thread will be used to execute 15 tasks.

4. And shut down the thread service once all thread stop execution.

a. main class for execution

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @author kvn prabhu
*
*/

/**
*
* This class will repeatedly open a URL in the browser and will close after
* 5000ms.
*
*/
public class OpenBrowser {

static int size = 0;

public static void main(String[] args) {

openBrowser();
}

/**
* 1.This method will create a Thread Pool of 10 threads. 2. 15 workers will
* be created and handled over to execute service to run the threads.
*/
private static void openBrowser() {
// Thread Pool with 10 threads.
ExecutorService service = Executors.newFixedThreadPool(10);
// create the workers and hand over to thread service.
for (int i = 0; i < 15; i++) {

BrowserWorker worker = new BrowserWorker();
service.execute(worker);
}
// shut down the pool once all thread completed execution.
service.shutdown();
// Print all running threads at any given time.
while (!service.isTerminated()) {

Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

if (size != threadSet.size()) {

System.out.println(“All Running Threads:” + threadSet);
size = threadSet.size();
}

}

// Print after service termination.
while (service.isTerminated()) {
System.out.println(“Execution Completed.”);
break;
}

}

/**
*
*/

}

b. worker thread for executing a task.

/**
* This class will open 10 browsers on each run.
*
* @author kvn
*
*/
class BrowserWorker implements Runnable {

public void run() {

fire();

}

public void fire() {
for (int i = 0; i < 10; i++) {
try {
Process process = Runtime
.getRuntime()
.exec(“C:\\Program Files\\Internet Explorer\\iexplore.exe \”http://www.youtube.com/watch?v=V5Qk6oRsNmw&feature=youtu.be\””);
Thread.sleep(10000);
process.destroy();
//    System.out.println(“Status:” + process.waitFor());
} catch (Exception e) {
}
}
}
}

Result:

Note: Use any text editor to unwrap the content.

All Running Threads:[Thread[pool-1-thread-10,5,main], Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[pool-1-thread-7,5,main], Thread[pool-1-thread-6,5,main], Thread[pool-1-thread-1,5,main], Thread[Signal Dispatcher,9,system], Thread[pool-1-thread-9,5,main], Thread[Attach Listener,5,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-3,5,main], Thread[pool-1-thread-4,5,main], Thread[pool-1-thread-5,5,main], Thread[pool-1-thread-2,5,main]]
All Running Threads:[Thread[pool-1-thread-10,5,main], Thread[Finalizer,8,system], Thread[Reference Handler,10,system], Thread[pool-1-thread-7,5,main], Thread[pool-1-thread-6,5,main], Thread[pool-1-thread-1,5,main], Thread[Signal Dispatcher,9,system], Thread[pool-1-thread-9,5,main], Thread[Attach Listener,5,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-3,5,main], Thread[pool-1-thread-5,5,main], Thread[pool-1-thread-2,5,main]]
All Running Threads:[Thread[pool-1-thread-10,5,main], Thread[Finalizer,8,system], Thread[Reference Handler,10,system], Thread[pool-1-thread-7,5,main], Thread[pool-1-thread-6,5,main], Thread[pool-1-thread-1,5,main], Thread[Signal Dispatcher,9,system], Thread[Attach Listener,5,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-3,5,main], Thread[pool-1-thread-5,5,main], Thread[pool-1-thread-2,5,main]]
All Running Threads:[Thread[pool-1-thread-10,5,main], Thread[Attach Listener,5,system], Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-6,5,main], Thread[pool-1-thread-7,5,main], Thread[pool-1-thread-1,5,main], Thread[Signal Dispatcher,9,system], Thread[pool-1-thread-3,5,main], Thread[pool-1-thread-2,5,main]]
All Running Threads:[Thread[pool-1-thread-10,5,main], Thread[Attach Listener,5,system], Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-6,5,main], Thread[pool-1-thread-7,5,main], Thread[pool-1-thread-1,5,main], Thread[Signal Dispatcher,9,system], Thread[pool-1-thread-3,5,main]]
All Running Threads:[Thread[pool-1-thread-10,5,main], Thread[Attach Listener,5,system], Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-6,5,main], Thread[pool-1-thread-7,5,main], Thread[Signal Dispatcher,9,system], Thread[pool-1-thread-3,5,main]]
All Running Threads:[Thread[pool-1-thread-10,5,main], Thread[Attach Listener,5,system], Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-7,5,main], Thread[Signal Dispatcher,9,system], Thread[pool-1-thread-3,5,main]]
All Running Threads:[Thread[Attach Listener,5,system], Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-7,5,main], Thread[Signal Dispatcher,9,system], Thread[pool-1-thread-3,5,main]]
All Running Threads:[Thread[Attach Listener,5,system], Thread[Finalizer,8,system], Thread[Reference Handler,10,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main], Thread[pool-1-thread-7,5,], Thread[Signal Dispatcher,9,system]]
All Running Threads:[Thread[Signal Dispatcher,9,system], Thread[Attach Listener,5,system], Thread[Reference Handler,10,system], Thread[Finalizer,8,system], Thread[main,5,main], Thread[pool-1-thread-8,5,main]]
Execution Completed.

Uncategorized

Infinispan Caching with Hibernate

Infinispan is an easy to configure caching mechanism with hibernate.Its comes bundled with latest Jboss Application Server (JBoss 6 EAP, JBoss 7). We can also configure infinispan caching cluster independent of JBoss. Infinispan caching works very well as the L2 cache in hibernate. But Composite key entities in hibernate will not support caching in cluster environment. In the caching cluster environment, all the entities are constructed in one of the instance and  replicated or distributed in all other instances. Though the composite key entities are also replicated or distributed, but not the session factories (Composite keys will have a reference to originated session factory).  Hence replication of composite key entities will fail.

For a quick start in infinispan Getting Started Guide.

 

Caching mechanism doesn’t work with composite keys in Hibernate. We have to remove all composite keys