Package org.mortbay.util.ajax

Examples of org.mortbay.util.ajax.Continuation


        long timeout = getReadTimeout(request);
        if (log.isDebugEnabled()) {
            log.debug("doMessage timeout="+timeout);
        }
       
        Continuation continuation = ContinuationSupport.getContinuation(request, client);
        Listener listener = getListener(request);
        if (listener!=null && continuation!=null && !continuation.isPending())
            listener.access();

        Message message = null;
        synchronized (client) {

            List consumers = client.getConsumers();
            MessageAvailableConsumer consumer = null;

            // Look for a message that is ready to go
            for (int i = 0; message == null && i < consumers.size(); i++) {
                consumer = (MessageAvailableConsumer) consumers.get(i);
                if (consumer.getAvailableListener() == null)
                    continue;

                // Look for any available messages
                message = consumer.receiveNoWait();
                if (log.isDebugEnabled()) {
                    log.debug("received "+message+" from "+consumer);
                }
            }

            // Get an existing Continuation or create a new one if there are no
            // messages

            if (message == null) {
                // register this continuation with our listener.
                listener.setContinuation(continuation);

                // Get the continuation object (may wait and/or retry
                // request here).
                continuation.suspend(timeout);
            }
            listener.setContinuation(null);

            // prepare the responds
            response.setContentType("text/xml");
View Full Code Here


            if (log.isDebugEnabled()) {
                log.debug("Receiving message(s) from: " + destination + " with timeout: " + timeout);
            }

            MessageAvailableConsumer consumer = (MessageAvailableConsumer) client.getConsumer(destination);
            Continuation continuation = null;
            Listener listener = null;
            Message message = null;

            synchronized (consumer) {
                // Fetch the listeners
                listener = (Listener) consumer.getAvailableListener();
                if (listener == null) {
                    listener = new Listener(consumer);
                    consumer.setAvailableListener(listener);
                }
                // Look for any available messages
                message = consumer.receiveNoWait();

                // Get an existing Continuation or create a new one if there are
                // no events.
                if (message == null) {
                    continuation = ContinuationSupport.getContinuation(request, consumer);

                    // register this continuation with our listener.
                    listener.setContinuation(continuation);

                    // Get the continuation object (may wait and/or retry
                    // request here).
                    continuation.suspend(timeout);
                }

                // Try again now
                if (message == null)
                    message = consumer.receiveNoWait();
View Full Code Here

            if (log.isDebugEnabled()) {
                log.debug("Receiving message(s) from: " + destination + " with timeout: " + timeout);
            }

            MessageAvailableConsumer consumer = (MessageAvailableConsumer) client.getConsumer(destination);
            Continuation continuation = null;
            Listener listener = null;
            Message message = null;

            // write a responds
            response.setContentType("text/xml");
View Full Code Here

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        try
        {
            final Continuation continuation = ContinuationSupport.getContinuation(request, mutex);
            synchronized (mutex)
            {
                MessageReceiver receiver = getReceiverForURI(request);

                MuleMessage requestMessage = receiver.createMuleMessage(request);
                requestMessage.setOutboundProperty(HttpConnector.HTTP_METHOD_PROPERTY, request.getMethod());

                //This will allow Mule to continue the response once the service has do its processing
                requestMessage.setReplyTo(continuation);
                setupRequestMessage(request, requestMessage, receiver);

                //we force asynchronous in the {@link #routeMessage} method
                routeMessage(receiver, requestMessage, request);

                continuation.suspend(10000);
            }

            writeResponse(response, (MuleMessage) continuation.getObject());
        }
        catch (Exception e)
        {
            throw new ServletException(e);
        }
View Full Code Here

    }

    @Override
    public void processReplyTo(MuleEvent event, MuleMessage returnMessage, Object replyTo) throws MuleException
    {
        Continuation continuation = (Continuation)replyTo;
        continuation.setObject(returnMessage);
        continuation.resume();
    }
View Full Code Here

    public String getAuthMethod() {
        return this.endpoint.getAuthMethod();
    }
   
    public void process(MessageExchange exchange) throws Exception {
        Continuation cont = (Continuation) locks.remove(exchange.getExchangeId());
        if (cont != null) {
            synchronized (cont) {
                if (log.isDebugEnabled()) {
                    log.debug("Resuming continuation for exchange: " + exchange.getExchangeId());
                }
                exchanges.put(exchange.getExchangeId(), exchange);
                cont.resume();
            }
        } else {
            throw new IllegalStateException("Exchange not found");
        }
    }
View Full Code Here

        if (!"POST".equals(request.getMethod())) {
            response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, request.getMethod() + " not supported");
            return;
        }
        // Not giving a specific mutex will synchronize on the contination itself
        Continuation cont = ContinuationSupport.getContinuation(request, null);
        MessageExchange exchange;
        // If the continuation is not a retry
        if (!cont.isPending()) {
            try {
                SoapMessage message = soapHelper.getSoapMarshaler().createReader().read(
                                            request.getInputStream(),
                                            request.getHeader(Constants.HEADER_CONTENT_TYPE));
                Context context = soapHelper.createContext(message);
                if (request.getUserPrincipal() != null) {
                    if (request.getUserPrincipal() instanceof JaasJettyPrincipal) {
                        Subject subject = ((JaasJettyPrincipal) request.getUserPrincipal()).getSubject();
                        context.getInMessage().setSubject(subject);
                    } else {
                        context.getInMessage().addPrincipal(request.getUserPrincipal());
                    }
                }
                request.setAttribute(Context.class.getName(), context);
                exchange = soapHelper.onReceive(context);
                NormalizedMessage inMessage = exchange.getMessage("in");
                inMessage.setProperty(JbiConstants.PROTOCOL_HEADERS, getHeaders(request));
                locks.put(exchange.getExchangeId(), cont);
                request.setAttribute(MessageExchange.class.getName(), exchange.getExchangeId());
                synchronized (cont) {
                    ((BaseLifeCycle) endpoint.getServiceUnit().getComponent().getLifeCycle()).sendConsumerExchange(exchange, endpoint);
                    if (exchanges.remove(exchange.getExchangeId()) == null) {
                        if (log.isDebugEnabled()) {
                            log.debug("Suspending continuation for exchange: " + exchange.getExchangeId());
                        }
                        // TODO: make this timeout configurable
                        boolean result = cont.suspend(1000 * 60); // 60 s
                        if (!result) {
                            throw new Exception("Error sending exchange: aborted");
                        }
                    }
                    request.removeAttribute(MessageExchange.class.getName());
                }
            } catch (SoapFault fault) {
                sendFault(fault, request, response);
                return;
            }
        } else {
            String id = (String) request.getAttribute(MessageExchange.class.getName());
            exchange = (MessageExchange) exchanges.remove(id);
            request.removeAttribute(MessageExchange.class.getName());
            boolean result = cont.suspend(0);
            // Check if this is a timeout
            if (exchange == null) {
                throw new IllegalStateException("Exchange not found");
            }
            if (!result) {
View Full Code Here

                }

                setCurrentConnection(this);
                long io = 0;

                Continuation continuation = _request.getContinuation();
                if (continuation != null && continuation.isPending())
                {
                    Log.debug("resume continuation {}",continuation);
                    if (_request.getMethod() == null)
                        throw new IllegalStateException();
                    handleRequest();
                }
                else
                {
                    // If we are not ended then parse available
                    if (!_parser.isComplete())
                        io = _parser.parseAvailable();

                    // Do we have more generating to do?
                    // Loop here because some writes may take multiple steps and
                    // we need to flush them all before potentially blocking in
                    // the
                    // next loop.
                    while (_generator.isCommitted() && !_generator.isComplete())
                    {
                        long written = _generator.flush();
                        io += written;
                        if (written <= 0)
                            break;
                        if (_endp.isBufferingOutput())
                            _endp.flush();
                    }

                    // Flush buffers
                    if (_endp.isBufferingOutput())
                    {
                        _endp.flush();
                        if (!_endp.isBufferingOutput())
                            no_progress = 0;
                    }

                    if (io > 0)
                        no_progress = 0;
                    else if (no_progress++ >= 2)
                        return;
                }
            }
            catch (HttpException e)
            {
                if (Log.isDebugEnabled())
                {
                    Log.debug("uri=" + _uri);
                    Log.debug("fields=" + _requestFields);
                    Log.debug(e);
                }
                _generator.sendError(e.getStatus(),e.getReason(),null,true);

                _parser.reset(true);
                _endp.close();
                throw e;
            }
            finally
            {
                setCurrentConnection(null);

                more_in_buffer = _parser.isMoreInBuffer() || _endp.isBufferingInput();

                synchronized (this)
                {
                    _handling = false;

                    if (_destroy)
                    {
                        destroy();
                        return;
                    }
                }

                if (_parser.isComplete() && _generator.isComplete() && !_endp.isBufferingOutput())
                {
                    if (!_generator.isPersistent())
                    {
                        _parser.reset(true);
                        more_in_buffer = false;
                    }

                    reset(!more_in_buffer);
                    no_progress = 0;
                }

                Continuation continuation = _request.getContinuation();
                if (continuation != null && continuation.isPending())
                {
                    break;
                }
                else if (_generator.isCommitted() && !_generator.isComplete() && _endp instanceof SelectChannelEndPoint) // TODO
                                                                                                                         // remove
View Full Code Here

    public void process(MessageExchange exchange) throws Exception {
        // Receive the exchange response
        // First, check if the continuation has not been removed from the map,
        // which would mean it has timed out.  If this is the case, throw an exception
        // that will set the exchange status to ERROR.
        Continuation cont = locks.get(exchange.getExchangeId());
        if (cont == null) {
            throw new Exception("HTTP request has timed out for exchange: " + exchange.getExchangeId());
        }
        // synchronized block
        synchronized (cont) {
            if (locks.remove(exchange.getExchangeId()) == null) {
                throw new Exception("HTTP request has timed out for exchange: " + exchange.getExchangeId());
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Resuming continuation for exchange: " + exchange.getExchangeId());
            }
            // Put the new exchange
            exchanges.put(exchange.getExchangeId(), exchange);
            // Resume continuation
            cont.resume();
            if (!cont.isResumed()) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Could not resume continuation for exchange: " + exchange.getExchangeId());
                }
                exchanges.remove(exchange.getExchangeId());
                throw new Exception("HTTP request has timed out for exchange: " + exchange.getExchangeId());
View Full Code Here

            if (handleStaticResource(request, response)) {
                return;
            }
            // Not giving a specific mutex will synchronize on the continuation
            // itself
            Continuation cont = ContinuationSupport.getContinuation(request, null);
            // If the continuation is not a retry
            if (!cont.isPending()) {
                // Create the exchange
                exchange = createExchange(request);
                // Put the exchange in a map so that we can later retrieve it
                // We don't put the exchange on the request directly in case the JMS flow is involved
                // because the exchange coming back may not be the same object as the one send.
                exchanges.put(exchange.getExchangeId(), exchange);
                // Put the exchange id on the request to be able to retrieve the exchange later
                request.setAttribute(MessageExchange.class.getName(), exchange.getExchangeId());
                // Put the continuation in a map under the exchange id key
                locks.put(exchange.getExchangeId(), cont);
                synchronized (cont) {
                    // Send the exchange
                    send(exchange);
                    if (logger.isDebugEnabled()) {
                        logger.debug("Suspending continuation for exchange: " + exchange.getExchangeId());
                    }
                    // Suspend the continuation for the configured timeout
                    // If a SelectConnector is used, the call to suspend will throw a RetryRequest exception
                    // else, the call will block until the continuation is resumed
                    long to = this.timeout;
                    if (to == 0) {
                        to = ((HttpComponent) getServiceUnit().getComponent()).getConfiguration().getConsumerProcessorSuspendTime();
                    }
                    boolean result = cont.suspend(to);
                    // The call has not thrown a RetryRequest, which means we don't use a SelectConnector
                    // and we must handle the exchange in this very method call.
                    // If result is false, the continuation has timed out.
                    // So get the exchange (in case the object has changed) and remove it from the map
                    exchange = exchanges.remove(exchange.getExchangeId());
                    // remove the exchange id from the request as we don't need it anymore
                    request.removeAttribute(MessageExchange.class.getName());
                    // If a timeout occurred, throw an exception that will be sent back to the HTTP client
                    // Whenever the exchange comes back, the process(MessageExchange) method will thrown an
                    // exception and the exchange will be set in an ERROR status
                    if (!result) {
                        // Remove the continuation from the map.
                        // This indicates the continuation has been fully processed
                        locks.remove(exchange.getExchangeId());
                        throw new Exception("Exchange timed out");
                    }
                }
            // The continuation is a retry.
            // This happens when the SelectConnector is used and in two cases:
            //  * the continuation has been resumed because the exchange has been received
            //  * the continuation has timed out
            } else {
                synchronized (cont) {
                    // Get the exchange id from the request
                    String id = (String) request.getAttribute(MessageExchange.class.getName());
                    // Remove the continuation from the map, indicating it has been processed or timed out
                    locks.remove(id);
                    exchange = exchanges.remove(id);
                    request.removeAttribute(MessageExchange.class.getName());
                    // Check if this is a timeout
                    if (exchange == null) {
                        throw new IllegalStateException("Exchange not found");
                    }
                    if (!cont.isResumed()) {
                        throw new Exception("Exchange timed out: " + exchange.getExchangeId());
                    }
                }
            }
            // At this point, we have received the exchange response,
View Full Code Here

TOP

Related Classes of org.mortbay.util.ajax.Continuation

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.