Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Newbie Help with Thread problem

Reply
Thread Tools

Newbie Help with Thread problem

 
 
Mikael Petterson
Guest
Posts: n/a
 
      08-28-2003
Hi,

I have a Jsp-page which creates only one ( just one) instance of my
SMSModule class. The jsp-page also creates a thread in my SMSModule to
send the sms I have in a queue. However it seems like there will only be
one thread created ---> only one sms will be sent. The second time there
will be not new thread to handle the next sms.

Any ideas? All help is very much appreciated!

//Mikael

Here is my jsp code:

<%@page import="com.lightlabs.teaching.help.lab3.*"%><%
//Declare
long delayMillis = 10000; // 10 seconds
//Get params from form
String originator = request.getParameter("originator");
String body = request.getParameter("body");
String destination = request.getParameter("destination");
//Set xml path for sms-c parameter file
String pathToXml = application.getRealPath("send.jsp");
System.out.println("Path to xml is "+pathToXml);
SMSModule myModule = SMSModule.getInstance();
//Get parameters for a certain SMSC
SMSProperties props = new SMSProperties();
props.setSmscProperties("msip2");

//Get smsc properties
try{

myModule.setHost(props.getHost());
myModule.setPort(Integer.parseInt(props.getPort()) );
myModule.setUserid(props.getUserid());
myModule.setPasswd(props.getPasswd());
myModule.setProtocol(Integer.parseInt(props.getPro tocol()));
//Execute thread into waiting state
myModule.start();
System.out.println("originator is: "+originator);
System.out.println("body is: "+body);
System.out.println("destination is: "+destination);
Sms sms = new Sms (originator, body, destination);
System.out.println("Sending sms to queue");
//Send sms to queue. This will trigger thread to handle sms.
QueueHandler.queue.add(sms);


System.out.println("now waiting...");
try{
Thread.sleep(5000);

}catch(Exception e){}

try {
myModule.join(delayMillis);

if (myModule.isAlive()) {
System.out.println("Timeout occurred; thread has not
finished");
} else {
System.out.println("Finished");
}
} catch (InterruptedException e) {
// Thread was interrupted
}

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



%><jsp:forward page="index.jsp" />

Here is the code for my SMSModule:
==================================

package com.lightlabs.teaching.help.lab3;


/**
* Exempel / Inspiration / 'Komma ig�ng'-kod till hur man kan g�ra en
* SMS-modul som klarar flera SMS-protokoll och kan hantera inkommande och
* utg�ende SMS-trafik. Den anv�nder sig av
Singleton-designm�nstret s�
* att man kan komma �t den fr�n andra h�ll i sin applikation.
*
* Mycket kan g�ras mer utf�rligt och genomt�nkt, men detta �r
som sagt bara t�nkt
* utg�ra en mall och n�got att utg� ifr�n.
*
* @author Mathias af Jochnick, Copyright (c) 2002 Lightlabs.
* Modified by Mikael Petterson 2003-03-03
*/
public class SMSModule extends Thread{

public static final int MSIP_PROTOCOL=1;
public static final int SMPP_PROTOCOL=2;

private static SMSModule instance;

//Properties
private String protocol = null;
private String host = null;
private int port = 0;
private String userid = null;
private String passwd = null;


//SMS
private Sms smsCurrent = null;
private Sms smsOldest = null;
private Sms smsFailed = null;

private QueueHandler qh;
private int protocolType;
private SMSProtocolHandler myProtocolHandler;


private SMSModule(){
//Create a queue
qh = new QueueHandler();

}

public static SMSModule getInstance(){
if(instance == null){
instance = new SMSModule();
}else{
System.out.println("Already have one instance.");
}
return instance;
}

public void run (){
while(true){

//Check queue
if(!qh.empty()){
System.out.println("Queue is not empty sending.....");
send();
}
System.out.println("Exiting thread");
return;
}
}


public void setProtocol(int protocolType){
this.protocolType=protocolType;
}

public void connect(String ip,
int port,
String username,
String password)
throws Exception{
System.out.println(getClass()+":connect()");

if(myProtocolHandler!=null)
throw new Exception("SMS Module already connected!");
switch(protocolType){
case MSIP_PROTOCOL:
myProtocolHandler=new MSIPProtocolHandler();
break;
// case SMPP_PROTOCOL:
// myProtocolHandler=new SMPPProtocolHandler();
// break;
default:
throw new Exception("Protocoltype "+protocolType+"
unknown or not set.");
}
myProtocolHandler.connect(ip,port,username,passwor d);
}

public void disconnect() throws Exception{
System.out.println(getClass()+":disconnect()");
if(myProtocolHandler==null){
throw new Exception("SMS Module not connected!");
}else{
myProtocolHandler.disconnect();
myProtocolHandler=null;
}

}

public boolean isLoggedIn(){
if(myProtocolHandler==null){
return false;
}else {
return myProtocolHandler.isLoggedIn();
}
}

public void sendSMS(){
if(isLoggedIn()){
//We have connection to SMSC
//Get sms from queue
smsCurrent =(Sms)qh.get();
//Get originator, body and destination
String from = smsCurrent.getOriginator();
String message = smsCurrent.getBody();
String to = smsCurrent.getDestination();
//send SMS
myProtocolHandler.sendSMS(from, message,to);
debug("Waiting for response from SMSC");
try{
Thread.sleep(5000);
}catch(Exception e){}
if(myProtocolHandler.getMessageSent()){
debug("Message has been sent");
myProtocolHandler.setMessageSent(false);
//Remove message from queue
qh.remove();
}else{
debug("Message not successfully sent");
//Put message back in queue
qh.add(smsCurrent);
}
}else{
System.err.println("There is no connection");
}


}

public void setHost(String host){
this.host = host;
}

public void setPort(int port){
this.port = port;
}

public void setUserid(String userid){
this.userid = userid;
}

public void setPasswd(String passwd){
this.passwd = passwd;
}

private void send(){
System.out.println(getClass()+":send()");
try {
if(!isLoggedIn()){
connect(host,port,userid,passwd);
}
try{
Thread.sleep(5000);

}catch(Exception e){}
sendSMS();
try{
Thread.sleep(5000);

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




/* Debug support */
private static boolean debugOn = true;
private static void debug(String msg){
if(debugOn)
System.out.println("\nCoolFlix log: "+msg+"\n");

}



}

 
Reply With Quote
 
 
 
 
Kevin Hooke
Guest
Posts: n/a
 
      08-29-2003
From a quick glance at your code, I would say you only ever get one thread
started because your SMSModule subclass of Thread you have implemented as a
singleton.

By doing so you are only ever going to have one instance of this class, and
therefore 1 thread - if you can not create more than one instance of your
Thread subclass it will be impossible to have more than 1 thread!

Just as a side comment, you may want to change your instantiation of your
singleton class (if you decide you still need to do this) to something like
this:

private static SMSModule instance = new SMSModule();

public static SMSModule getInstance()
{
return this.instance;
}

I think you will find this is the safest threadsafe approach to implementing
a singleton class ( you can find plenty more info about this if you google
for 'java singleton') - but bonus points for avoiding the common Java
singleton pitfall of trying to implement 'double checked locking' (you can
search for more about that too if you have never heard of that approach).

I have an article about DCL on my site too at:
http://www.kevinhooke.com/code_examp...on_example.jsp

--
Kevin Hooke

MindBeans Software Consulting
http://www.mindbeans.net


 
Reply With Quote
 
 
 
 
Harald Hein
Guest
Posts: n/a
 
      08-29-2003
"Mikael Petterson" wrote:

> However it seems like there will only be
> one thread created ---> only one sms will be sent


Because you only start one thread. Where should the others come from?
The VM guessing your mind?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem mutex-thread "Unlocking mutex owned by another thread ???" NaeiKinDus C++ 3 04-15-2007 09:35 PM
problem mutex-thread "Unlocking mutex owned by another thread ???" NaeiKinDus C++ 1 04-14-2007 07:40 PM
Newbie:Using ASP.NET thread pool thread to dispatch TCP data, etc. Navin Mishra ASP .Net Web Services 0 03-22-2005 06:45 PM
Problem in deleting the dynamic memory in a parent thread that is allocated from a child thread Manisha C++ 2 09-13-2004 05:48 PM
Newbie: Thread myThread = Thread.currentThread(); ???? Zalek Bloom Java 14 11-21-2003 09:47 PM



Advertisments