This chapter is the first part of sending text messages. Let’s talk about the code for sending text messages synchronously/asynchronously. In the next few articles, we will slightly improve the functions and add the limit on the sending frequency and the daily sending times.
There may be many ways to send text messages. Our method is to use services provided by the service provider. Generally speaking, these services are language-free. Here we use Java to write sample programs.
1. Interface for sending text messages <br />Select a service provider according to your own situation.
2. Development Document <br />We can see from the development document. You can use http requests directly or WebService requests to send text messages. Since the code in the java and jsp folders in the DEMO folder use http requests to send text messages. So I won't go into details here. We use WebService to demonstrate sending text messages.
3. Generate client code
From the interface document, we know that the url of its WebService WSDL is: http://106.ihuyi.cn/webservice/sms.php?WSDL Then we can execute the following command to generate client code:
wsimport -keep http://106.ihuyi.cn/webservice/sms.php?WSDL
Among them, wsimport is a tool that comes with JDK, and the -keep url option is "Keep the generated files". This command will generate sms.cn.ihuyi._106 packages in the current directory, as well as many classes. Next, start writing our own code.
4. Define the interface
For convenience, here we first define an interface:
Sms.java
public interface Sms { /** * Send a SMS to mobile, the content is message * * @param mobile phone number * @param message SMS content * @return successfully returns -1, otherwise return another value */ int sendMessage(String mobile, String message);}This interface is very simple, there is only one method. This method is used to send text messages.
5. Send text messages simultaneously
Next, we first implement a class that sends SMS synchronously:
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("Send SMS Failed, code:" + code); return code; }}In line 17, we obtain a proxy object of the remote object. Then we can send SMS, query the account balance and other operations through this proxy object.
In line 18, the SMS content is submitted using the submit method of the proxy object. The parameter information and return value meaning of the method are explained in detail in the interface document.
On line 19, we get the status code of the result. According to the instructions on the document, the status code is 2, indicating that the submission is successful. For simplicity, we only focus on the situation where the submission is successful. It should be noted that the status code is 2, only indicating that the submission is successful. According to the official website "Response within 3-5 seconds, 100% arrival" on the official website, we can infer. If the submission is successful, basically the SMS will be sent successfully within 3-5 seconds. According to the user's network situation, the user may receive the SMS with a slight delay.
It is also very simple to use this code to send text messages. You can directly new an object, set your account and password and send text messages.
6. Send text messages asynchronously
Since sending SMS involves network communication, the sendMessage method may have some delay. In order to improve the user experience, we can use the method of sending SMS asynchronously. The principle is very simple: If the user requests to send SMS, we do not directly call the sendMessage method of IhuyiSmsImpl, but save the request (producer), and then tell the user: The SMS is sent successfully. After that, several consumers take out the task and call the sendMessage method to send SMS.
Here, I use a thread pool to complete 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("Error occurred while submitting the task" + e); return 0; } return -1; } public void destroy(){ try{ executorService.shutdown(); } catch(Exception e){} }}The code is very simple. You can directly add the sendMessage(mobile, message) method of the Sms interface to the task queue of the thread pool as a task. In this way, when there are free threads, sendSms.sendMessage(mobile, message) will be executed to send SMS. Here we assume that as long as it is saved to the thread pool, you can successfully send SMS. Because sending failure is actually rare.
Send text messages synchronously/asynchronously is completed. In the next few articles, we will take a look at some common restrictions, such as: you can only send once a minute, you can only send 5 times a day, etc.
I hope you like this article.