set something in the session bean from client (Multithreading)
hi thanks for your notice.
this is ma university's project.
this is what i realy supposed to do:
generate 5000 random integer in client and set them whithin a second in the server .
i defiened 10 stateless session bean in the server and each of them is supposed to be set 500 random integer num as i explained before that i tried to do that by 10 threads in my client that each thread has a for-loop with 500 loops that in each loop it sets a num.
so when i ran the client it took 6 secs to set them in the server .all threads finish their job in the same time !!! however when i ran just one of them it took 1 sec!!!
i tried it by a simple for with 5000 loops and it took 6 secs as well!!!
so i think its up to the container as you mentioned.i think the container puts all request from the client in a line then service them one by one...
if i could managed the container to cross more than one request at a same time then it would work.
or maybe there is another way to do that !!!
when this client is run it sets 500 random num by each thread and set them in each session bean that has been defined that it takes 6 sec.
so if comment all the threads in the listen() and just start one of them and set its loop to 5000 it also do the same!!!!
i want to decrease the time by multithreading or anything else.
all the session beans have a same body.i post one of them as sample:
so this ma client's code:
client:>>>>
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import javax.ejb.*;
public class clientTest1 {
bmpThread th;
MailSess obj;
MailSess2 obj2;
MailSess3 obj3;
MailSess4 obj4;
MailSess5 obj5;
MailSess6 obj6;
MailSess7 obj7;
MailSess8 obj8;
MailSess9 obj9;
MailSess10 obj10;
public clientTest1(){
try{
System.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
System.setProperty("java.naming.provider.url", "localhost:1099");
InitialContext ctx = new InitialContext();
MailSessHome ref = (MailSessHome) ctx.lookup("MailSess");
MailSess2Home ref2 = (MailSess2Home) ctx.lookup("MailSess2");
MailSess2Home ref3 = (MailSess2Home) ctx.lookup("MailSess3");
MailSess2Home ref4 = (MailSess2Home) ctx.lookup("MailSess4");
MailSess2Home ref5 = (MailSess2Home) ctx.lookup("MailSess5");
MailSess2Home ref6 = (MailSess2Home) ctx.lookup("MailSess6");
MailSess2Home ref7 = (MailSess2Home) ctx.lookup("MailSess7");
MailSess2Home ref8 = (MailSess2Home) ctx.lookup("MailSess8");
MailSess2Home ref9 = (MailSess2Home) ctx.lookup("MailSess9");
MailSess2Home ref10 = (MailSess2Home) ctx.lookup("MailSess10");
MailSessHome home = (MailSessHome) PortableRemoteObject.narrow(ref,MailSessHome.class);
MailSess2Home home2 = (MailSess2Home) PortableRemoteObject.narrow(ref2,MailSess2Home.class);
MailSess3Home home3 = (MailSess3Home) PortableRemoteObject.narrow(ref3,MailSess3Home.class);
MailSess4Home home4 = (MailSess4Home) PortableRemoteObject.narrow(ref4,MailSess4Home.class);
MailSess5Home home5 = (MailSess5Home) PortableRemoteObject.narrow(ref5,MailSess5Home.class);
MailSess6Home home6 = (MailSess6Home) PortableRemoteObject.narrow(ref6,MailSess6Home.class);
MailSess7Home home7 = (MailSess7Home) PortableRemoteObject.narrow(ref7,MailSess7Home.class);
MailSess8Home home8 = (MailSess8Home) PortableRemoteObject.narrow(ref8,MailSess8Home.class);
MailSess9Home home9 = (MailSess9Home) PortableRemoteObject.narrow(ref9,MailSess9Home.class);
MailSess10Home home10 = (MailSess10Home) PortableRemoteObject.narrow(ref10,MailSess10Home.class);
obj = home.create();
obj2 = home2.create();
obj3 = home3.create();
obj4 = home4.create();
obj5 = home5.create();
obj6 = home6.create();
obj7 = home7.create();
obj8 = home8.create();
obj9 = home9.create();
obj10 = home10.create();
}catch(Exception e){System.out.println(e.getMessage());}
}
public static void main(String[] args) throws Exception{
clientTest1 clienttest1 = new clientTest1();
long time1=System.currentTimeMillis();
clienttest1.listen();
long time2=System.currentTimeMillis();
System.out.println("time="+(time2-time1));
}
public void listen(){
try{
}catch(Exception i){}
try{
th=new bmpThread(this);
Thread t=new Thread(th,"t");
t.start();
Thread t1=new Thread(th,"t1");
t1.start();
/* Thread t2=new Thread(th,"t2");
t2.start();
Thread t3=new Thread(th,"t3");
t3.start();
Thread t4=new Thread(th,"t4");
t4.start();
Thread t5=new Thread(th,"t5");
t5.start();
Thread t6=new Thread(th,"t6");
t6.start();
Thread t7=new Thread(th,"t7");
t7.start();
Thread t8=new Thread(th,"t8");
t8.start();
Thread t9=new Thread(th,"t9");
t9.start();*/
}catch(Exception yy){}
}
}
class bmpThread extends Thread {
clientTest1 client;
bmpThread(clientTest1 client){
this.client=client;
}
public void run(){
try{
long time1=System.currentTimeMillis();
if(currentThread().getName().equals("t")){
try{
for(int i=0;i<500;i++){
client.obj.setRand(Integer.parseInt(String.valueOf(Math.round(Math.random() * 24 + 1))));
}
}catch(Exception e){System.out.println(e);}
}
else if(currentThread().getName().equals("t1")){
try{
for(int i=0;i<500;i++){
client.obj2.setRand(Integer.parseInt(String.valueOf(Math.round(Math.random() * 24 + 1))));
}
}catch(Exception e){System.out.println(e);}
}
else if(currentThread().getName().equals("t2")){
try{
for(int i=0;i<500;i++){
client.obj3.setRand(Integer.parseInt(String.valueOf(Math.round(Math.random() * 24 + 1))));
}
}catch(Exception e){System.out.println(e);}
}
else if(currentThread().getName().equals("t3")){
try{
for(int i=0;i<500;i++){
client.obj4.setRand(Integer.parseInt(String.valueOf(Math.round(Math.random() * 24 + 1))));
}
}catch(Exception e){System.out.println(e);}
}
else if(currentThread().getName().equals("t4")){
try{
for(int i=0;i<500;i++){
client.obj5.setRand(Integer.parseInt(String.valueOf(Math.round(Math.random() * 24 + 1))));
}
}catch(Exception e){System.out.println(e);}
}
else if(currentThread().getName().equals("t5")){
try{
for(int i=0;i<500;i++){
client.obj6.setRand(Integer.parseInt(String.valueOf(Math.round(Math.random() * 24 + 1))));
}
}catch(Exception e){System.out.println(e);}
}
else if(currentThread().getName().equals("t6")){
try{
for(int i=0;i<500;i++){
client.obj7.setRand(Integer.parseInt(String.valueOf(Math.round(Math.random() * 24 + 1))));
}
}catch(Exception e){System.out.println(e);}
}
else if(currentThread().getName().equals("t7")){
try{
for(int i=0;i<500;i++){
client.obj8.setRand(Integer.parseInt(String.valueOf(Math.round(Math.random() * 24 + 1))));
}
}catch(Exception e){System.out.println(e);}
}
else if(currentThread().getName().equals("t8")){
try{
for(int i=0;i<500;i++){
client.obj9.setRand(Integer.parseInt(String.valueOf(Math.round(Math.random() * 24 + 1))));
}
}catch(Exception e){System.out.println(e);}
}
else if(currentThread().getName().equals("t9")){
try{
for(int i=0;i<500;i++){
client.obj10.setRand(Integer.parseInt(String.valueOf(Math.round(Math.random() * 24 + 1))));
}
}catch(Exception e){System.out.println(e);}
}
long time2=System.currentTimeMillis();
System.out.println("thread"+this.currentThread().getName().toString()+" thread time: "+(time2-time1));
}catch(Exception e){System.out.println(e.getMessage());}
}
}
//****************************************
Session bean remote:
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface MailSess extends EJBObject {
public void setRand(int rand) throws RemoteException;
public int getRand() throws RemoteException;
}
//****************************
Session bean home remote:
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
public interface MailSessHome extends EJBHome {
public MailSess create() throws CreateException, RemoteException;
}
//******************************
sessin bean bean:
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
public class MailSessBean implements SessionBean {
SessionContext sessionContext;
int rand;
static String randomNum="";
static int x=0;
public void ejbRemove() {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
public void setRand(int rand) {
this.rand = rand;
randomNum+=String.valueOf(this.getRand())+'*';
}
public int getRand() {
return rand;
}
public void ejbCreate() throws CreateException {
}
}