Uncategorized

Jboss Deployment Time Out Increase

Some of the web applications will take longer time to deploy than expected. By default JBoss has only 60 seconds deployment time out.

To Increase the JBoss time, set the following configuration value in standalone.xml in jboss 7. Deployment time out will be in milliseconds.

<subsystem xmlns=”urn:jboss:domain:deployment-scanner:1.1″>
<deployment-scanner path=”deployments” relative-to=”jboss.server.base.dir” scan-interval=”5000″ deployment-timeout=”3000″/>
</subsystem>

 

Uncategorized

Simple Java Project in eclipse using Maven

Follow below steps to create simple java project using maven.

  Download the latest eclipse

2.       Install maven from eclipse market place if it not available in eclipse.

 Maven Installation

3.       Create new Maven project using Maven wizard

Selct Maven Wizard

4.       Select quickstart archetype.

Quick Start

5.       Name the project and click finish

Name Maven Artifacts

6.       Done. Check the eclipse for newly created project.

Uncategorized

QuickStart Infinispan Cache

Infinispan is an easy to configure caching mechanism for java based applications. It can be used as  stand alone and cluster modes. It can be run in a server as well  as a simple java process.  The following content will describe using infinispan as a java process. And all the dependencies will be packed as a jar file to start the cache with the help of maven.

Prerequisites: 1. Download maven and set the path in system environment variables. 2. internet connection

Use below command to create a new java project using maven

mvn archetype:generate -DgroupId=com.infinispan -DartifactId=infinispan -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

1. Create a maven simple project with following settings

<groupId>com.infinispan</groupId>
    <artifactId>infinispan</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>infinispan</name>

Simple Java Project Using Eclipse and Maven

2. Add following dependencies in pom.xml

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<infinispan-version>5.3.0.Final</infinispan-version>
</properties>

<dependencies>
<!– Infinispan Dependencies –>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<version>5.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-client-hotrod</artifactId>
<version>5.3.0.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.infinispan.StartCache</mainClass>
<packageName>com.infinispan</packageName>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${project.url}</url>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>

</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.infinispan.StartCache</mainClass>
<packageName>com.infinispan</packageName>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${project.url}</url>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

3. Write a java class to start infinispan cache container

// Get the Cache Container
EmbeddedCacheManager cacheManager = new DefaultCacheManager(
“app-infinispan.xml”);
// Get the Cache Store.
Cache<Object, Object> generalStore = cacheManager
.getCache(“generalStore”);
// Put into cache store.
generalStore.put(“email”, “me@mymail.com”);
// retrieve from cache store.
System.out.println(“Fetched fromCache:” + generalStore.get(“email”)+” Cache Size:”+generalStore.size());

System.exit(0);

4. Go to the project base folder and build the project by maven command

> mvn clean compile assembly:assembly

5. Go to BaseDirectory/infinispan/target folder and run the jar as below,

> java -jar infinispan-0.0.1-SNAPSHOT-jar-with-dependencies.jar

this will start infinisapn cache container in the a java process.

6. To start cluster environment, run the above jar file from multiple command prompts.

Infinispan automatically discover and form the cluster with all cache containers.

7. Use the infinispan client to put and get the objects.

// Get the Cache Container
EmbeddedCacheManager cacheManager = new DefaultCacheManager(
“app-infinispan.xml”);
// Get the Cache Store.
Cache<Object, Object> generalStore = cacheManager
.getCache(“generalStore”);
// Put into cache store.
generalStore.put(“email”, “me@mymail.com”);
// retrieve from cache store.
System.out.println(“Fetched fromCache:” + generalStore.get(“email”)+” Cache Size:”+generalStore.size());

System.exit(0);

For an easy start, import the project into eclipse and use the code.

Download Project

For more information on infinispan, visit http://infinispan.org/

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