Netty-based message push solution for Android (1)

Source: Internet
Author: User

Netty-based message push solution for Android (1)
Message push Scheme (polling, persistent connection) Polling

Round robin: It is relatively simple. What is easier to understand and implement is that the client pulls information from the server. The higher the requirement for timeliness of information, the higher the frequency of pulling information. The client pull information can be triggered by some events, or a timer that constantly queries the server. Therefore, the disadvantages of this solution are obvious. When the polling frequency is high, the pressure on the server side is great, the communication traffic is also great, and most of the time it is useless.

Persistent connection

Persistent connection: the client and the server maintain a persistent connection. When information is pushed, the server sends the information to the client. The advantage of this solution is that information push is highly timely and basically Real-time. In addition to maintaining the connection heartbeat, no additional traffic is generated, but the server needs to maintain the connection, when the number of clients is large, the server consumes a lot of resources. The several frameworks mentioned later in this article all use the NIO feature of Java to relieve the pressure on the server and resource consumption, but after all there is a connection, in terms of performance, it is still unable to compare with the traditional HTTP serverless connection.

Others: on the mobile phone end, messages and emails can also be pushed. These methods involve the built-in service framework of operators and mobile phone operating systems, which has many restrictions. In addition, they are all charged, so they are not considered.
Android open-source framework (Androidpn Openfire MINA Netty) Android mobile app. Most of the Information pushed is about androidpn. This is a Java open-source information push solution based on XMPP protocol, including the complete server and client. The server has two versions: Tomcat and Jetty. After downloading the server, configure the database connection parameters and IP port to run. The client can use the Android code by referring to its example. Therefore, this is a highly customized version for android applications. If you want to modify and adjust the server, there are many moves.
Openfire is applicable to a wider range of applications. It is an open-source Real-Time Collaboration Server Based on XMPP protocol. It can be used to easily build an IM platform. Androidpn is simplified on Openfire and only applies to Android message push. From here, we can also see that in terms of message pushing, XMPP is a bit cool.
MINA and Netty are Socket frameworks and are written by the same author, with little difference in architecture. MINA is managed by Apache, and Openfire and Androidpn are both managed by MINA. Netty is managed by JBOSS. From the information I found, it is more inclined to be controlled by Netty, it is generally considered that the performance of Netty is slightly better, and the documents and examples are more complete.
The GCM C2DM Baidu cloud PUSH Service also has some existing cloud push services to choose from. You do not have to build a platform on your own. Google's GCM and Google Cloud Messaging are upgraded versions of C2DM. They are the most suitable for Android terminals, but they are widely understood in China, I am playing the "coming to the wall" game. Sometimes you can drill in and sometimes hit into the water, so it is unreliable. Baidu and some other companies also offer cloud push services, which seems to be free of charge. Google should be depressed. Searches, maps, emails, and pushes on Android are all wedding clothes for others in China.
Regression nature-Protocol (TCP/IP http xmpp mqtt)

I didn't expect a message push to have so many "connotations. In essence, it is not to maintain a connection. When the server needs to push messages, it transmits information to the client through these connections. If you want to be flexible and controllable, you can use the TCP/IP protocol to push messages. The next step is to implement the HTTP protocol. In this way, you can use both the firewall and AJAX to push messages on the web page. The asynchronous request support of Servlet 3.0 can also be used for message pushing. The XMPP and MQTT protocols must be more complex. You 'd better select an open-source framework to build them.

Netty Introduction

NettyIs an asynchronous, event-driven network programming framework and tool, is a NIO-based client, server-side programming framework, usingNettyYou can quickly and easily develop a network application, such as a client or server application that implements a certain protocol. Netty simplifies and streamlined the programming and development process of network applications, such as TCP and UDP socket service development.

Netty download

Download from official website or CSDN

Netty getting started (Hello World)

Create a java project in Eclipse, put the netty. jar package into the lib directory, and then build path --> add to build path. The directory structure is as follows:


The client code is as follows:

Public class HelloClient {public static void main (String args []) {// Client service initiator ClientBootstrap = new ClientBootstrap (new NioClientSocketChannelFactory (Executors. newCachedThreadPool (), Executors. newCachedThreadPool (); // sets a Handler bootstrap class for processing server messages and various message events. setPipelineFactory (new ChannelPipelineFactory () {@ Overridepublic ChannelPipeline getPipeline () throws Exception {return Channels. pipeline (new HelloClientHandler () ;}}); // connect to the local port 8000 server bootstrap. connect (new InetSocketAddress ("127.0.0.1", 8000);} private static class HelloClientHandler extends SimpleChannelHandler {@ Overridepublic void channelConnected (ChannelHandlerContext ctx, ChannelStateEvent e) {System. out. println ("Hello world, I'm client. ");}}}
The server code is as follows:

Public class HelloServer {public static void main (String [] args) {// Server service initiator ServerBootstrap bootstrap = new ServerBootstrap (new NioServerSocketChannelFactory (Executors. newCachedThreadPool (), Executors. newCachedThreadPool (); // sets a Handler bootstrap class for processing client messages and various message events. setPipelineFactory (new ChannelPipelineFactory () {@ Overridepublic ChannelPipeline getPipeline () throws Exception {return Channels. p Ipeline (new HelloServerHandler () ;}}); // open port 8000 for client access. Bootstrap. bind (new InetSocketAddress (8000);} private static class HelloServerHandler extends SimpleChannelHandler {@ Overridepublic void channelConnected (ChannelHandlerContext ctx, ChannelStateEvent e) {System. out. println ("Hello world, I'm server. ");}}}
Run the server first and then the client. The effect is as follows:


Next I will further explain the use of Netty.


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.