RabbitMQ(四)-设计一个简单的消息队列

453 阅读2分钟

下面我们用Java语言写一个简单 的消息队列。 由消息队列、消息生产者、消息消费者,客户端工具、消息队列服务端。

消息处理中心:

import java.util.concurrent.ArrayBlockingQueue;

/**
 * @author Spark Guo
 * @version 1.0
 * @Description TODO
 * @since 2020/7/21
 */
public class Broker {
    //设置存储消息的最大数量
    private final static int MAX_SIZE = 5;
    //保存消息的容器
    private static ArrayBlockingQueue<String> MassageQueue = new ArrayBlockingQueue<String>(MAX_SIZE);
    //生产消息
    public static void produce(String msg){
        if (MassageQueue.offer(msg)){
            System.out.println("成功向消息中心投递消息:"+msg+",当前暂存消息数目为"+MassageQueue.size());
        }else{
            System.out.println("消息中心已满,不能继续放入消息!");
        }
        System.out.println("==================================");
    }

    //消费消息
    public static String consume(){
        String msg = MassageQueue.poll();
        if(msg!=null){
            System.out.println("已经消费消息:"+msg+",当前暂存消息数目为"+MassageQueue.size());
        }else{
            System.out.println("消息处理中心已经没有消息可供消费!");
        }
        System.out.println("==================================");
        return msg;
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Spark Guo
 * @version 1.0
 * @Description TODO
 * @since 2020/7/21
 */
public class BrokerServer implements  Runnable {

    public static int SERVICE_PORT = 9999;
    private final Socket socket ;

    public BrokerServer(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try (
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(socket.getOutputStream());
        ){
            while (true){
                String str = in.readLine();
                if (str==null){
                    continue;
                }
                System.out.println("接收到的原始数据为:"+str);
                if (str.equals("CONSUME")){//CONSUME表示要消费一条消息
                    String msg = Broker.consume();
                    out.println(msg);
                    out.flush();
                }else{//其他情况都表示要生产消息到消息队列中
                    Broker.produce(str);
                }
            }

        } catch (IOException e) {
            System.out.println("服务器异常");
//            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(BrokerServer.SERVICE_PORT);
        while(true){
            BrokerServer bs = new BrokerServer(server.accept());
            new Thread(bs).start();
        }
    }

}

消息生产者:

/**
 * @author Spark Guo
 * @version 1.0
 * @Description TODO
 * @since 2020/7/21
 */
public class ProduceClient {

    public static void main(String[] args) throws Exception {
        MQClient client = new MQClient();
        client.produce("Hello World4!!");
    }

}

消息消费者:

public class ConsumeClient {
    public static void main(String[] args) throws Exception {
        MQClient client = new MQClient();
        String message = client.consume();
        System.out.println("获取的消息为:"+message);
    }
}

客户端工具类:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @author Spark Guo
 * @version 1.0
 * @Description TODO
 * @since 2020/7/21
 */
public class MQClient {

    //生产消息
    public static void produce(String msg) throws Exception {
        Socket socket = new Socket(InetAddress.getLocalHost(),BrokerServer.SERVICE_PORT);
        try (
                PrintWriter out = new PrintWriter(socket.getOutputStream());
        ){
            out.println(msg);
            out.flush();
        }
    }

    //消费消息
    public static String consume() throws Exception {
        Socket socket = new Socket(InetAddress.getLocalHost(),BrokerServer.SERVICE_PORT);
        try(BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream())){
            //先向消息队列发送CONSUME表示消费消息
            out.println("CONSUME");
            out.flush();
            //再从队列获取一条消息
            String message = in.readLine();
            return message;
        }
    }

}