Apache Workmanager Parallel Processing Sample


import java.io.IOException;

import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import weblogic.work.ExecuteThread;

import commonj.work.Work;
import commonj.work.WorkManager;

/**
* Servlet implementation class TestWork
*/
public class TestWork extends HttpServlet {
private static final long serialVersionUID = 1L;

static boolean first,second,thrird,fouth,fifth=false;
/**
* @see HttpServlet#HttpServlet()
*/
public TestWork() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
long startTime = System.currentTimeMillis();

try {
InitialContext ic = new InitialContext();
System.out.println(“## [servlet] executing in: ”
+ ((ExecuteThread) Thread.currentThread()).getWorkManager()
.getName());
WorkManager wm = (WorkManager) ic
.lookup(“java:comp/env/wm/MyWorkManager”);

wm.schedule(new Work(){
public void run() {
System.out.println(“fist Job”);
new FirstJob().doMyJob();
}
public void release() {

}

public boolean isDaemon() {
System.out.println(“is damon”);
return false;
}
});

wm.schedule(new Work(){
public void run() {
System.out.println(“Second Job”);
new SecondJob().doMyJob();
}
public void release() {

}

public boolean isDaemon() {;
return false;
}
});

wm.schedule(new Work(){
public void run() {
System.out.println(“Third Job”);
new ThirdJob().doMyJob();
}
public void release() {

}

public boolean isDaemon() {
return false;
}
});

wm.schedule(new Work(){
public void run() {
new FourthJob().doMyJob();
}
public void release() {

}

public boolean isDaemon() {
return false;
}
});

wm.schedule(new Work(){
public void run() {
System.out.println(“Fifth Job”);
new FifthJob().doMyJob();
}
public void release() {

}

public boolean isDaemon() {
System.out.println(“is damon”);
return false;
}
});

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

int i=1;
System.out.println(“”+first+ second + thrird+fouth + fifth);
while (!(first && second && thrird && fouth && fifth)){
try {
System.out.println(“inside while”);
Thread.sleep(1000);

if(i>30) //break the wait for checking the job execution after 30 sec
break;
} catch (Exception e) {
e.printStackTrace();
}
}
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime – startTime;
System.out.println(“Total time taken “+elapsedTime/1000 +” Seconds”);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}

import java.util.Date;

public class FirstJob {

public void doMyJob() {

System.out
.println(FirstJob.class + ” Startting my job at ” + new Date());
try {
Thread.sleep(5000);
TestWork.first = true;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(FirstJob.class + ” End my job at ” + new Date());
}

}

import java.util.Date;

public class SecondJob {

public void doMyJob(){

System.out.println(SecondJob.class +” Startting my job at “+new Date());
try {
Thread.sleep(5000);
TestWork.second = true;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(SecondJob.class +” End my job at “+new Date());
}
}

import java.util.Date;

public class ThirdJob {
public void doMyJob(){

System.out.println(ThirdJob.class +” Startting my job at “+new Date());
try {
Thread.sleep(5000);
TestWork.thrird = true;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(ThirdJob.class +” End my job at “+new Date());
}
}

import java.util.Date;

public class FourthJob {

public void doMyJob(){

System.out.println(FourthJob.class +” Startting my job at “+new Date());
try {
Thread.sleep(5000);
TestWork.fouth = true;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(FourthJob.class +” End my job at “+new Date());
}

}

import java.util.Date;

public class FifthJob {

public void doMyJob(){

System.out.println(FifthJob.class +” Startting my job at “+new Date());
try {
Thread.sleep(5000);
TestWork.fifth = true;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(FifthJob.class +” End my job at “+new Date());
}

}

web.xml


<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns=”http://java.sun.com/xml/ns/javaee&#8221; xmlns:web=”http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221; xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221; id=”WebApp_ID” version=”2.5″>
<display-name>testworkmgr</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>TestWork</display-name>
<servlet-name>TestWork</servlet-name>
<servlet-class>com.test.TestWork</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestWork</servlet-name>
<url-pattern>/TestWork</url-pattern>
</servlet-mapping>

<resource-ref>
<res-ref-name>wm/MyWorkManager</res-ref-name>
<res-type>commonj.work.WorkManager</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>

Advertisements

About Praveen Orathel

I am a java web application developer.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s