Incomplete Sample Code

/*This class is implementation class for sending email/SMS/Pager to the customer when an alert is generated*/

import java.net.*;
import java.sql.*;
import.java.io.*;
import java.util.*;

public class MailServer extends Thread
implements LoadBalancingServer
{
//define variables to interact with the database
MailDB mdb = new MailDB();
//variable of class resultset to store
//the results after firing a query
private static ResultSet rset = null;
//define other instance variables
static ServerLogFile serverlogfile = new ServerLogFile();
private int threadno=0;
private static String mclist[][] = null;
private Socket sock= null;
private ArrayList clientarray = null;
private ArrayList statusarray = null;
private int capacity = 0;
private static ArrayList centralarray = new ArrayList();

private int i1 = 0;
private int i2 = 0;
private int i3 = 0;
private int i4 = 0;
private int i5 = 0;

private int newcapacity = 0;
private Integer finalcapacity = null;
private ObjectOutputStream oos = null;
private ObjectInputStream ois = null;
private static boolean flag = true;
private StringTokenizer stokenizer= null;
private int counter =0;
private static String[] machines=null;
private int msgid=0;
private boolean logflag= true;
private Object objread = null;
private String returnstring="";
private String alert_no="";
private String contact1="";
private String contact2="";
private String from_id="";
private String from_add="";
private String attachment_id="";
private String backupmode_flag="";
private String contact1_counter="";
private Statement st=null;

private String status ="";
private boolean issockopen=false;
private String mode_in="";

//Constructors
public MailServer()
{}

public Mailserver(String readpropertyfile)
{

try{

//read the m/c no. from the property file
File prop = new File("server.properties");
FileInputStream fis = new FileInputStream(prop);
PropertyResourceBundle prb = new PropertyResourceBundle(fis);
String machinenames = (String)
prb.handleGetObject("alertnow.notification.machines");
readMachines(machinenames);
//installing and populating the two
//dimensional array
mclist = new String[machines.length][3];
//for each client machine initialize machine
//name, status, & default capacity
for (int k =0; k<machines.length; k++)
{
mclist[k][0]=machines[k];
mclist[k][1]="1";
mclist[k][2]="2";
}
}
catch(IOException ex)
{
serverlogfile.writeLogMessage("mailServercould not locate Server Property file");
}
}

//get data
/*this method retrieves the data from the
database in a resultset. This method is
called only when the central arraylist gets exhausted */

public void getData()
{
rset=mdb.getResultset();
}

/* This method populates the central arraylist
with the messages stored in resultset*/

public void populateData(){
int count= 0;

try
{
//populate centralarray from the resultset
while (rset.next())
{
alert_no = rset.getString("alert_no");
message = rset.getString("message");
contact1 = rset.getString("contact1");
contact2 = rset.getString("contact2");
from_id = rset.getString("from_id");

attachment_id = rset.getString("attachment_id");
backupmode_flag = rset.getString("backupmode_flag");
contact1_counter = rset.getString("contact1_counter");
mode_in = rset.getString("mode_in");
from_add = mdb.getFromAddress(from_id);
mdb.updateRecord(alert_no);

//back up mode code
if ((mdb.getModeStatus(mode_id).equals("0")) &&
backupmode_flag.equals("0"))
{
mdb.insertbackmode(alertno);
}
else
{
centralarray.add(alert_no);
centralarray.add(message);
if(contact1_counter.equals("0"))
centralarray.add(contact1);
else
{
if(contact2==null)
{
centralarray.add(contact1);
}
else
{
centralarray.add(contact2);
}
}
centralarray.add(from_add);
centralarray.add(attachment_id);
//count++;
}
}
}
catch (SQLException ex)
{
System.out.println(ex.getMessage());
}

}

/* This method create new socket connection
for the respective machine. This method sets
the capacity and status for each machine in
case of failed socket connection this method
also sets the capacity and status for the machine which
failed to make socket connection earlier */

public void getStream()
{
try
{
sock = new Socket(mclist[threadno][0], 3001);

if (logflag==false)
{
serverlogfile.writeLogMessage(mclist[threadno][0] +
"connected with MAIL SERVER");

logflag=true;
}
if (mclist[threadno][1].equals("0"))
{
mclist[threadno][1]="1";
mclist[threadno][2]="2";
}

}
catch(IOException ex)
{
mclist[threadno][1]="0";
mclist[threadno][2]="0";

if(logflag==true)
{
serverlogfile.writeLogMessage(mclist[threadno][0] +
" failed to connect with MAIL SERVER");

logflag=false;
}
}
}

/*This method distributes the messages into
the arraylist for respective machine from
the central ArrayList based on the capacity
of the machine. The distributed messages are
removed from the central arraylist. When
the central arraylist is exhausted method call
is made to retrieve the data from database
and populate it.*/

public void shareData(int fcap)
{
capacity = fcap * 5;
clientarray = new ArrayList();

synchronized (centralarray)
{
//synchronized block to avoid concurrent access
// of threads to the central array list.

if(centralarray.size()!=0)
{
if(capacity>centralarray.size())
{
/*populate client machine arraylist
from central arraylist*/
for(i1=0; i1<centralarray.size(); i1=i1+5)
{
clientarray.add(centralarray.get(i1));
clientarray.add(centralarray.get(i1+2));
clientarray.add(centralarray.get(i1+3));
clientarray.add(centralarray.get(i1+4));
}

int size = centralarray,size();
/*remove populated messages in respective client
machine arraylist from central arraylist*/

for(i2=0;i2<size;i2++)
{
centralarray.remove(0);
}
}
else
{
for(i3=0;i3<capacity;i3=i3+5)
{
clientarray.add(centralarray.get(i3));
clientarray.add(centralarray.get(i3+1));
clientarray.add(centralarray.get(i3+2));
clientarray.add(centralarray.get(i3+3));
clientarray.add(centralarray.get(i3+4));
}

/*remove populated messages in respective
client machine arraylist from central arraylist*/
for(i4=0; i4<capacity;i4++)
{
centralarray.remove(0);
}
}
}

if (centralarray.size()==0)
{
if(mclist[threadno][1].equals("1"))
{
getData();
pupulateData();
}
}
}
}


/*This method dispatches the messages on an
ObjectoutputStream to the respective machine.
In case of an exception in writing the message
the message are added back to the top of the
central array list*/

public void despatchMessage()
{
try
{
if(sock!=null)
{
oos = new ObjectOutputStream(sock.getOutputStream());
oos.writeObject(clientarray);
}
}
catch(IOException ex)
{
synchronized(centralarray)
{
for(int j =0; j<clientarray.size(); j++)
centralarray.add(0, clientarray.get(j));
}
System.out.println(ex.getMessage());
}
}

/* This method reads the respective arraylist on an
ObjectInputStream which contains the id and status of
the messages. */

public void getStatus()
{
statusarray = new ArrayList();

try
{
if(sock!=null)
{
ois=new ObjectInputStream(
sock.getInputStream());
objread = ois.readObject();

if(objread instanceof ArrayList)
{
statusarray = (ArrayList)objread;

System.out.println("RECEIVED "+
statusarray);
returnstring = "";
}
else
{
returnstring = "EMAIL";
}

ois.close();
}
}
catch(IOException ioe)
{

System.out.println("IN GETSTATUS");
System.out.println(cnf.getMessage());
}
}

/* This method updates the database based on
the arrayList read from respective machines.
It also reinitializes the capacity of the machines
under various conditions.*/

public int setStatus()
{

if(returnstring.equals("EMAIL"))
{
newcapacity=2;
}
else
{
if(flag)
{
newcapacity = 2;
flag=false;
}
else if(mclist[threadno][1].equals("1") &&
statusarray.size()==0)
{
newcapacity=2;
}
else
{
newcapacity=2;
}

//update database from each message
//status received from client machine
for(i5=0; i5<statusarray.size();i5+=2){
msgid=Integer.parseInt((String)statusarray.get(i5));
status=(String)statusarray.get(i5+1)
if (status.equals("failure"))
mdb.updateErrorStatus(msgid);
else
mdb.updateSuccessStatus(msgid);
}
}
return newcapacity;
}

/* This method sets the capacity of respective machine
based on the ArrayList read from the resp[ective machines.
and other various conditions handled in setStatus method.*/

public void setCapacity(int cap)
{
finalcapacity = new Integer(cap);
mclist[threadno][2] = finalcapacity.toString();
}

/*The main method*/

public static void main(String clientproperties[])
{
MailDB mdb = new MailDB("STRING PASSED TO CREATE DB CONNECTION");
ArrayList threadlist = new ArrayList();
threadlist.add(new MailServer(
"Passing string to read the property file once"));
((Thread)threadlist.get(0)).setName("0");
((Thread)threadlist.get(0)).start();

//start thread for each client machine
for(int i=1;i<machines.length;i++)
{
threadlist.add(new MailServer());
((Thread)threadlist.get(i)).setName(""+i);
((Thread)threadlist.get(i)).start();
}
}


public void run()
{
int aa = 0;
threadno=Integer.parseInt(Thread.currentThread().getName());
//start of continuous process
while(true)
{
try
{
if(MainServer.startclientflag==false)
{
mdb.conn.close();
MainServer.firstquery=true;
System.out.println("Stopping the backup server " +
"processes in mailserver");
return;
}
st=mdb.conn.createStatement();
st.executeQuery("Select getDate()");
System.out.println("IN RUN" +
st.executeQuery("select getDate()"));
st.close();
getStream();
shareData(Integer.parseInt(mclist[threadno][2]));
despatchMessage();
getStatus();
setCapacity(setStatus());
try
{
Thread.sleep(2000);
}
catch(InterruptedException ex)
{
System.out.println(ex.getMessage());
}

}
catch(SQLException ex)
{
System.out.println("IN SQLEXCEPTION OF MAIL SERVER");
ConnectionDB condb = new ConnectionDB();
try
{
MailDB conn = condb.getConnection();
st=mdb.conn.createStatement();
System.out.println("IN CATCH OF MAILSERVER" +
st.executeQuery("select getDate()");
st.close();
}
catch(SQLException sq)
{
try
{
sq.printStackTrace();
Thread.sleep(10000);
}
catch(Exception ee)
{}

}
}
catch(Exception ex)
{
try
{
ex.printStackTrace();
Thread.sleep(20000);
}
catch (Exception ee)
{}
}
}
}

/* This method is called once from the constructor
to read the machine names from the property file
and populate the machine name array. */
public void readMachines(String machinenames)
{
stokenizer=new StringTokenizer(machinenames, ",");
int number = stokenizer.countTokens();
machines = new String[number];
//add machine names in machine array
while(stokenizer.hasMoreTokens())
{
machines[counter=stokenizer.nextToken();
counter++;
}
}

//end of all methods

}//end of class


 

  Java Alert Notification System Home Page

   Previous    Next  
 

UNIVERSAL TEACHER PUBLICATIONS
Web: universalteacherpublications.com, universalteacher.com, universalteacher4u.com