Package org.jboss.errai.bus.server

Examples of org.jboss.errai.bus.server.MessageQueue


    }

    private void pollForMessages(HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse, boolean wait) throws IOException {
        try {
            final MessageQueue queue = service.getBus().getQueue(httpServletRequest.getSession().getId());

            if (queue == null) {
                sendDisconnectWithReason(httpServletResponse.getOutputStream(),
                        "There is no queue associated with this session.");
                return;
            }

            queue.heartBeat();

            List<MarshalledMessage> messages = queue.poll(wait).getMessages();

            httpServletResponse.setHeader("Cache-Control", "no-cache");
            httpServletResponse.addHeader("Payload-Size", String.valueOf(messages.size()));
            httpServletResponse.setContentType("application/json");
            OutputStream stream = httpServletResponse.getOutputStream();
View Full Code Here


            context.addCometHandler(handler);
        } else {

            boolean post = "POST".equals(request.getMethod());

            MessageQueue queue = getQueue(session, !post);

            if (queue != null) {
                synchronized (activeEvents) {
                    Set<HttpServletResponse> responses = activeEvents.get(session);

                    if (!post && queue.messagesWaiting()) {
                        context.notify(getMessages(response, queue), CometEvent.NOTIFY, handler);
                    }

                    if (!queueToSession.containsKey(queue))
                        queueToSession.put(queue, session);
View Full Code Here

    private MessageQueue getQueue(QueueSession session, boolean pause) {

        System.out.println("getQueue !!!!!! " + service  + " " +  service.getBus() + " " +
                session.getSessionId() + " " + service.getBus().getQueue(session.getSessionId()));

        MessageQueue queue = service.getBus().getQueue(session.getSessionId());

        System.out.println("getQueue !!!!!! " + pause + ":pause, queue is " + queue == null);
       
        if (pause && queue != null && queue.getActivationCallback() == null) {

            System.out.println("getQueue !!!!!! in first if");

            queue.setActivationCallback(new QueueActivationCallback() {
                volatile boolean resumed = false;

                public void activate(MessageQueue queue) {
                    System.out.println("getQueue !!!!!! activate");

                    if (resumed) {
                        return;
                    }
                    resumed = true;
                    queue.setActivationCallback(null);

                    try {

                        System.out.println("getQueue !!!!!! first try block");

                        Set<HttpServletResponse> activeSessEvents;
                        QueueSession session;
                        session = queueToSession.get(queue);
                        if (session == null) {
                            System.out.println("getQueue !!!!!! session is null");

                            queue.stopQueue();
                            return;
                        }

                        activeSessEvents = activeEvents.get(queueToSession.get(queue));
View Full Code Here

    private void pollForMessages(HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse, boolean wait) throws IOException {
        try {

            final MessageQueue queue = service.getBus().getQueue(httpServletRequest.getSession().getId());

            if (queue == null) {
                sendDisconnectWithReason(httpServletResponse.getOutputStream(),
                        "There is no queue associated with this session.");
                return;
            }

            queue.heartBeat();

            List<MarshalledMessage> messages = queue.poll(wait).getMessages();

            httpServletResponse.setHeader("Cache-Control", "no-cache");
            httpServletResponse.addHeader("Payload-Size", String.valueOf(messages.size()));
            httpServletResponse.setContentType("application/json");
            OutputStream stream = httpServletResponse.getOutputStream();
View Full Code Here

     */
    public void event(final CometEvent event) throws IOException, ServletException {
        final HttpServletRequest request = event.getHttpServletRequest();
        final QueueSession session = sessionProvider.getSession(request.getSession());

        MessageQueue queue;
        switch (event.getEventType()) {
            case BEGIN:
                boolean post = "POST".equals(request.getMethod());
                if ((queue = getQueue(session, !post)) != null) {
                    synchronized (activeEvents) {
                        Set<CometEvent> events = activeEvents.get(session);


                        if (post) {
                            // do not pause incoming messages.
                            break;
                        } else if (queue.messagesWaiting()) {
                            transmitMessages(event.getHttpServletResponse(), queue);
                            event.close();
                            break;
                        }

                        if (!queueToSession.containsKey(queue)) {
                            queueToSession.put(queue, session);
                        }

                        if (events == null) {
                            activeEvents.put(session, events = new HashSet<CometEvent>());
                        }
                        if (events.contains(event)) {
                            break;
                        } else {
                            event.setTimeout(30000);
                            events.add(event);
                        }
                    }
                } else {
                    sendDisconnectWithReason(event.getHttpServletResponse().getOutputStream(),
                            "There is no queue associated with this session.");
                }
                break;

            case END:
                event.close();
                synchronized (activeEvents) {
                    Set<CometEvent> evt = activeEvents.get(session);
                    if (evt != null && !evt.remove(event)) {
                        return;
                    }
                }

                if ((queue = getQueue(session, false)) != null) {
                    queue.heartBeat();
                } else {
                    return;
                }


                break;

            case ERROR:
                log.error("An Error Occured: " + (event != null ? event.getEventSubType() : "<null>"));

                queue = getQueue(session, false);
                if (queue == null) {
                    return;
                }

                synchronized (activeEvents) {
                    Set<CometEvent> evt = activeEvents.get(session);
                    if (evt != null && !evt.remove(event)) {
                        return;
                    }
                }

                if (event.getEventSubType() == CometEvent.EventSubType.TIMEOUT) {
                    if (queue != null) queue.heartBeat();
                } else {
                    if (queue != null) {
                        queueToSession.remove(queue);
                        service.getBus().closeQueue(session.getSessionId());
                        activeEvents.remove(session);
View Full Code Here

            }

            return messagesSent;
        }
        catch (IOException e) {
            MessageQueue queue = service.getBus().getQueue(request.getSession().getId());
            if (queue != null) {
                queue.stopQueue();
            }
            e.printStackTrace();
            return -1;
        }
    }
View Full Code Here

        }
    }


    private MessageQueue getQueue(QueueSession session, boolean pause) {
        MessageQueue queue = service.getBus().getQueue(session.getSessionId());
        if (pause && queue != null && queue.getActivationCallback() == null) {
            queue.setActivationCallback(new QueueActivationCallback() {
                volatile boolean resumed = false;

                public void activate(MessageQueue queue) {
                    //      log.info("Resume...");
                    if (resumed) {
                        return;
                    }
                    resumed = true;
                    queue.setActivationCallback(null);

                    try {
                        Set<CometEvent> activeSessEvents;
                        QueueSession session;
                        session = queueToSession.get(queue);
                        if (session == null) {
                            log.error("Could not resume: No session.");
                            queue.stopQueue();
                            return;
                        }

                        activeSessEvents = activeEvents.get(queueToSession.get(queue));
View Full Code Here

     */
    public void event(final HttpEvent event) throws IOException, ServletException {
        final HttpServletRequest request = event.getHttpServletRequest();
        final QueueSession session = sessionProvider.getSession(request.getSession());

        MessageQueue queue;
        switch (event.getType()) {
            case BEGIN:
                boolean post = "POST".equals(request.getMethod());
                queue = getQueue(session, !post);
                if (queue == null) {
                    sendDisconnectWithReason(event.getHttpServletResponse().getOutputStream(),
                            "There is no queue associated with this session.");
                }

                synchronized (activeEvents) {

                    if (post) {
                        // do not pause incoming messages.
                        break;
                    } else if (queue.messagesWaiting()) {
                        transmitMessages(event.getHttpServletResponse(), queue);
                        event.close();
                        break;
                    }

                    if (!queueToSession.containsKey(queue)) {
                        queueToSession.put(queue, session);
                    }


                    Set<HttpEvent> events = activeEvents.get(session);
                    if (events == null) {
                        activeEvents.put(session, events = new LinkedHashSet<HttpEvent>());
                    }

                    if (events.contains(event)) {
                        event.close();
                    } else {
                        events.add(event);
                    }
                }
                break;


            case END:
                if ((queue = getQueue(session, false)) != null) {
                    queue.heartBeat();
                }

                synchronized (activeEvents) {
                    Set<HttpEvent> evt = activeEvents.get(session);
                    if (evt != null) {
                        evt.remove(event);
                    }
                }

                event.close();
                break;

            case EOF:
                event.close();
                break;

            case TIMEOUT:
            case ERROR:
                queue = getQueue(session, false);

                synchronized (activeEvents) {
                    Set<HttpEvent> evt = activeEvents.get(session);
                    if (evt != null) {
                        evt.remove(event);
                    }
                }

                if (event.getType() == HttpEvent.EventType.TIMEOUT) {
                    if (queue != null) queue.heartBeat();
                } else {
                    if (queue != null) {
                        queueToSession.remove(queue);
                        service.getBus().closeQueue(session.getSessionId());
                        //   session.invalidate();
View Full Code Here

        return messagesSent;
    }


    private MessageQueue getQueue(QueueSession session, boolean post) {
        MessageQueue queue = service.getBus().getQueue(session.getSessionId());

        if (post && queue != null && queue.getActivationCallback() == null) {
            queue.setActivationCallback(new QueueActivationCallback() {
                boolean resumed = false;

                public void activate(MessageQueue queue) {
                    if (resumed) {
                        //            log.info("Blocking");
                        return;
                    }
                    resumed = true;
                    queue.setActivationCallback(null);

                    //     log.info("Attempt to resume queue: " + queue.hashCode());
                    try {
                        Set<HttpEvent> activeSessEvents;
                        QueueSession session;
View Full Code Here

    }

    private void pollForMessages(HttpServletRequest httpServletRequest,
                                 HttpServletResponse httpServletResponse, boolean wait) throws IOException {
        try {
            final MessageQueue queue = service.getBus().getQueue(httpServletRequest.getSession().getId());

            if (queue == null) {
                sendDisconnectWithReason(httpServletResponse.getOutputStream(),
                        "There is no queue associated with this session.");
            }

            synchronized (queue) {

                if (wait) {
                    final Continuation cont = ContinuationSupport.getContinuation(httpServletRequest, queue);

                    if (!queue.messagesWaiting()) {

                        queue.setActivationCallback(new QueueActivationCallback() {
                            public void activate(MessageQueue queue) {
                                queue.setActivationCallback(null);
                                cont.resume();
                            }
                        });

                        if (!queue.messagesWaiting()) {
                            cont.suspend(45 * 1000);
                        }
                    } else {
                        queue.setActivationCallback(null);
                    }

                }

                pollQueue(queue, httpServletRequest, httpServletResponse);
View Full Code Here

TOP

Related Classes of org.jboss.errai.bus.server.MessageQueue

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.