`
jojo_java
  • 浏览: 93464 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

MessageQueue实现

    博客分类:
  • JAVA
 
阅读更多
package me2.jojo.MessageQueue;

import me2.core.data.ResultMessage;

/**
 * Description:修改指针的实现<br>
 * 
 * @author JOJO
 * @version 0.1
 */
public class ResultMsgQueue
{

    private int       size;

    private int       currCount;

    private int       dSize;

    private int       priority;

    private ResultMessage elements[];

    private int       next[];

    private int[]     previous;

    private int       p[][];

    private int       emptyP_Head;

    private int       emptyP_Tail;

    public ResultMsgQueue(int _size, int _priority)
    {
        size = _size;

        currCount = 0;

        priority = _priority;

        dSize = size / 4;

        elements = new ResultMessage[size];

        next = new int[size];

        previous = new int[size];

        p = new int[priority][2];

        for (int i = 0; i < priority; i++)
        {
            p[i][0] = -1;
            p[i][1] = -1;
        }

        for (int i = 0; i < size; i++)
        {
            next[i] = i + 1;
        }
        emptyP_Head = 0;
        emptyP_Tail = size - 1;
    }

    public ResultMsgQueue()
    {
        this(8, 3);
    }

    private/* synchronized */int allocate ()
    {

        int index = emptyP_Head;

        if (emptyP_Head != emptyP_Tail)
        {
            emptyP_Head = next[emptyP_Head];
        }
        else
        {
            emptyP_Tail = emptyP_Head = -1;
        }
        return index;
    }

    public synchronized int put (ResultMessage element)
    {
        int index = allocate();
        if (index == -1)
        {
            // printAll();
            this.distoryPolicy(dSize);
            return -1;
        }
        int priority = element.getPriority();
        elements[index] = element;
        currCount++;
        if (p[priority][0] == -1)
        {
            p[priority][0] = index;
            // 挂接到前面的指针
            int[] previousP = this.getPreviousPointer(priority);
            if (previousP != null)
            {
                next[previousP[1]] = index;
                previous[index] = previousP[1];
            }
        }
        else
        {
            // 插入操作
            next[p[priority][1]] = index;
            previous[index] = p[priority][1];
        }
        // 链接后面的指针
        int[] nextP = this.getNextPointer(priority);
        if (nextP != null)
        {
            next[index] = nextP[0];
            previous[nextP[0]] = index;
        }
        p[priority][1] = index;

        return index;
    }

    public synchronized ResultMessage get ()
    {
        ResultMessage element = null;
        for (int i = 0; i < priority; i++)
        {
            if (p[i][0] != -1)
            {
                int n = p[i][0];
                element = elements[n];
                elements[n] = null;
                currCount--;
                if (p[i][0] == p[i][1])
                {
                    p[i][0] = -1;
                    p[i][1] = -1;
                }
                else
                {
                    p[i][0] = next[n];
                }
                /** ************ */
                if (emptyP_Tail == -1)
                {
                    emptyP_Head = n;
                    emptyP_Tail = n;
                }
                else
                {
                    next[emptyP_Tail] = n;
                    emptyP_Tail = n;
                }
                /** ************ */
                return element;
            }
        }
        return element;
    }

    public void printAll ()
    {
        for (int i = 0; i < size; i++)
        {
            System.out.println(elements[i]);
        }
        System.out.println();
    }

    private synchronized void distoryPolicy (int size)
    {
        //System.out.println("总数:" + currCount);
        int[] lastP = this.getLastPointer();
        if (lastP != null)
        {
            int index = lastP[1];
            emptyP_Tail = index;
            while (size > 0)
            {
                int n = elements[index].getPriority();
                elements[index] = null;
                if (p[n][0] == p[n][1])
                {
                    p[n][0] = -1;// .reset();
                    p[n][1] = -1;
                }
                else
                {
                    p[n][1] = previous[index];
                }
                emptyP_Head = index;
                index = previous[index];
                currCount--;
                size--;
            }
        }
        //System.out.println("销毁之后:" + currCount);
    }

    /**
     * 返回当前队列中的报文数量
     * 
     * @return 队列中的报文数量
     */
    public int size ()
    {
        return currCount;
    }

    public void printFormFirst ()
    {
        ResultMessage element = null;
        while ((element = get()) != null)
        {
            System.out.println(element);
        }
        System.out.println();
    }

    private int[] getFirstPointer ()
    {
        return this.getNextPointer(-1);
    }

    private int[] getLastPointer ()
    {
        return this.getPreviousPointer(priority);
    }

    private int[] getNextPointer (int _priority)
    {
        int n = this.priority - 1;
        while (_priority < n)
        {
            if (p[++_priority][0] != -1) return p[_priority];
        }
        return null;
    }

    private int[] getPreviousPointer (int priority)
    {
        while (priority > 0)
        {
            if (p[--priority][1] != -1) return p[priority];
        }
        return null;
    }
}

 

分享到:
评论

相关推荐

    将Sun的Open Message Queue与Spring集成

    Open Message Queue是Sun Java System Message Queue的一个开源版本。Open message queue是一个企业级,可升级,非常成熟的消息服务器。它为面向消息的系统集成提供一套完整的JMS(Java Message Service )实现。...

    实例MSMQ(MessageQueue)

    C#实现微软的消息队列 分客户端和服务端

    spring-redis-mq, 基于 Spring 和 Redis 的分布式消息队列(MessageQueue)实现.zip

    spring-redis-mq, 基于 Spring 和 Redis 的分布式消息队列(MessageQueue)实现

    Handler+Looper+MessageQueue+Message机制

    作用: 跨线程通信,异步通信。...MessageQueue(消息队列):由Looper负责管理,管理Handler发送过来的Message,其底层实现采用的是单链表。 Handler(处理者):负责Message的发送及处理。通过 Handler.send

    RocketMQ实践:确保消息不丢失与顺序性的高效策略

    其次,保证消息顺序是通过将相关联的消息放入同一个MessageQueue实现的。此外,本文还涉及了处理消息积压的策略,这在面对大量未处理消息时尤为关键。最后,本文还介绍了RocketMQ的消息轨迹功能,这有助于更好地跟踪...

    MessageQueue.zip

    基于自编写的类Fourinone,实现了消息队列(MQ)的发送/接收、主题订阅两种经典消息服务模式,之后讨论了消息队列在解耦、冗余、扩展性、灵活性、可恢复性、顺序保证、缓冲、异步通信方面的优势,最后总结了消息队列...

    PHP实现基于Redis的MessageQueue队列封装操作示例

    本文实例讲述了PHP实现基于Redis的MessageQueue队列封装操作。分享给大家供大家参考,具体如下: Redis的链表List可以用来做链表,高并发的特性非常适合做分布式的并行消息传递。 项目地址:...

    save and forward message queue (SAFMQ)

    一个很不错的消息中间件的源码,使用C/C++实现,目前版本为8.0版本,强烈推荐!

    用消息队列实现Client和Server间的通信.rar_message queue_server client_消息队列 _消

    用消息队列实现Client和Server间的通信

    messagequeue

    实现一个进程内的队列引擎,单机可支持100万队列以上。 2.2 语言限定 JAVA和C++ 注意: Java和C++一起参与排名。 C++的Demo还在制作中(预计两天后可以出来),其核心逻辑与Java是一致的。选手们可以参考Java先行...

    message-queue:php的消息队列

    消息队列(Message Queue):把消息按照产生的次序加入队列,而由另外的处理程序/模块将其从队列中取出,并加以处理;从而形成了一个基本的消息队列。应用场景:短信服务、电子邮件服务、图片处理服务、好友动态推送...

    MessageQueue:使用套接字编程实现客户端-服务器体系结构

    MessageQueue 使用套接字编程实现客户端-服务器体系结构 支持多个客户端。 在单独的线程中服务每个客户端请求。 对于每个请求,服务器都会有一个有效的响应。

    远程调用服务(RPC)和消息(MessageQueue)对比

    远程调用服务(RPC)和消息(MessageQueue)对比在阿里的平台技术部参与开发了Dubbo(远程调用服务)和Napoli(消息解决方案),又给网站应用支持这2个产品很长一段时间,了解了这2个产品的实现及应用对这两个产品的用法。...

    String+tomcat+jms实例

    在tomcat 下的context.xml文件中加入 auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" description="JMS Connection Factory" ... physicalName="MyMessageQueue"/&gt;

    Android消息处理机制Looper和Handler详解

    Message:消息,其中包含了消息ID,消息处理对象以及处理的数据等,由MessageQueue统一列队,终由Handler处理。 Handler:处理者,负责Message的发送及处理。使用Handler时,需要实现handleMessage(Message msg)方法...

    rsmq-worker:可以简单地围绕RSMQ(Redis Simple Message Queue)实现一个工作程序的助手

    帮助程序可以简单地实现一个工作程序 。 :warning: 注意: RSMQ使用Redis EVAL命令(LUA脚本),因此最低Redis版本是2.6+ 。 安装 npm install rsmq-worker 初始化 new RSMQWorker ( queuename , options ) ; ...

    pg_message_queue.zip

    利用postgresql 的notify 机制来实现的一个队列应用

    消息队列(MSMQ)

    Message Queue(微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布于相连的网络空间中的任一位置。它的实现原理是:消息的发送者把自己想要...

    Android 面试宝典

    5. Android中的动画有哪几类,它们的特点和区别是什么? 两种.一种是Tween动画.还有一种是Frame动画. Tween动画,这种实现方式...4)线程:UI thread通常就是main thread,而Android启动程序时会替它建立一个Message Queue.

Global site tag (gtag.js) - Google Analytics