java SMS series of synchronous and asynchronous SMS

  • 2020-05-05 11:15:44
  • OfStack

This chapter is the first part of sending SMS, which describes the code of synchronous/asynchronous sending SMS. In the next few articles, we will improve the function slightly, adding the limit of sending frequency and the limit of daily sending times

There may be many ways to send text messages, but our method is to use the services provided by the service provider 1,
for sending short messages Choose a service provider according to your situation.
2. Development document
As you can see from the development documentation, you can either use http request directly or WebService request to send SMS Generate the client code

From the interface in the document we know its WebService WSDL url: http: / / 106. ihuyi. cn/webservice/sms php & # 63; WSDL then we can execute the following command to generate the client code :


wsimport -keep http://106.ihuyi.cn/webservice/sms.php?WSDL

wsimport is a tool that comes with JDK, and the -keep url option is "save generated files ". This command will generate the sms.cn.ihuyi._106 package in the current directory, as well as numerous classes

4. Define interface

For convenience, here we first define an interface :

Sms.java


public interface Sms {
 /**
  *  to mobile Send a text message ,  Content is message
  * 
  * @param mobile  Mobile phone no. 
  * @param message  Message content 
  * @return  Successfully returns -1,  Otherwise, other values are returned 
  */
 int sendMessage(String mobile, String message);
}

This interface is very simple, with only one method. This method is used to send SMS.

5, synchronous SMS

Let's first implement a class that synchronizes SMS messages :

IhuyiSmsImpl.java


public class IhuyiSmsImpl implements Sms {

 private String account;
 private String password;

 public void setAccount(String account) {
  this.account = account;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 @Override
 public int sendMessage(String mobile, String message) {
  cn.ihuyi._106.Sms factory = new cn.ihuyi._106.Sms();
  SmsSoap smsSoap = factory.getSmsSoap();
  SubmitResult submit = smsSoap.submit(account, password, mobile, message);
  int code = submit.getCode();
  if(code == 2){
   return -1;
  }
  System.out.println(" Text message failed , code:" + code);
  return code;
 }
}

On line 17, we get a proxy object for the remote object, from which we can then send text messages, check account balances, and so on

Line 18, the text message is submitted using the submit method of the proxy object. The method's parameter information and return value meanings are detailed in the interface documentation.

Row 19 we get the result of a status code. According to the instructions on the document, submit successful status code is 2. Simplicity, here we focus on submitted to success. It is important to note that the status code is 2 just shows success. According to the official website of the "3-5 seconds response, 100% reach", we can speculate. If submitted to success, so basically for 3 to 5 seconds, messages will send success, according to the user's network situation, may be a slight delay users can receive SMS.

Using this code to send a text message is also very simple, new directly an object, set up the account and password can send a message.

6. Asynchronous SMS

Due to send text messages to the network communication, thus sendMessage method there may be some delay. In order to improve the user experience, we can use the method of asynchronous send text messages. The principle is simple: if the user request to send text messages, we are not direct call IhuyiSmsImpl sendMessage method, but the request stored (producers), and then tell the user: text message sent successfully. After several consumers take out tasks, call sendMessage method to send text messages.

Here, I use the thread pool to do the above task :

AsyncSmsImpl.java


public class AsyncSmsImpl implements Sms {
 public Sms sendSms;
 private ExecutorService executorService = Executors.newFixedThreadPool(3);

 public void setSendSms(Sms sendSms) {
  this.sendSms = sendSms;
 }

 @Override
 public int sendMessage(String mobile, String message) {
  try {
   executorService.submit(() -> sendSms.sendMessage(mobile, message));
  }
  catch(Exception e) {
   Sysemt.out.println(" An error occurred while submitting the task " + e);
   return 0;
  }
  return -1;
 }

 public void destroy(){
  try{
   executorService.shutdown();
  }
  catch(Exception e){}
 }
}

The code is simple, simply adding the sendMessage(mobile, message) method of the Sms interface to the task queue of the thread pool as a task

This is where synchronous/asynchronous texting is done, and in the next few posts we'll look at some common limitations, such as 1 message per minute and 5 messages per day.
I hope you enjoy this article.


Related articles: