ear All,
I have java function as below. If you notice this class calls another class that is SMSClient and in that class I have other classes called too. So then I write a php as below and is just call this function and return the results. I want to wait for all the class to function and only finally return the answer. How make it wait for the entire function to complete? Thank you.

<?php
$output = array();
$return_var = 0;
exec("java callSMSClient +60164562233 test1", $output, $return_var);
$output_str = join($output, "\n");

echo "<pre>The output was:\n$output_str\n\n";
echo "The return value was $return_var</pre>";
?>
public class callSMSClient{

  public static void main(String[] args)
  {
  	SMSClient t1 = new SMSClient(1);
  	if (args.length > 0)
    {
  	 String phoneNumber= args[0];
  	 String messageSend = "";
  	 System.out.println("ARgs lenght is :"+args.length);
  	 for(int i=1;i<args.length;i++)
  	 {
  	 	if(i==1)
  	 	{
  	 	  messageSend = args[i];
  	 	}
  	 	else
  	 	{
  	 	  messageSend = messageSend+" "+args[i];	
  	 	}
  	 	
  	 }
  	 //System.out.println("Message sent  :"+messageSend);
  	 t1.sendMessage(phoneNumber,messageSend);	
    }

  
  }
}

Recommended Answers

All 16 Replies

Are you sure that t1.sendMessage(phoneNumber,messageSend) is a blocking function ? Perhaps it starts a new thread and therefor returns control instantly.

Dear Pritaeas,
I am not too clear what is blocking function? Below is how the full function look like.

public class SMSClient implements Runnable{

  public final static int SYNCHRONOUS=0;
  public final static int ASYNCHRONOUS=1;
  private Thread myThread=null;

  private int mode=-1;
  private String recipient=null;
  private String message=null;

  public int status=-1;
  public long messageNo=-1;


  public SMSClient(int mode) {
      this.mode=mode;
    }

  public int sendMessage (String recipient, String message){
    this.recipient=recipient;
    this.message=message;
    //System.out.println("recipient: " + recipient + " message: " + message);
    myThread = new Thread(this);
    myThread.start();
//    run();
    return status;
    }
    public void run(){

    Sender aSender = new Sender(recipient,message);

    try{
      //send message
          aSender.send ();

         // System.out.println("sending ... ");

      //in SYNCHRONOUS mode wait for return : 0 for OK, -2 for timeout, -1 for other errors
      if (mode==SYNCHRONOUS) {
          while (aSender.status == -1){
            myThread.sleep (1000);
          }
      }
      if (aSender.status == 0) messageNo=aSender.messageNo ;

    }catch (Exception e){

        e.printStackTrace();

    }

    this.status=aSender.status ;

    aSender=null;


  }
}

The SYNCHRONOUS option should be set.

Dear Pritaeas,
Where must I put this synchronous option in my main code? Thank you.

In the constructor.

Dear Pritaeas,
I have tried putting it here like this public synchronized SMSClient(int mode) { but I get an error as below

modifier synchronized not allowed here
  public synchronized SMSClient(int mode) {

I have tried putting it on the public int sendMessage it works but from the web it is same it can function as how I wanted too. Thank you.

Dear Pritaeas,
What else is my mistake can you help me please? Thank you.

You should put it here, instead of the 1

SMSClient t1 = new SMSClient(0);

Dear Priaeas,
synchronized SMSClient t1 = new SMSClient(0);. Is this how you wanted is it ? Can you correct me. Thank you.

Does it work ?

Dear Pritaeas,
Below is what I get when I try to compile. What else must I do? Thank you.

callSMSClient.java:10: '(' expected
        synchronized SMSClient t1 = new SMSClient(1);
                    ^
callSMSClient.java:10: ')' expected
        synchronized SMSClient t1 = new SMSClient(1);
                              ^
callSMSClient.java:10: illegal start of expression
        synchronized SMSClient t1 = new SMSClient(1);
                                  ^
callSMSClient.java:10: ';' expected
        synchronized SMSClient t1 = new SMSClient(1);
                                   ^
callSMSClient.java:34: reached end of file while parsing
}
 ^

Now I see what you did; no, remove the synchronized.

SMSClient t1 = new SMSClient(0);

0 means synchronyzed, 1 means asynchronous. It is defined as such in the class.

Dear Pritaeas,
Ok thank you I saw it at last. Actually this code I took from a website so I am testing it out. Let me do further analysis then I will get back to you. Thank you.

Dear Pritaeas,
I want to additional code where every 30 second it will check a table to see if there is any message to be send if it is there then it will pick and send it. Any idea where to start please? Thank you.

Search for cronjob. Next time better start a new thread.

Dear Pritaeas,
Ok I will start a new thread first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.