Analysis of Implementation Principles of MPs projects based on pipelines (1)

Source: Internet
Author: User

Analysis of Implementation Principles of MPs projects based on pipelines (1)

Project implementation principle

Sevice only needs to send data to the pipeline (data pool). When there is data in the pool, it will automatically find you. You don't have to worry about how data is sent and received, but you just need to care about the processing of your business.

For example

Advantages:

The pipeline-based implementation is that the message sending or receiving only needs to be sent to or read from the pipeline, instead of paying attention to how to send the message through the Channer, thus realizing the decoupling between the service layer and the socket.

It depends on broadcasting and does not rely on callback functions. It is asynchronous and non-blocking with nio, and truly achieves zero wait for threads.

Disadvantages:

It is difficult to implement the data sent by dropping functions (or not at all). It can only be implemented through broadcast.

Related Classes

● ClientMessagePool, ServiceMessagePool (data pool)

The internal implementation principle is a linked list queue. The data is added and read into the corresponding queue, and the queue header elements are read.

● Sevice

The business logic processing class must implement the IMessageSevice interface and register with MessageObserver

● MessageObserver

There is an IMessageSevice linked list, which stores the services that implement the IMessageSevice interface and forms the observer mode with Sevice,

There will be a thread dedicated to monitoring the MessagePool. Once there is data, it will be handed over to the MessageObserver. MessageObserver sends the message to the specified service based on the specified message.

● SocketChannel

Implements a SockenChannel class, which is equivalent to a client. Reads data from the ClientMessagePool. Once there is data, the data is written to the MPs queue.

● Selector

Receives the registration of the MPs queue and pushes data to the specified SocketChannel Based on the Transmission Conditions. Data is also filtered Based on the filter conditions.

Code Implementation

Pipeline Code Implementation

 
 
  1. package com.pool;   
  2.    
  3. import java.util.Queue;   
  4. import java.util.concurrent.LinkedBlockingQueue;   
  5.    
  6. public class MessagePool {   
  7.     public static Queue<String> clintmessageQueue = new LinkedBlockingQueue<String>();    
  8.     public static Queue<String> serverMessageQueue = new LinkedBlockingQueue<String>();    
  9.        
  10. }   

Interface

 
 
  1. package com.pool;   
  2.    
  3. public interface IMessagePool {   
  4.    
  5.     public void addMessage(String message);   
  6.    
  7.     public String pollMessage();   
  8.        
  9.     public boolean isEmpty();   
  10.    
  11. }  

Implementation class

 
 
  1. package com.pool.impl;   
  2.    
  3. import com.pool.IMessagePool;   
  4. import com.pool.MessagePool;   
  5.    
  6. public class ClientMessagePool implements IMessagePool {   
  7.     @Override   
  8.     public  void addMessage(String message) {   
  9.         MessagePool.clintmessageQueue.add(message);   
  10.     }   
  11.     @Override   
  12.     public  String pollMessage() {   
  13.         return MessagePool.clintmessageQueue.poll();   
  14.     }   
  15.        
  16.     @Override   
  17.     public boolean isEmpty() {   
  18.         if(MessagePool.clintmessageQueue.size()>0)   
  19.             return true;   
  20.         else   
  21.             return false;   
  22.     }   
  23. }   


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.