Android Netty-based message push scheme Hello World (i)

Source: Internet
Author: User

Message push scenarios (polling, long connections) polling

Polling: Relatively simple, the most easy to understand and implement is the client to the server pull information, the higher the timeliness of information requirements, the higher the frequency of pull information. The trigger of the client pull message can be a number of events, or it can be a timer, constantly to query the server. So the disadvantage of this scheme is also obvious, in the polling frequency is high, the server side of the pressure is very large, traffic is also very large, and most of the time is not working hard.

Long connections

Long connection: A long connection is maintained between the client and the server, and the server sends the information to the client with this connection when the message is pushed. The advantage of this scheme is that information is pushed in a timely manner, basically real-time, and in addition to maintaining a connected heartbeat, will not generate additional traffic, but the server needs to maintain the connection, when the number of clients is large, the resource consumption of servers will be very large. A few of the frameworks mentioned later in this article, with the help of the Java NIO feature, alleviate the pressure and resource consumption on the server, but after all, there is a connection and cannot be compared with the traditional HTTP no-connect server in performance.

Other

Other: In the mobile phone, in fact, you can also have text messages, mail and other ways to carry out information push. These methods because of the operator and mobile phone operating system built-in service framework, more restrictions, not to mention to be charged, so do not consider.

Open source Framework (ANDROIDPN openfire MINA Netty)

Android mobile app, information pushes most of the data is about ANDROIDPN, this is an XMPP protocol based Java Open source information push scheme, including the full service side and the client. There are two versions of Tomcat and jetty on the server, and after downloading it, configure the database connection parameters and the IP port to run. The client can refer to its example and take the Android code with you. So this is a highly customized version for Android apps, and if you want to make changes to the server, move more places.
The OpenFire is a more extensive, open source real-time collaboration server based on the XMPP protocol, which can be used to simply build an IM platform. ANDROIDPN was simplified on the openfire and only pushed for Android messages. It can also be seen from here that the XMPP protocol is a bit overkill for message push.
Mina and Netty are socket frames, which are the same author, with little difference in architecture. Mina to Apache tube, OpenFire and ANDROIDPN are used Mina;netty is the JBoss tube, from the data I retrieved, more biased to Netty, generally think Netty performance is slightly superior, documents and examples more complete.

Message Push Service (GCM C2DM Baidu Cloud Push)

In fact, there are some existing cloud push services to choose from, do not have to build a platform for themselves. Google's Gcm,google Cloud Messaging, is C2DM upgrade version, Android terminal with the words, originally is the most suitable but, but in the country everyone knows, play is "wall" game, sometimes you can drill past, sometimes you will be hit overboard, so not reliable ah. Baidu and some other companies also offer cloud push services, as if there were no free ones. Google should be more depressed, Android search, map, mail, push and so on, in the country are for others to marry clothes.

Regression Essence-Protocol (TCP/IP HTTP XMPP MQTT)

Did not expect a message push can have so much "connotation" ah. In essence, it is not the maintenance of a connection, the service side need to push the message when it is through these connections to the client to transmit information. Think carefully, to be flexible and controllable, the best way is to implement message push based on TCP/IP protocol. Further up, it is implemented on the HTTP protocol, so that both the firewall can be passed through and Ajax can be used to implement message push on the Web page. Servlet 3.0 's asynchronous request support, in fact, can also be used to do message push. The XMPP and MQTT protocols are more complex, and it's best to choose an open source framework to build.

Netty Introduction

Netty is an asynchronous, event-driven Network programming framework and tool that is a NiO-based client, a server-side programming framework that uses Netty to ensure that you quickly and simply develop a Web application, such as a client that implements some kind of protocol, a service-side application. Netty quite simplifies and streamlines the programming of Web applications, for example, the development of socket services for TCP and UDP .

Netty Download

Official website download or csdn download

Netty Getting Started (Hello world)

Create a new Java project in Eclipse, put the Netty.jar package in the Lib directory, and build path-->add to build path, the directory structure is as follows

The client code is as follows

[Java]View Plaincopy
  1. Public class Helloclient {
  2. public static void Main (String args[]) {
  3. //Client Service Launcher
  4. Clientbootstrap bootstrap = new Clientbootstrap (new Nioclientsocketchannelfactory (  Executors.newcachedthreadpool (), Executors.newcachedthreadpool ()));
  5. //Set up a class to handle server-side messages and various message events (Handler)
  6. Bootstrap.setpipelinefactory (new Channelpipelinefactory () {
  7. @Override
  8. Public channelpipeline Getpipeline () throws Exception {
  9. return Channels.pipeline (new Helloclienthandler ());
  10. }
  11. });
  12. //Connect to the local 8000 port service side
  13. Bootstrap.connect (new Inetsocketaddress ("127.0.0.1", 8000));
  14. }
  15. private static class Helloclienthandler extends Simplechannelhandler {
  16. @Override
  17. public void channelconnected (Channelhandlercontext ctx, channelstateevent e) {
  18. System.out.println ("Hello World, I ' m client.");
  19. }
  20. }
  21. }

The service-side code is as follows

[Java]View Plaincopy
  1. Public class HelloServer {
  2. public static void Main (string[] args) {
  3. //Server service Initiator
  4. Serverbootstrap bootstrap = new Serverbootstrap (new Nioserversocketchannelfactory (  Executors.newcachedthreadpool (), Executors.newcachedthreadpool ()));
  5. //Set up a class to handle client messages and various message events (Handler)
  6. Bootstrap.setpipelinefactory (new Channelpipelinefactory () {
  7. @Override
  8. Public channelpipeline Getpipeline () throws Exception {
  9. return Channels.pipeline (new Helloserverhandler ());
  10. }
  11. });
  12. //Open 8000-Port confession client access.
  13. Bootstrap.bind (new inetsocketaddress (8000));
  14. }
  15. private static class Helloserverhandler extends Simplechannelhandler {
  16. @Override
  17. public void channelconnected (Channelhandlercontext ctx, channelstateevent e) {
  18. System.out.println ("Hello World, I ' m server.");
  19. }
  20. }
  21. }

Run the server first and then run the client with the following effect


Below I will further explain the use of Netty.

Android Netty-based message push scheme Hello World (i)

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.