Example usage for java.util.concurrent ConcurrentLinkedQueue iterator

List of usage examples for java.util.concurrent ConcurrentLinkedQueue iterator

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentLinkedQueue iterator.

Prototype

public Iterator<E> iterator() 

Source Link

Document

Returns an iterator over the elements in this queue in proper sequence.

Usage

From source file:com.interacciones.mxcashmarketdata.driver.queue.QueueWriteFile.java

public void ReadQueue(ConcurrentLinkedQueue<String> msgQueue) {
    LOGGER.debug("Reading from queue... Sizing " + msgQueue.size());

    while (true) {
        Iterator<String> it = msgQueue.iterator();

        while (it.hasNext()) {
            String msg = (String) it.next();
            //messageProcessing.receive(msg.toString());
            msgQueue.poll();/*ww w .j  a va2  s .co m*/
        }

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
            LOGGER.error("Error: " + e.getMessage());
        }
    }
}

From source file:com.interacciones.mxcashmarketdata.mama.queue.QueueReader.java

public void ReadQueue(ConcurrentLinkedQueue<Parser> msgQueue) {

    LOGGER.debug("Reading from queue... Sizing " + msgQueue.size());

    while (true) {
        Iterator<Parser> it = msgQueue.iterator();

        while (it.hasNext()) {
            Parser msg = (Parser) it.next();
            LOGGER.debug("Message Type: " + msg.TypeMessage());
            LOGGER.debug("Symbol (Emisora):" + msg.Emisora());
            sendMessage.sendMessage(msg);
            msgQueue.poll();/*w  w w.  j  a v  a  2s.co m*/
        }

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
            LOGGER.error("Error: " + e.getMessage());
        }
    }
}

From source file:com.clican.pluto.cluster.base.BaseMessageDispatcher.java

public void dispatch(Message msg) throws SynchronizeException {
    String msgName = msg.getName();
    ConcurrentLinkedQueue<IMessageHandler> handlerQueue = messageHandlerMapping.get(msgName);
    if (handlerQueue != null && handlerQueue.size() != 0) {
        Iterator<IMessageHandler> it = handlerQueue.iterator();
        List<IMessageHandler> executedHandlerList = new ArrayList<IMessageHandler>();
        while (it.hasNext()) {
            try {
                IMessageHandler handler = it.next();
                handler.handle(msg);/* w  ww  .  j  a  v a 2s  . c om*/
                executedHandlerList.add(handler);
            } catch (SynchronizeException e) {
                for (IMessageHandler handler : executedHandlerList) {
                    try {
                        handler.rollback(msg);
                    } catch (Throwable t) {
                        log.error("", e);
                    }
                }
                throw e;
            }
        }
    }
}

From source file:com.chinamobile.bcbsp.comm.Receiver.java

/**
 * Combine the incoming message queue.//from   w w w.j  ava2  s  .  c  o  m
 * @return IMessage
 */
@SuppressWarnings("unchecked")
private IMessage combine(ConcurrentLinkedQueue<IMessage> incomingQueue) {
    IMessage msg = (IMessage) this.combiner.combine(incomingQueue.iterator());
    return msg;
}

From source file:com.mobilehelix.appserver.push.PushManager.java

public void removeSession(String uniqueID, String combinedUser) {
    idMap.remove(uniqueID);/* ww w .ja  v  a 2 s. c o m*/
    ConcurrentLinkedQueue<PushReceiver> receivers = this.userPushMap.get(combinedUser);
    if (receivers != null) {
        Integer idx = 0;
        Integer nsessions = receivers.size();
        Iterator<PushReceiver> it = receivers.iterator();
        while (it.hasNext()) {
            PushReceiver pr = it.next();
            if (pr.getUniqueID().equals(uniqueID)) {
                LOG.log(Level.WARNING, "Removing push session {0} or {1} for combined user {3}",
                        new Object[] { idx, nsessions, combinedUser });
                receivers.iterator().remove();
                break;
            }
            ++idx;
        }
    }
}

From source file:com.chinamobile.bcbsp.fault.storage.Checkpoint.java

/**
 * write the message and graphdata information for
 * migrate slow staff./* w  ww .ja v a 2 s .c  o m*/
 * @param communicator
 *        message information need to backup
 * @param graphData
 *        graphdata to backup
 * @param writePath
 *        backup write path
 * @param job
 *        job to backup
 * @param staff
 *        staff to backup
 * @return write result if success and nothing to write true
 *         not false
 * @throws IOException
 *         exceptions when write messages
 * @author liuzhicheng
 */
public boolean writeMessages(CommunicatorInterface communicator, GraphDataInterface graphData, Path writePath,
        BSPJob job, GraphStaffHandler graphStaffHandler, Staff staff,
        ConcurrentLinkedQueue<String> messagesQueue) throws IOException, InterruptedException {
    //    if (communicator.getIncomedQueuesSize() == 0) {
    //       LOG.info("Feng test! writeMessages "+communicator.getIncomedQueuesSize());
    //      return true;
    //    }
    //    LOG.info("The init write path is : " + writePath.toString());
    //    BSPHdfs HDFSCheckpoint = new BSPHdfsImpl();
    //    BSPoutHdfs OUT = new BSPoutHdfsImpl();
    //    OUT.fsDataOutputStream(writePath, HDFSCheckpoint.getConf());
    //    StringBuffer sb = new StringBuffer();
    //boolean writeResult = false;
    try {
        //Staff staff = graphStaffHandler;
        //    OutputFormat outputformat = (OutputFormat) ReflectionUtils.newInstance(
        //            job.getConf().getClass(Constants.USER_BC_BSP_JOB_OUTPUT_FORMAT_CLASS,
        //                OutputFormat.class), job.getConf());
        //        outputformat.initialize(job.getConf());
        //        RecordWriter output = outputformat.getRecordWriter(job,
        //            staff.getStaffAttemptId(), writePath);
        LOG.info("The init write path is : " + writePath.toString());
        BSPHdfs HDFSCheckpoint = new BSPHdfsImpl();
        BSPoutHdfs OUT = new BSPoutHdfsImpl();
        OUT.fsDataOutputStream(writePath, HDFSCheckpoint.getConf());
        StringBuffer sb = new StringBuffer();

        ConcurrentLinkedQueue<String> messages = messagesQueue;
        Iterator<String> it = messages.iterator();
        //        if(messages.size()!=0){
        //           LOG.info("Feng test writeMessages messages "+it.next());
        //           //continue;
        //        }
        while (it.hasNext()) {
            String message = it.next();
            LOG.info("Write messages " + message);
            sb.append(message);
            sb.append("\n");
        }
        //        Iterator<IMessage> messagesIter = messages.iterator();
        //        StringBuffer sb = new StringBuffer();
        //        //sb.append(vertexID + Constants.MESSAGE_SPLIT);
        //       while (messagesIter.hasNext()) {
        //          IMessage msg = messagesIter.next();
        //          String info = msg.intoString();
        //          if (info != null) {
        //           sb.append(info + Constants.SPACE_SPLIT_FLAG);
        //         }
        //       }
        //       if (sb.length() > 0) {
        //           int k = sb.length();
        //           sb.delete(k - 1, k - 1);
        //         }
        //       //sb.append("\n");
        //       output.write(new Text(v.getVertexID() + Constants.MESSAGE_SPLIT), new Text(sb.toString()));
        //       LOG.info("Feng test! message record"+sb.toString());
        //        
        //    graphData.getAllVertex(graphStaffHandler,communicator,output);
        OUT.writeBytes(sb.toString());
        OUT.flush();
        OUT.close();
        // return true;
    } catch (IOException e) {
        LOG.error("Exception has happened and been catched!", e);
        return false;
    }
    return true;
    //    //Vertex<?, ?, Edge> vertex = graphData.getForAll(i);
    //    String vertexID = vertex.getVertexID().toString();
    //    Iterator<IMessage> it = communicator.getMessageIterator(vertexID);
    //    sb.append(vertexID + Constants.MESSAGE_SPLIT);
    //    while (it.hasNext()) {
    //      IMessage msg = it.next();
    //      String info = msg.intoString();
    //      if (info != null) {
    //        sb.append(info + Constants.SPACE_SPLIT_FLAG);
    //      }
    //    }
    //    sb.append("\n");
    //  }
    //  OUT.writeBytes(sb.toString());
    //  OUT.flush();
    //  OUT.close()
}

From source file:com.chinamobile.bcbsp.comm.CombinerTool.java

/** combine the message queues.
 * @param outgoingQueue// w w w. jav  a  2 s .co m
 */
private ConcurrentLinkedQueue<IMessage> combine(ConcurrentLinkedQueue<IMessage> outgoingQueue) {
    // Map of outgoing queues indexed by destination vertex ID.
    TreeMap<String, ConcurrentLinkedQueue<IMessage>> outgoingQueues = new TreeMap<String, ConcurrentLinkedQueue<IMessage>>();
    ConcurrentLinkedQueue<IMessage> tempQueue = null;
    IMessage tempMessage = null;
    // Traverse the outgoing queue and put the messages with the same
    // dstVertexID into the same queue in the tree map.
    Iterator<IMessage> iter = outgoingQueue.iterator();
    String dstVertexID = null;
    /**The result queue for return.*/
    ConcurrentLinkedQueue<IMessage> resultQueue = new ConcurrentLinkedQueue<IMessage>();
    while (iter.hasNext()) {
        tempMessage = iter.next();
        dstVertexID = tempMessage.getDstVertexID();
        tempQueue = outgoingQueues.get(dstVertexID);
        if (tempQueue == null) {
            tempQueue = new ConcurrentLinkedQueue<IMessage>();
        }
        tempQueue.add(tempMessage);
        outgoingQueues.put(dstVertexID, tempQueue);
    }
    // Do combine operation for each of the outgoing queues.
    for (Entry<String, ConcurrentLinkedQueue<IMessage>> entry : outgoingQueues.entrySet()) {
        tempQueue = entry.getValue();
        tempMessage = (IMessage) this.combiner.combine(tempQueue.iterator());
        resultQueue.add(tempMessage);
    }
    outgoingQueue.clear();
    outgoingQueues.clear();
    return resultQueue;
}

From source file:com.chinamobile.bcbsp.comm.Communicator.java

@Override
public Iterator<IMessage> getMessageIterator(String vertexID) throws IOException {
    ConcurrentLinkedQueue<IMessage> incomedQueue = messageQueues.removeIncomedQueue(vertexID);
    Iterator<IMessage> iterator = incomedQueue.iterator();
    return iterator;
}

From source file:com.chinamobile.bcbsp.graph.GraphDataMananger.java

@Override
public void getAllVertex(GraphStaffHandler graphStaffHandler, CommunicatorInterface communicator,
        RecordWriter output) throws IOException, InterruptedException {
    // TODO Auto-generated method stub
    try {//from   www  .  j av  a 2s.c o  m
        Vertex v = vertexClass.newInstance();
        for (int i = (MetaDataOfGraph.BCBSP_DISKGRAPH_HASHNUMBER - 1); i >= 0; i--) {
            int counter = MetaDataOfGraph.VERTEX_NUM_PERBUCKET[i];
            if (counter == 0) {
                continue;
            }
            this.prepareBucket(i, this.lastSuperstepCounter + 1);
            graphStaffHandler.preBucket(i, this.lastSuperstepCounter + 1);
            for (int j = 0; j < counter; j++) {
                fillVertex(v);
                LOG.info("Feng test! vertex is null " + v.getVertexID().toString());
                //this.vertexlist.add(v);
                //graphStaffHandler.vertexProcessing(v, bsp, job, superStepCounter,
                //  context, true);
                //      //Vertex<?, ?, Edge> vertex = graphData.getForAll(i);
                String vertexID = v.getVertexID().toString();
                //Iterator<IMessage> it = communicator.getMessageIterator(vertexID);
                ConcurrentLinkedQueue<IMessage> messages = communicator
                        .getMessageQueue(String.valueOf(v.getVertexID()));
                if (messages.size() == 0) {
                    LOG.info("Feng test vertex " + v.getVertexID().toString() + " message is empty!");
                    continue;
                }
                Iterator<IMessage> messagesIter = messages.iterator();
                StringBuffer sb = new StringBuffer();
                //sb.append(vertexID + Constants.MESSAGE_SPLIT);
                while (messagesIter.hasNext()) {
                    IMessage msg = messagesIter.next();
                    String info = msg.intoString();
                    if (info != null) {
                        sb.append(info + Constants.SPACE_SPLIT_FLAG);
                    }
                }
                if (sb.length() > 0) {
                    int k = sb.length();
                    sb.delete(k - 1, k - 1);
                }
                //sb.append("\n");
                output.write(new Text(v.getVertexID() + Constants.MESSAGE_SPLIT), new Text(sb.toString()));
            }

            //  OUT.close()
            this.vManager.processVertexSave(v);

            this.finishPreparedBucket();
        }
    } catch (InstantiationException e) {
        throw new RuntimeException("[Graph Data Manageer] saveAllVertices", e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException("[Graph Data Manageer] saveAllVertices", e);
    }
}

From source file:com.chinamobile.bcbsp.comm.CombinerTool.java

/** run method. */
public void run() {
    LOG.info("[CombinerTool] Start!");
    long time = 0;
    long totalBeforeSize = 0;
    long totalAfterSize = 0;
    String outgoIndex = null;// w  w  w.j a  v a  2s  . c  om
    ConcurrentLinkedQueue<IMessage> maxQueue = null;
    int maxSize = 0;
    int lastCount = 0;
    while (!this.sender.getNoMoreMessagesFlag()) {
        outgoIndex = this.messageQueues.getMaxOutgoingQueueIndex();
        if (outgoIndex != null) {
            maxSize = this.messageQueues.getOutgoingQueueSize(outgoIndex);
        }
        lastCount = (this.combinedCountsMap.get(outgoIndex) == null ? 0
                : this.combinedCountsMap.get(outgoIndex));
        // If new updated size is over the threshold
        if ((maxSize - lastCount) > this.combineThreshold) {
            // Get the queue out of the map.
            maxQueue = this.messageQueues.removeOutgoingQueue(outgoIndex);
            if (maxQueue == null) {
                continue;
            }
            long start = System.currentTimeMillis();
            /* Clock */
            // Combine the messages.
            maxQueue = combine(maxQueue);
            long end = System.currentTimeMillis();
            /* Clock */
            time = time + end - start;
            int maxSizeBefore = maxSize;
            totalBeforeSize += maxSizeBefore;
            maxSize = maxQueue.size();
            totalAfterSize += maxSize;
            // Note the count after combination.
            this.combinedCountsMap.put(outgoIndex, maxQueue.size());
            // Put the combined messages back to the outgoing queue.
            Iterator<IMessage> iter = maxQueue.iterator();
            while (iter.hasNext()) {
                this.messageQueues.outgoAMessage(outgoIndex, iter.next());
            }
            maxQueue.clear();
        } else {
            try {
                // Wait for 500ms until next check.
                Thread.sleep(500);
            } catch (Exception e) {
                throw new RuntimeException("[[CombinerTool] run exception", e);
            }
        }
    }
    LOG.info("[CombinerTool] has combined totally (" + totalBeforeSize + ") messages into (" + totalAfterSize
            + "). Compression rate = " + (float) totalAfterSize * 100 / totalBeforeSize + "%.");
    LOG.info("[CombinerTool] has used time: " + time / TIME_CHANGE + " seconds totally!");
    LOG.info("[CombinerTool] Die!");
}