M
Mikael Petterson
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");
}
}
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");
}
}