Newbie Help with Thread problem

Discussion in 'Java' started by Mikael Petterson, Aug 28, 2003.

  1. 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.getProtocol()));
    //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,password);
    }

    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");

    }



    }
     
    Mikael Petterson, Aug 28, 2003
    #1
    1. Advertising

  2. Mikael Petterson

    Kevin Hooke Guest

    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_examples/singleton_example.jsp

    --
    Kevin Hooke

    MindBeans Software Consulting
    http://www.mindbeans.net
     
    Kevin Hooke, Aug 29, 2003
    #2
    1. Advertising

  3. Mikael Petterson

    Harald Hein Guest

    "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?
     
    Harald Hein, Aug 29, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zalek Bloom
    Replies:
    14
    Views:
    684
    Michael Borgwardt
    Nov 21, 2003
  2. Manisha
    Replies:
    2
    Views:
    548
  3. NaeiKinDus
    Replies:
    1
    Views:
    592
    Jack Klein
    Apr 14, 2007
  4. NaeiKinDus
    Replies:
    3
    Views:
    621
    James Kanze
    Apr 15, 2007
  5. Navin Mishra
    Replies:
    0
    Views:
    200
    Navin Mishra
    Mar 22, 2005
Loading...

Share This Page