Package org.apache.servicemix.nmr.api

Examples of org.apache.servicemix.nmr.api.Exchange


        e.setMessage(Type.Fault, null);
    }

    @Test
    public void testError() {
        Exchange e = new ExchangeImpl(Pattern.InOnly);
        assertNull(e.getError());
        assertEquals(Status.Active, e.getStatus());
        e.setError(new Exception());
        assertNotNull(e.getError());
        assertEquals(Status.Error, e.getStatus());
    }
View Full Code Here


        assertEquals(Status.Error, e.getStatus());
    }

    @Test
    public void testProperties() {
        Exchange e = new ExchangeImpl(Pattern.InOnly);
        assertNotNull(e.getProperties());
        e.setProperty("name", "value");
        assertEquals("value", e.getProperty("name"));
        assertNotNull(e.getProperty("name", String.class));
        assertNotNull(e.getProperty("name", byte[].class));
        assertNotNull(e.removeProperty("name"));
        assertNull(e.getProperty("name"));
        e.setProperty(Exchange.class, new ExchangeImpl(Pattern.InOnly));
        assertNotNull(e.getProperty(Exchange.class.getName(), Exchange.class));
        assertNotNull(e.getProperty(Exchange.class));
        assertNotNull(e.removeProperty(Exchange.class));
        assertNull(e.getProperty(Exchange.class));
        assertTrue(e.getProperties().isEmpty());
        e.setProperties(createMap("key", "val"));
        assertNotNull(e.getProperties());
        assertFalse(e.getProperties().isEmpty());
        e.setProperties(null);
        assertNull(e.getProperty("name"));
        assertNull(e.getProperty(Exchange.class));
        assertNull(e.getProperty("name", byte[].class));
        assertNull(e.removeProperty("name"));
        e.setProperties(null);
        e.setProperty(Exchange.class, new ExchangeImpl(Pattern.InOnly));
        assertNotNull(e.getProperty(Exchange.class));
        e.setProperties(null);
        e.setProperty("name", "value");
        assertNotNull(e.getProperty("name"));
    }
View Full Code Here

        }

        public void process(Exchange exchange) {
            @SuppressWarnings("unchecked")
            Holder<Exchange> holder = (Holder<Exchange>) exchange.getProperty("correlated");
            Exchange correlated = holder != null ? holder.get() : null;
            if (exchange.getStatus() == Status.Error) {
                correlated.setError(exchange.getError());
                correlated.setStatus(Status.Error);
            } else if (exchange.getStatus() == Status.Done) {
                correlated.setStatus(Status.Done);
            } else if (exchange.getFault(false) != null) {
                correlated.setFault(exchange.getFault());
            } else if (exchange.getOut(false) != null) {
                correlated.setOut(exchange.getOut());
            } else if (exchange.getIn(false) != null) {
                correlated = channel.createExchange(exchange.getPattern());
                exchange.setProperty("correlated", new Holder<Exchange>(correlated));
                correlated.setProperty("correlated", new Holder<Exchange>(exchange));

                //correlated.setProperty(JbiConstants.SENDER_ENDPOINT, getService() + ":" + getEndpoint());
                correlated.setProperty(MessageExchangeImpl.SERVICE_NAME_PROP, service);
                DeliveryChannelImpl.createTarget(channel.getNMR(), correlated);
                correlated.setIn(exchange.getIn());
            } else {
                throw new IllegalStateException();
            }
            channel.send(correlated);
        }
View Full Code Here

        return factory;
    }

    public MessageExchange accept() throws MessagingException {
        try {
            Exchange exchange = queue.take();
            if (exchange == null) {
                return null;
            }
            MessageExchange me = getMessageExchange(exchange);
            ((MessageExchangeImpl) me).beforeReceived();
View Full Code Here

    public MessageExchange accept(long timeout) throws MessagingException {
        try {
            long t0  = System.currentTimeMillis();
            long cur = t0;
            while (cur - t0 < timeout) {
                Exchange exchange = queue.poll(t0 + timeout - cur, TimeUnit.MILLISECONDS);
                if (exchange == null || exchange.getError() instanceof AbortedException) {
                    cur = System.currentTimeMillis();
                    continue;
                }
                MessageExchange me = getMessageExchange(exchange);
                ((MessageExchangeImpl) me).beforeReceived();
View Full Code Here

        ((MessageExchangeImpl) exchange).afterSend();
        return channel.sendSync(((MessageExchangeImpl) exchange).getInternalExchange(), timeout);
    }

    protected void createTarget(MessageExchange messageExchange) throws MessagingException {
        Exchange exchange = ((MessageExchangeImpl) messageExchange).getInternalExchange();
        if (exchange.getTarget() == null) {
            Map<String, Object> props = new HashMap<String, Object>();
            if (messageExchange.getEndpoint() != null) {
                props.put(Endpoint.SERVICE_NAME, messageExchange.getEndpoint().getServiceName());
                props.put(Endpoint.ENDPOINT_NAME, messageExchange.getEndpoint().getEndpointName());
            } else {
                QName serviceName = messageExchange.getService();
                if (serviceName != null) {
                    props.put(Endpoint.SERVICE_NAME, serviceName);
                } else {
                    QName interfaceName = messageExchange.getInterfaceName();
                    if (interfaceName != null) {
                        props.put(Endpoint.INTERFACE_NAME, interfaceName);
                    }
                }
            }
            if (props.isEmpty()) {
                throw new MessagingException("No endpoint, service or interface name specified for routing");
            }
            Reference target = context.getNmr().getEndpointRegistry().lookup(props);
            exchange.setTarget(target);
        }
    }
View Full Code Here

public class ExchangeUtilsTest extends TestCase {

    private static final Log LOG = LogFactory.getLog(ExchangeUtilsTest.class);

    public void testReReadable() throws Exception {
        Exchange e = new ExchangeImpl(Pattern.InOnly);
        Message msg = e.getIn();
        msg.addAttachment("id", new BufferedInputStream(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 })));
        msg.setBody(new StringSource("<hello/>"));

        e.ensureReReadable();

        assertNotNull(msg.getBody());
        assertTrue(msg.getBody() instanceof DOMSource);
        assertNotNull(msg.getAttachment("id"));
        assertTrue(msg.getAttachment("id") instanceof ByteArrayInputStream);
View Full Code Here

        assertNotNull(msg.getAttachment("id"));
        assertTrue(msg.getAttachment("id") instanceof ByteArrayInputStream);
    }

    public void testDisplay() throws Exception {
        Exchange e = new ExchangeImpl(Pattern.InOnly);
        e.setOperation(new QName("op"));
        e.setProperty("key", "value");
        e.setStatus(Status.Done);
        Message msg = e.getIn();
        msg.setHeader("header", "value");
        msg.addAttachment("id", new BufferedInputStream(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 })));
        msg.setBody(new StringSource("<hello/>"));

        String str = e.display(false);
        LOG.info(str);
        assertNotNull(msg.getBody());
        assertTrue(msg.getBody() instanceof StringSource);
        assertNotNull(msg.getAttachment("id"));
        assertTrue(msg.getAttachment("id") instanceof BufferedInputStream);

        str = e.display(true);
        LOG.info(str);
        assertNotNull(msg.getBody());
        assertTrue(msg.getBody() instanceof DOMSource);
        assertNotNull(msg.getAttachment("id"));
        assertTrue(msg.getAttachment("id") instanceof ByteArrayInputStream);
View Full Code Here

        BindingMessageInfo bmi = control.createMock(BindingMessageInfo.class);
        EasyMock.expect(boi.getOutput()).andReturn(bmi);
        exchange.put(BindingOperationInfo.class, boi);
        Channel channel = control.createMock(Channel.class);
        EasyMock.expect(nmr.createChannel()).andReturn(channel);
        Exchange xchg = control.createMock(Exchange.class);
        EasyMock.expect(channel.createExchange(Pattern.InOut)).andReturn(xchg);
        org.apache.servicemix.nmr.api.Message inMsg = control.createMock(org.apache.servicemix.nmr.api.Message.class);
        EasyMock.expect(xchg.getIn()).andReturn(inMsg);
        EndpointRegistry endpoints = control.createMock(EndpointRegistry.class);
        EasyMock.expect(channel.getNMR()).andReturn(nmr);
        EasyMock.expect(nmr.getEndpointRegistry()).andReturn(endpoints);
        org.apache.servicemix.nmr.api.Message outMsg = control.createMock(org.apache.servicemix.nmr.api.Message.class);
        EasyMock.expect(xchg.getOut()).andReturn(outMsg);
       
        Source source = new StreamSource(new ByteArrayInputStream(
                            "<message>TestHelloWorld</message>".getBytes()));
        EasyMock.expect(outMsg.getBody(Source.class)).andReturn(source);
        EasyMock.expect(xchg.getOut()).andReturn(outMsg);
        EasyMock.expect(outMsg.getAttachments()).andReturn(new HashMap<String, Object>());
        EasyMock.expect(outMsg.getHeaders()).andReturn(new HashMap<String, Object>());
        control.replay();
        try {
            conduit.prepare(message);
View Full Code Here

        Thread.sleep(5000);
        NMR nmr = getOsgiService(NMR.class);
        assertNotNull(nmr);
       
        Channel client = nmr.createChannel();
        Exchange e = client.createExchange(Pattern.InOut);
        for (Endpoint ep : nmr.getEndpointRegistry().getServices()) {
            e.setTarget(nmr.getEndpointRegistry().lookup(nmr.getEndpointRegistry().getProperties(ep)));
            e.getIn().setBody(new StringSource("<?xml version=\"1.0\" encoding=\"UTF-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><ns2:sayHi xmlns:ns2=\"http://cxf.examples.servicemix.apache.org/\"><arg0>Bonjour</arg0></ns2:sayHi></soap:Body></soap:Envelope>"));
            boolean res = client.sendSync(e);
            assertTrue(res);
        }
   
    }
View Full Code Here

TOP

Related Classes of org.apache.servicemix.nmr.api.Exchange

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.