Java Send SMS series of synchronous, asynchronous send SMS _java

Source: Internet
Author: User
Tags wsdl

This chapter is to send the first part of the text message, say synchronous/Asynchronous send SMS code, after a few of us slightly improve the function, add the frequency of the limit and the number of days to send restrictions.

There may be many ways to send text messages, and our approach is to use services provided by service providers. In general, these services are language-independent, and here we use Java to write sample programs.
1, send the text message interface
Choose the service provider according to your own situation.
2, the development of documents
From the development documentation we can see. You can use HTTP requests directly or send text messages using WebService requests. Because the code in the Java and JSP folders under the Demo folder is to send text messages using HTTP requests. So this is no longer in detail, and we use the WebService way 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 the client code:

Wsimport-keep Http://106.ihuyi.cn/webservice/sms.php?WSDL

Where Wsimport is the JDK's own tool, the-keep URL option is "Keep generated files." This command generates SMS.CN.IHUYI._106 packages in the current directory, as well as numerous classes. Next, start writing our own code.

4. Define Interface

For convenience, here we first define an interface:

Sms.java

Public interface SMS {
 /**
  * Send SMS to Mobile with message
  * 
  * @param mobile phone number
  * @param message SMS Content
  * @return return 1 successfully, otherwise return the other value
  *
 /int SendMessage (string mobile, String message);
}

This interface is simple, there is only one way. This method is used to send text messages.

5. Send SMS synchronously

Next we first implement a class that sends messages 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 Failure, code:" + code);
  return code;
 }


On line 17th, we get a proxy object for the remote object. After that, you can send text messages through this proxy object, query account balance, and so on.

The 18th line, using the proxy object's Submit method submitted the text message content. The parameter information of the method and the meaning of the return value are described in detail in the interface document.

Line 19th We get the status code of the result. According to the instructions on the document, the status code of 2 indicates the success of the commit. For simplicity's sake, here we focus on the success of the submission. It should be noted that the status code of 2 is only a description of the successful submission. According to the official online "3-5 seconds response, 100% arrival", we can speculate. If the submission is successful, then basically 3-5 seconds, the message will be sent successfully, according to the user's network situation, may be slightly delayed users can receive text messages.

Using this code to send a text message is also very simple, direct new object, set up a good account and password can send text messages.

6. Send SMS Asynchronously

Because sending text messages involves network communication, the SendMessage method may have some latency. To improve the user experience, we can use the method of sending SMS asynchronously. The principle is simple: if the user requests to send text messages, we are not directly call the Ihuyismsimpl SendMessage method, but will save the request (producer), and then tell the User: SMS sent successfully. After a number of consumers take out the task, call the SendMessage method to send text messages.

Here, I use the thread pool to complete the above tasks:

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 task" + e);
   return 0;
  }
  return-1;
 }

 public void Destroy () {
  try{
   executorservice.shutdown ();
  }
  catch (Exception e) {}
 }
}

The code is simply to add the SMS interface's SendMessage (mobile, message) method as a task to the thread pool's task queue. This will execute Sendsms.sendmessage (mobile, message) SMS when there is an idle thread. Here we assume that a message can be sent successfully as long as it is saved to the thread pool. Because the sending failure situation is actually very rare.

Here to sync/asynchronous send SMS even if it is done, the next few we look at some of the common limitations of the implementation, such as: a minute can only be sent 1 times a day can only send 5 times.
I hope you enjoy this article.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.