Search in sources :

Example 1 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project camel by apache.

the class CamelOutputStream method asyncInvokeFromWorkQueue.

protected void asyncInvokeFromWorkQueue(final org.apache.camel.Exchange exchange) throws IOException {
    Runnable runnable = new Runnable() {

        public void run() {
            try {
                syncInvoke(exchange);
            } catch (Throwable e) {
                ((PhaseInterceptorChain) outMessage.getInterceptorChain()).abort();
                outMessage.setContent(Exception.class, e);
                ((PhaseInterceptorChain) outMessage.getInterceptorChain()).unwind(outMessage);
                MessageObserver mo = outMessage.getInterceptorChain().getFaultObserver();
                if (mo == null) {
                    mo = outMessage.getExchange().get(MessageObserver.class);
                }
                mo.onMessage(outMessage);
            }
        }
    };
    try {
        Executor ex = outMessage.getExchange().get(Executor.class);
        if (ex != null) {
            outMessage.getExchange().put(Executor.class.getName() + ".USING_SPECIFIED", Boolean.TRUE);
            ex.execute(runnable);
        } else {
            WorkQueueManager mgr = outMessage.getExchange().get(Bus.class).getExtension(WorkQueueManager.class);
            AutomaticWorkQueue qu = mgr.getNamedWorkQueue("camel-cxf-conduit");
            if (qu == null) {
                qu = mgr.getAutomaticWorkQueue();
            }
            // need to set the time out somewhere
            qu.execute(runnable);
        }
    } catch (RejectedExecutionException rex) {
        if (!hasLoggedAsyncWarning) {
            LOG.warn("Executor rejected background task to retrieve the response.  Suggest increasing the workqueue settings.");
            hasLoggedAsyncWarning = true;
        }
        LOG.info("Executor rejected background task to retrieve the response, running on current thread.");
        syncInvoke(exchange);
    }
}
Also used : Bus(org.apache.cxf.Bus) MessageObserver(org.apache.cxf.transport.MessageObserver) Executor(java.util.concurrent.Executor) AutomaticWorkQueue(org.apache.cxf.workqueue.AutomaticWorkQueue) WorkQueueManager(org.apache.cxf.workqueue.WorkQueueManager) IOException(java.io.IOException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 2 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project camel by apache.

the class CamelDestinationTest method testRoundTripDestinationWithFault.

@Test
public void testRoundTripDestinationWithFault() throws Exception {
    inMessage = null;
    EndpointInfo conduitEpInfo = new EndpointInfo();
    conduitEpInfo.setAddress("camel://direct:Producer");
    // set up the conduit send to be true
    CamelConduit conduit = setupCamelConduit(conduitEpInfo, true, false);
    final Message outMessage = new MessageImpl();
    endpointInfo.setAddress("camel://direct:EndpointA");
    final CamelDestination destination = setupCamelDestination(endpointInfo, true);
    destination.setCheckException(true);
    // set up MessageObserver for handling the conduit message
    MessageObserver observer = new MessageObserver() {

        public void onMessage(Message m) {
            try {
                Exchange exchange = new ExchangeImpl();
                exchange.setInMessage(m);
                m.setExchange(exchange);
                verifyReceivedMessage(m, "HelloWorld");
                //verifyHeaders(m, outMessage);
                // setup the message for
                Conduit backConduit;
                backConduit = getBackChannel(destination, m);
                // wait for the message to be got from the conduit
                Message replyMessage = new MessageImpl();
                replyMessage.setContent(Exception.class, new RuntimeCamelException());
                sendoutMessage(backConduit, replyMessage, true, "HelloWorld Fault");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    MockEndpoint error = context.getEndpoint("mock:error", MockEndpoint.class);
    error.expectedMessageCount(1);
    //this call will active the camelDestination
    destination.setMessageObserver(observer);
    // set is one way false for get response from destination
    // need to use another thread to send the request message
    sendoutMessage(conduit, outMessage, false, "HelloWorld");
    // wait for the message to be got from the destination,
    // create the thread to handler the Destination incoming message
    verifyReceivedMessage(inMessage, "HelloWorld Fault");
    error.assertIsSatisfied();
    destination.shutdown();
}
Also used : MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) WebServiceException(javax.xml.ws.WebServiceException) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.cxf.message.Exchange) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Conduit(org.apache.cxf.transport.Conduit) RuntimeCamelException(org.apache.camel.RuntimeCamelException) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 3 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project camel by apache.

the class CamelTransportTestSupport method setupCamelConduit.

protected CamelConduit setupCamelConduit(EndpointInfo endpointInfo, boolean send, boolean decoupled) {
    if (decoupled) {
    // setup the reference type
    } else {
        target = EasyMock.createMock(EndpointReferenceType.class);
    }
    CamelConduit camelConduit = new CamelConduit(context, bus, endpointInfo, target);
    if (send) {
        // setMessageObserver
        observer = new MessageObserver() {

            public void onMessage(Message m) {
                inMessage = m;
            }
        };
        camelConduit.setMessageObserver(observer);
    }
    return camelConduit;
}
Also used : EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message)

Example 4 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project cxf by apache.

the class ClientImpl method doInvoke.

private Object[] doInvoke(final ClientCallback callback, BindingOperationInfo oi, Object[] params, Map<String, Object> context, Exchange exchange) throws Exception {
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
    ClassLoaderHolder origLoader = null;
    try {
        ClassLoader loader = bus.getExtension(ClassLoader.class);
        if (loader != null) {
            origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
        }
        if (exchange == null) {
            exchange = new ExchangeImpl();
        }
        exchange.setSynchronous(callback == null);
        Endpoint endpoint = getEndpoint();
        if (LOG.isLoggable(Level.FINE)) {
            LOG.fine("Invoke, operation info: " + oi + ", params: " + Arrays.toString(params));
        }
        Message message = endpoint.getBinding().createMessage();
        // Make sure INVOCATION CONTEXT, REQUEST_CONTEXT and RESPONSE_CONTEXT are present
        // on message
        Map<String, Object> reqContext = null;
        Map<String, Object> resContext = null;
        if (context == null) {
            context = new HashMap<>();
        }
        reqContext = CastUtils.cast((Map<?, ?>) context.get(REQUEST_CONTEXT));
        resContext = CastUtils.cast((Map<?, ?>) context.get(RESPONSE_CONTEXT));
        if (reqContext == null) {
            reqContext = new HashMap<>(getRequestContext());
            context.put(REQUEST_CONTEXT, reqContext);
        }
        if (resContext == null) {
            resContext = new HashMap<>();
            context.put(RESPONSE_CONTEXT, resContext);
        }
        message.put(Message.INVOCATION_CONTEXT, context);
        setContext(reqContext, message);
        exchange.putAll(reqContext);
        setParameters(params, message);
        if (null != oi) {
            exchange.setOneWay(oi.getOutput() == null);
        }
        exchange.setOutMessage(message);
        exchange.put(ClientCallback.class, callback);
        setOutMessageProperties(message, oi);
        setExchangeProperties(exchange, endpoint, oi);
        PhaseInterceptorChain chain = setupInterceptorChain(endpoint);
        message.setInterceptorChain(chain);
        if (callback == null) {
            chain.setFaultObserver(outFaultObserver);
        } else {
            // We need to wrap the outFaultObserver if the callback is not null
            // calling the conduitSelector.complete to make sure the fail over feature works
            chain.setFaultObserver(new MessageObserver() {

                public void onMessage(Message message) {
                    Exception ex = message.getContent(Exception.class);
                    if (ex != null) {
                        completeExchange(message.getExchange());
                        if (message.getContent(Exception.class) == null) {
                            // handle the right response
                            List<Object> resList = null;
                            Message inMsg = message.getExchange().getInMessage();
                            Map<String, Object> ctx = responseContext.get(Thread.currentThread());
                            resList = CastUtils.cast(inMsg.getContent(List.class));
                            Object[] result = resList == null ? null : resList.toArray();
                            callback.handleResponse(ctx, result);
                            return;
                        }
                    }
                    outFaultObserver.onMessage(message);
                }
            });
        }
        prepareConduitSelector(message);
        // add additional interceptors and such
        modifyChain(chain, message, false);
        try {
            chain.doIntercept(message);
        } catch (Fault fault) {
            enrichFault(fault);
            throw fault;
        }
        if (callback != null) {
            return null;
        }
        return processResult(message, exchange, oi, resContext);
    } finally {
        if (origLoader != null) {
            origLoader.reset();
        }
        if (origBus != bus) {
            BusFactory.setThreadDefaultBus(origBus);
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) Fault(org.apache.cxf.interceptor.Fault) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) UncheckedException(org.apache.cxf.common.i18n.UncheckedException) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 5 with MessageObserver

use of org.apache.cxf.transport.MessageObserver in project cxf by apache.

the class ClientImpl method setExchangeProperties.

protected void setExchangeProperties(Exchange exchange, Endpoint endpoint, BindingOperationInfo boi) {
    if (endpoint != null) {
        exchange.put(Endpoint.class, endpoint);
        exchange.put(Service.class, endpoint.getService());
        exchange.put(Binding.class, endpoint.getBinding());
    }
    if (boi != null) {
        exchange.put(BindingOperationInfo.class, boi);
    }
    if (exchange.isSynchronous() || executor == null) {
        exchange.put(MessageObserver.class, this);
    } else {
        exchange.put(Executor.class, executor);
        exchange.put(MessageObserver.class, new MessageObserver() {

            public void onMessage(final Message message) {
                if (!message.getExchange().containsKey(Executor.class.getName() + ".USING_SPECIFIED")) {
                    executor.execute(new Runnable() {

                        public void run() {
                            ClientImpl.this.onMessage(message);
                        }
                    });
                } else {
                    ClientImpl.this.onMessage(message);
                }
            }
        });
    }
    exchange.put(Retryable.class, this);
    exchange.put(Client.class, this);
    exchange.put(Bus.class, bus);
    if (endpoint != null) {
        EndpointInfo endpointInfo = endpoint.getEndpointInfo();
        if (boi != null) {
            exchange.put(Message.WSDL_OPERATION, boi.getName());
        }
        QName serviceQName = endpointInfo.getService().getName();
        exchange.put(Message.WSDL_SERVICE, serviceQName);
        QName interfaceQName = endpointInfo.getService().getInterface().getName();
        exchange.put(Message.WSDL_INTERFACE, interfaceQName);
        QName portQName = endpointInfo.getName();
        exchange.put(Message.WSDL_PORT, portQName);
        URI wsdlDescription = endpointInfo.getProperty("URI", URI.class);
        if (wsdlDescription == null) {
            String address = endpointInfo.getAddress();
            try {
                wsdlDescription = new URI(address + "?wsdl");
            } catch (URISyntaxException e) {
            // do nothing
            }
            endpointInfo.setProperty("URI", wsdlDescription);
        }
        exchange.put(Message.WSDL_DESCRIPTION, wsdlDescription);
    }
}
Also used : EndpointInfo(org.apache.cxf.service.model.EndpointInfo) MessageObserver(org.apache.cxf.transport.MessageObserver) Message(org.apache.cxf.message.Message) QName(javax.xml.namespace.QName) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

MessageObserver (org.apache.cxf.transport.MessageObserver)43 Message (org.apache.cxf.message.Message)33 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)19 IOException (java.io.IOException)16 Conduit (org.apache.cxf.transport.Conduit)13 Exchange (org.apache.cxf.message.Exchange)11 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)10 EndpointReferenceType (org.apache.cxf.ws.addressing.EndpointReferenceType)10 MessageImpl (org.apache.cxf.message.MessageImpl)9 Test (org.junit.Test)9 Bus (org.apache.cxf.Bus)8 QName (javax.xml.namespace.QName)7 ConduitInitiator (org.apache.cxf.transport.ConduitInitiator)6 Fault (org.apache.cxf.interceptor.Fault)5 Destination (org.apache.cxf.transport.Destination)5 InputStream (java.io.InputStream)4 URISyntaxException (java.net.URISyntaxException)4 URL (java.net.URL)4 HashMap (java.util.HashMap)4 List (java.util.List)4