Search in sources :

Example 96 with Producer

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

the class NettyTransferExchangeOptionTest method sendExchange.

private Exchange sendExchange(boolean setException) throws Exception {
    Endpoint endpoint = context.getEndpoint("netty:tcp://localhost:{{port}}?transferExchange=true");
    Exchange exchange = endpoint.createExchange();
    Message message = exchange.getIn();
    message.setBody("Hello!");
    message.setHeader("cheese", "feta");
    exchange.setProperty("ham", "old");
    exchange.setProperty("setException", setException);
    Producer producer = endpoint.createProducer();
    producer.start();
    // ensure to stop producer after usage
    try {
        producer.process(exchange);
    } finally {
        producer.stop();
    }
    return exchange;
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Message(org.apache.camel.Message) Producer(org.apache.camel.Producer)

Example 97 with Producer

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

the class NettyTransferExchangeOptionTest method sendExchange.

private Exchange sendExchange(boolean setException) throws Exception {
    Endpoint endpoint = context.getEndpoint("netty4:tcp://localhost:{{port}}?transferExchange=true");
    Exchange exchange = endpoint.createExchange();
    Message message = exchange.getIn();
    message.setBody("Hello!");
    message.setHeader("cheese", "feta");
    exchange.setProperty("ham", "old");
    exchange.setProperty("setException", setException);
    Producer producer = endpoint.createProducer();
    producer.start();
    // ensure to stop producer after usage
    try {
        producer.process(exchange);
    } finally {
        producer.stop();
    }
    return exchange;
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Message(org.apache.camel.Message) Producer(org.apache.camel.Producer)

Example 98 with Producer

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

the class RestSwaggerEndpointTest method shouldCreateProducers.

@Test
public void shouldCreateProducers() throws Exception {
    final CamelContext camelContext = mock(CamelContext.class);
    when(camelContext.getClassResolver()).thenReturn(new DefaultClassResolver());
    final Endpoint endpointDelegate = mock(Endpoint.class);
    when(camelContext.getEndpoint("rest:GET:/v2:/pet/{petId}")).thenReturn(endpointDelegate);
    final Producer delegateProducer = mock(Producer.class);
    when(endpointDelegate.createProducer()).thenReturn(delegateProducer);
    final RestSwaggerComponent component = new RestSwaggerComponent(camelContext);
    component.setHost("http://petstore.swagger.io");
    final RestSwaggerEndpoint endpoint = new RestSwaggerEndpoint("rest-swagger:getPetById", "getPetById", component);
    final Producer producer = endpoint.createProducer();
    assertThat(producer).isSameAs(delegateProducer);
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultClassResolver(org.apache.camel.impl.DefaultClassResolver) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) Test(org.junit.Test)

Example 99 with Producer

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

the class RestSwaggerEndpoint method createProducer.

@Override
public Producer createProducer() throws Exception {
    final CamelContext camelContext = getCamelContext();
    final Swagger swagger = loadSpecificationFrom(camelContext, specificationUri);
    final Map<String, Path> paths = swagger.getPaths();
    for (final Entry<String, Path> pathEntry : paths.entrySet()) {
        final Path path = pathEntry.getValue();
        final Optional<Entry<HttpMethod, Operation>> maybeOperationEntry = path.getOperationMap().entrySet().stream().filter(operationEntry -> operationId.equals(operationEntry.getValue().getOperationId())).findAny();
        if (maybeOperationEntry.isPresent()) {
            final Entry<HttpMethod, Operation> operationEntry = maybeOperationEntry.get();
            final String uriTemplate = pathEntry.getKey();
            final HttpMethod httpMethod = operationEntry.getKey();
            final String method = httpMethod.name();
            final Operation operation = operationEntry.getValue();
            return createProducerFor(swagger, operation, method, uriTemplate);
        }
    }
    final String supportedOperations = paths.values().stream().flatMap(p -> p.getOperations().stream()).map(Operation::getOperationId).collect(Collectors.joining(", "));
    throw new IllegalArgumentException("The specified operation with ID: `" + operationId + "` cannot be found in the Swagger specification loaded from `" + specificationUri + "`. Operations defined in the specification are: " + supportedOperations);
}
Also used : CamelContext(org.apache.camel.CamelContext) UriPath(org.apache.camel.spi.UriPath) Path(io.swagger.models.Path) Scheme(io.swagger.models.Scheme) SwaggerParser(io.swagger.parser.SwaggerParser) Metadata(org.apache.camel.spi.Metadata) UriPath(org.apache.camel.spi.UriPath) URISyntaxException(java.net.URISyntaxException) Swagger(io.swagger.models.Swagger) Json(io.swagger.util.Json) StringHelper.before(org.apache.camel.util.StringHelper.before) HashMap(java.util.HashMap) Endpoint(org.apache.camel.Endpoint) Consumer(org.apache.camel.Consumer) ObjectHelper.notNull(org.apache.camel.util.ObjectHelper.notNull) StringHelper.notEmpty(org.apache.camel.util.StringHelper.notEmpty) UriEndpoint(org.apache.camel.spi.UriEndpoint) Processor(org.apache.camel.Processor) UriParam(org.apache.camel.spi.UriParam) ObjectHelper.isNotEmpty(org.apache.camel.util.ObjectHelper.isNotEmpty) Path(io.swagger.models.Path) RestSwaggerHelper.isHostParam(org.apache.camel.component.rest.swagger.RestSwaggerHelper.isHostParam) Map(java.util.Map) ResourceHelper(org.apache.camel.util.ResourceHelper) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) Operation(io.swagger.models.Operation) RestSwaggerHelper.isMediaRange(org.apache.camel.component.rest.swagger.RestSwaggerHelper.isMediaRange) ExchangePattern(org.apache.camel.ExchangePattern) CamelContext(org.apache.camel.CamelContext) StringHelper.after(org.apache.camel.util.StringHelper.after) DefaultEndpoint(org.apache.camel.impl.DefaultEndpoint) Optional.ofNullable(java.util.Optional.ofNullable) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Producer(org.apache.camel.Producer) List(java.util.List) Entry(java.util.Map.Entry) Optional(java.util.Optional) HttpMethod(io.swagger.models.HttpMethod) InputStream(java.io.InputStream) RestConfiguration(org.apache.camel.spi.RestConfiguration) Operation(io.swagger.models.Operation) Entry(java.util.Map.Entry) Swagger(io.swagger.models.Swagger) HttpMethod(io.swagger.models.HttpMethod)

Example 100 with Producer

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

the class TrapTest method testSendReceiveTraps.

@Test
public void testSendReceiveTraps() throws Exception {
    // Create a trap PDU
    PDU trap = new PDU();
    trap.setType(PDU.TRAP);
    OID oid = new OID("1.2.3.4.5");
    trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
    // put your uptime here
    trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
    trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));
    //Add Payload
    Variable var = new OctetString("some string");
    trap.add(new VariableBinding(oid, var));
    // Send it
    LOG.info("Sending pdu " + trap);
    Endpoint endpoint = context.getEndpoint("direct:snmptrap");
    Exchange exchange = endpoint.createExchange();
    exchange.getIn().setBody(trap);
    Producer producer = endpoint.createProducer();
    producer.process(exchange);
    synchronized (this) {
        Thread.sleep(1000);
    }
    // If all goes right it should come here
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    mock.assertIsSatisfied();
    List<Exchange> exchanges = mock.getExchanges();
    SnmpMessage msg = (SnmpMessage) exchanges.get(0).getIn();
    PDU receivedTrap = msg.getSnmpMessage();
    Assert.assertEquals(trap, receivedTrap);
    if (LOG.isInfoEnabled()) {
        LOG.info("Received SNMP TRAP:");
        Vector<? extends VariableBinding> variableBindings = receivedTrap.getVariableBindings();
        for (VariableBinding vb : variableBindings) {
            LOG.info("  " + vb.toString());
        }
    }
}
Also used : PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) Variable(org.snmp4j.smi.Variable) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) TimeTicks(org.snmp4j.smi.TimeTicks) OID(org.snmp4j.smi.OID) Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Producer(org.apache.camel.Producer) VariableBinding(org.snmp4j.smi.VariableBinding) Test(org.junit.Test)

Aggregations

Producer (org.apache.camel.Producer)197 Endpoint (org.apache.camel.Endpoint)139 Exchange (org.apache.camel.Exchange)138 Test (org.junit.Test)72 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)69 Processor (org.apache.camel.Processor)34 RouteBuilder (org.apache.camel.builder.RouteBuilder)23 Message (org.apache.camel.Message)21 CountDownLatch (java.util.concurrent.CountDownLatch)16 File (java.io.File)12 CamelContext (org.apache.camel.CamelContext)12 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)10 DefaultExchange (org.apache.camel.impl.DefaultExchange)9 Mockito.anyLong (org.mockito.Mockito.anyLong)9 Consumer (org.apache.camel.Consumer)8 FileDataSource (javax.activation.FileDataSource)7 AsyncProcessor (org.apache.camel.AsyncProcessor)7 DataHandler (javax.activation.DataHandler)6 Field (java.lang.reflect.Field)5 ExchangePattern (org.apache.camel.ExchangePattern)5