Java callback mechanism

Source: Internet
Author: User
Tags sendmsg

In the public number development process, the use of callback mode, when sending a template message to the user each time to wait a long time, which is unreasonable, so think of using callback mode to properly shorten the waiting time. Directly on the code:

Creates a callback interface for the callback pattern, which is called when the server executes to return a value.

1 /**2 * Callback Interface3  * @authorXiao Quan4 * @create December 9, 2015 morning 11:54:485 * @varsion 1.06  */7  Public InterfaceMsgcallback {8      Public voidhandler (String status);9}

Send server-side (contains only calls to send message methods):

1 /**2 * Simulation Server3  * @authorXiao Quan4 * @create December 9, 2015 morning 11:57:105 * @varsion 1.06  */7  Public classSendserver {8     9      Public voidgetclientmsg (msgcallback callback,weixinmsg msg) {TenSYSTEM.OUT.PRINTLN ("Server": the server received a message to be sent as "+msg.getmsg ()); OneString status = "200"; A         //The time-consuming process of simulating a server request server -         Try{ -Thread.Sleep (5*1000); the //String result = Httputil.post (wxurl,data); -             if(Msg.getto (). Contains ("1")) {//test: Send failed if 1 is included -Status = "201"; -             } +}Catch(interruptedexception e) { - e.printstacktrace (); +         } AString result = "Server Side": Sent to "+msg.getto () +" message ("+msg.getmsg () +") "; at         if("$". Equals (status)) {//Default Success -System.out.println (result+ "Send successfully, return success status 200"); -}Else{ -System.out.println (result+ "Send failed, return failed status 201"); -         } - Callback.handler (status); //callback method  in     } -}

Impersonation Call class:

1 /**2 * Analog Send client, implement callback interface3  * @authorXiao Quan4 * @create December 9, 2015 morning 11:58:225 * @varsion 1.06  */7  Public classSendclientImplementsMsgcallback {8     Privatesendserver server;9      Publicsendclient (sendserver server) {Ten          This. Server =server; One     } A      Public voidSendmsg (Finalweixinmsg msg) {// Send Message Method -System.out.println ("operator": the message to be sent is "+msg.getmsg ()); -         NewThread (NewRunnable () { the @Override -              Public voidrun () { -Server.getclientmsg (sendclient. This, msg); -             } + }). Start (); -System.out.println ("operator": \ "" "+msg.getfrom () +" \ "Send to \" "+msg.getto () +" \ "Message (" +msg.getmsg () + ") has entered the Send queue! "); +     } A @Override at      Public voidhandler (String status) { -System.out.println ("operator": Server send return status "+status); -     } -}

Give a test class:

1 /**2  * @authorXiao Quan3 * @create December 9, 2015 PM 12:08:304 * @varsion 1.05  */6  Public classTestcallback {7      Public Static voidMain (string[] args) {8Sendserver Server =Newsendserver ();9Sendclient client =Newsendclient (server);Ten weixinmsg msg; One          for(inti=0;i<5;i++){ Amsg =Newweixinmsg (); -Msg.setfrom ("Sender"); -Msg.setmsg ("Thank you for your valuable advice"); theMsg.setto (i+ ""); - client.sendmsg (msg); -         } -     } +}

Test results:

1 "operator": Message to be sent thank you for your valuable advice2"operator": "Sender" sent to "0"message (Thank you for your valuable advice) has entered the Send queue! 3 "operator": Message to be sent thank you for your valuable advice4"operator": "Sender" Sent to "1"message (Thank you for your valuable advice) has entered the Send queue! 5 "operator": Message to be sent thank you for your valuable advice6 "Server": the server receives the message to be sent thank you for your valuable advice7"operator": "Sender" Sent to "2"message (Thank you for your valuable advice) has entered the Send queue! 8 "operator": Message to be sent thank you for your valuable advice9"operator": "Sender" Sent to "3"message (Thank you for your valuable advice) has entered the Send queue! Ten "Server": the server receives the message to be sent thank you for your valuable advice One "operator": Message to be sent thank you for your valuable advice A"operator": "Sender" Sent to "4"message (Thank you for your valuable advice) has entered the Send queue!  - "Server": the server receives the message to be sent thank you for your valuable advice - "Server": the server receives the message to be sent thank you for your valuable advice the "Server": the server receives the message to be sent thank you for your valuable advice -"Server Side": Send to 0 message (thank you for your valuable suggestions) sent successfully, return to success status 200 -"Operator": the server sends a return status of 200 -"Server Side": Send to 2 message (thank you for your valuable suggestions) sent successfully, return to success status 200 +"Operator": the server sends a return status of 200 -"Server Side": Send to 3 message (thank you for your valuable suggestions) sent successfully, return to success status 200 +"Operator": the server sends a return status of 200 A"Server Side": Send to 1 message (Thank you for your valuable suggestions) sent failed, return failure status 201 at"Operator": the server sends a return status of 201 -"Server Side": Send to 4 message (thank you for your valuable suggestions) sent successfully, return to success status 200 -"Operator": the server sends a return status of 200

The above is a simple implementation of the callback process, interested in child shoes can be in-depth study under

Thanks for maths~

Java callback mechanism

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.