Search in sources :

Example 51 with Exchange

use of org.apache.camel.Exchange in project camel by apache.

the class Mina2ProducerShutdownMockTest method testProducerShutdownTestingWithMock.

@Test
public void testProducerShutdownTestingWithMock() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Hello World");
    // create our mock and record expected behavior = that worker timeout should be set to 0
    SocketConnector mockConnector = createMock(SocketConnector.class);
    mockConnector.dispose(true);
    replay(mockConnector);
    // normal camel code to get a producer
    Endpoint endpoint = context.getEndpoint(String.format("mina2:tcp://localhost:%1$s?textline=true&sync=false", getPort()));
    Exchange exchange = endpoint.createExchange();
    Producer producer = endpoint.createProducer();
    producer.start();
    // set input and execute it
    exchange.getIn().setBody("Hello World");
    producer.process(exchange);
    // insert our mock instead of real MINA IoConnector
    Field field = producer.getClass().getDeclaredField("connector");
    field.setAccessible(true);
    field.set(producer, mockConnector);
    //
    // Everything is asynchronous.
    // We need to wait a second to make sure we get the message.
    //
    Thread.sleep(1000);
    // stop using our mock
    producer.stop();
    verify(mockConnector);
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Field(java.lang.reflect.Field) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Producer(org.apache.camel.Producer) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SocketConnector(org.apache.mina.transport.socket.SocketConnector) Test(org.junit.Test)

Example 52 with Exchange

use of org.apache.camel.Exchange in project camel by apache.

the class Mina2ProducerShutdownTest method sendMessage.

private void sendMessage() throws Exception {
    Endpoint endpoint = context.getEndpoint(URI);
    Producer producer = endpoint.createProducer();
    Exchange exchange = endpoint.createExchange();
    exchange.getIn().setBody("Hello World");
    producer.start();
    producer.process(exchange);
    producer.stop();
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer)

Example 53 with Exchange

use of org.apache.camel.Exchange in project camel by apache.

the class Mina2TcpWithInOutTest method testMinaRouteWithInOut.

@Test
public void testMinaRouteWithInOut() throws Exception {
    latch = new CountDownLatch(1);
    uri = String.format("mina2:tcp://localhost:%1$s?textline=true", getPort());
    Mina2ReverserServer server = new Mina2ReverserServer(getPort());
    server.start();
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:x").to(uri).process(new Processor() {

                public void process(Exchange e) {
                    receivedExchange = e;
                    latch.countDown();
                }
            });
        }
    });
    context.start();
    // now lets fire in a message
    Endpoint endpoint = context.getEndpoint("direct:x");
    Exchange exchange = endpoint.createExchange(ExchangePattern.InOut);
    Message message = exchange.getIn();
    message.setBody("Hello!");
    message.setHeader("cheese", 123);
    Producer producer = endpoint.createProducer();
    producer.start();
    producer.process(exchange);
    // now lets sleep for a while
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    assertNotNull(receivedExchange.getIn());
    assertEquals("!olleH", receivedExchange.getIn().getBody());
    producer.stop();
    context.stop();
    server.stop();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Endpoint(org.apache.camel.Endpoint) Message(org.apache.camel.Message) Producer(org.apache.camel.Producer) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 54 with Exchange

use of org.apache.camel.Exchange in project camel by apache.

the class Mina2TransferExchangeOptionTest method sendExchange.

private Exchange sendExchange(boolean setException) throws Exception {
    Endpoint endpoint = context.getEndpoint(String.format("mina2:tcp://localhost:%1$s?sync=true&encoding=UTF-8&transferExchange=true", getPort()));
    Producer producer = endpoint.createProducer();
    Exchange exchange = endpoint.createExchange();
    //Exchange exchange = endpoint.createExchange();
    Message message = exchange.getIn();
    message.setBody("Hello!");
    message.setHeader("cheese", "feta");
    exchange.setProperty("ham", "old");
    exchange.setProperty("setException", setException);
    producer.start();
    producer.process(exchange);
    return exchange;
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) Message(org.apache.camel.Message)

Example 55 with Exchange

use of org.apache.camel.Exchange in project camel by apache.

the class Mina2TransferExchangeOptionTest method testMinaTransferExchangeOptionWithException.

@Test
public void testMinaTransferExchangeOptionWithException() throws Exception {
    Exchange exchange = sendExchange(true);
    assertExchange(exchange, true);
}
Also used : Exchange(org.apache.camel.Exchange) Test(org.junit.Test)

Aggregations

Exchange (org.apache.camel.Exchange)3446 Test (org.junit.Test)1735 Processor (org.apache.camel.Processor)1405 RouteBuilder (org.apache.camel.builder.RouteBuilder)666 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)640 DefaultExchange (org.apache.camel.impl.DefaultExchange)473 Message (org.apache.camel.Message)379 Endpoint (org.apache.camel.Endpoint)235 HashMap (java.util.HashMap)190 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)155 Producer (org.apache.camel.Producer)150 File (java.io.File)120 ArrayList (java.util.ArrayList)117 CamelContext (org.apache.camel.CamelContext)117 List (java.util.List)99 Map (java.util.Map)96 ProducerTemplate (org.apache.camel.ProducerTemplate)94 IOException (java.io.IOException)92 Tx (org.nhindirect.common.tx.model.Tx)83 Predicate (org.apache.camel.Predicate)78