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
- package com.pool;
-
- import java.util.Queue;
- import java.util.concurrent.LinkedBlockingQueue;
-
- public class MessagePool {
- public static Queue<String> clintmessageQueue = new LinkedBlockingQueue<String>();
- public static Queue<String> serverMessageQueue = new LinkedBlockingQueue<String>();
-
- }
Interface
- package com.pool;
-
- public interface IMessagePool {
-
- public void addMessage(String message);
-
- public String pollMessage();
-
- public boolean isEmpty();
-
- }
Implementation class
- package com.pool.impl;
-
- import com.pool.IMessagePool;
- import com.pool.MessagePool;
-
- public class ClientMessagePool implements IMessagePool {
- @Override
- public void addMessage(String message) {
- MessagePool.clintmessageQueue.add(message);
- }
- @Override
- public String pollMessage() {
- return MessagePool.clintmessageQueue.poll();
- }
-
- @Override
- public boolean isEmpty() {
- if(MessagePool.clintmessageQueue.size()>0)
- return true;
- else
- return false;
- }
- }