Java Code Examples for org.apache.camel.ConsumerTemplate#receive()

The following examples show how to use org.apache.camel.ConsumerTemplate#receive() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CamelSinkTaskTest.java    From camel-kafka-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnlyBody() {
    Map<String, String> props = new HashMap<>();
    props.put(CamelSinkConnectorConfig.TOPIC_CONF, TOPIC_NAME);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_URL_CONF, SEDA_URI);

    CamelSinkTask sinkTask = new CamelSinkTask();
    sinkTask.start(props);

    List<SinkRecord> records = new ArrayList<SinkRecord>();
    SinkRecord record = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel", 42);
    records.add(record);
    sinkTask.put(records);

    ConsumerTemplate consumer = sinkTask.getCms().createConsumerTemplate();
    Exchange exchange = consumer.receive(SEDA_URI, RECEIVE_TIMEOUT);
    assertEquals("camel", exchange.getMessage().getBody());
    assertEquals("test", exchange.getMessage().getHeaders().get(CamelSinkTask.KAFKA_RECORD_KEY_HEADER));
    assertEquals(LoggingLevel.OFF.toString(), sinkTask.getCamelSinkConnectorConfig(props)
        .getString(CamelSinkConnectorConfig.CAMEL_SINK_CONTENT_LOG_LEVEL_CONF));

    sinkTask.stop();
}
 
Example 2
Source File: CamelSinkTaskTest.java    From camel-kafka-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testUrlPrecedenceOnComponentProperty() {
    Map<String, String> props = new HashMap<>();
    props.put(CamelSinkConnectorConfig.TOPIC_CONF, TOPIC_NAME);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_URL_CONF, SEDA_URI);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_COMPONENT_CONF, "shouldNotBeUsed");
    props.put(CamelSinkTask.getCamelSinkEndpointConfigPrefix() + "endpointProperty", "shouldNotBeUsed");
    props.put(CamelSinkTask.getCamelSinkPathConfigPrefix() + "pathChunk", "shouldNotBeUsed");

    CamelSinkTask sinkTask = new CamelSinkTask();
    sinkTask.start(props);

    List<SinkRecord> records = new ArrayList<SinkRecord>();
    SinkRecord record = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel", 42);
    records.add(record);
    sinkTask.put(records);

    ConsumerTemplate consumer = sinkTask.getCms().createConsumerTemplate();
    Exchange exchange = consumer.receive(SEDA_URI, RECEIVE_TIMEOUT);
    assertEquals("camel", exchange.getMessage().getBody());
    assertEquals("test", exchange.getMessage().getHeaders().get(CamelSinkTask.KAFKA_RECORD_KEY_HEADER));

    sinkTask.stop();
}
 
Example 3
Source File: CamelSinkTaskTest.java    From camel-kafka-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnlyBodyUsingComponentProperty() {
    Map<String, String> props = new HashMap<>();
    props.put(CamelSinkConnectorConfig.TOPIC_CONF, TOPIC_NAME);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_COMPONENT_CONF, "seda");
    props.put(CamelSinkTask.getCamelSinkEndpointConfigPrefix() + "bridgeErrorHandler", "true");
    props.put(CamelSinkTask.getCamelSinkPathConfigPrefix() + "name", "test");

    CamelSinkTask sinkTask = new CamelSinkTask();
    sinkTask.start(props);

    List<SinkRecord> records = new ArrayList<SinkRecord>();
    SinkRecord record = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel", 42);
    records.add(record);
    sinkTask.put(records);

    ConsumerTemplate consumer = sinkTask.getCms().createConsumerTemplate();
    Exchange exchange = consumer.receive(SEDA_URI, RECEIVE_TIMEOUT);
    assertEquals("camel", exchange.getMessage().getBody());
    assertEquals("test", exchange.getMessage().getHeaders().get(CamelSinkTask.KAFKA_RECORD_KEY_HEADER));
    assertEquals(1, sinkTask.getCms().getEndpoints()
        .stream().filter(e -> e.getEndpointUri().equals("seda://test?bridgeErrorHandler=true")).count());

    sinkTask.stop();
}
 
Example 4
Source File: CamelSinkTaskTest.java    From camel-kafka-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testOnlyBodyUsingMultipleComponentProperties() {
    Map<String, String> props = new HashMap<>();
    props.put(CamelSinkConnectorConfig.TOPIC_CONF, TOPIC_NAME);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_COMPONENT_CONF, "seda");
    props.put(CamelSinkTask.getCamelSinkEndpointConfigPrefix() + "bridgeErrorHandler", "true");
    props.put(CamelSinkTask.getCamelSinkEndpointConfigPrefix() + "size", "50");
    props.put(CamelSinkTask.getCamelSinkPathConfigPrefix() + "name", "test");

    CamelSinkTask sinkTask = new CamelSinkTask();
    sinkTask.start(props);

    List<SinkRecord> records = new ArrayList<SinkRecord>();
    SinkRecord record = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel", 42);
    records.add(record);
    sinkTask.put(records);

    ConsumerTemplate consumer = sinkTask.getCms().createConsumerTemplate();
    Exchange exchange = consumer.receive(SEDA_URI, RECEIVE_TIMEOUT);
    assertEquals("camel", exchange.getMessage().getBody());
    assertEquals("test", exchange.getMessage().getHeaders().get(CamelSinkTask.KAFKA_RECORD_KEY_HEADER));

    assertEquals(1, sinkTask.getCms().getEndpoints()
        .stream().filter(e -> e.getEndpointUri().equals("seda://test?bridgeErrorHandler=true&size=50")).count());

    sinkTask.stop();
}
 
Example 5
Source File: CamelSinkTaskTest.java    From camel-kafka-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testAggregationBody() {
    Map<String, String> props = new HashMap<>();
    props.put(CamelSinkConnectorConfig.TOPIC_CONF, TOPIC_NAME);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_URL_CONF, SEDA_URI);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_AGGREGATE_CONF, "#class:org.apache.camel.kafkaconnector.utils.SampleAggregator");
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_AGGREGATE_SIZE_CONF, "5");
    CamelSinkTask sinkTask = new CamelSinkTask();
    sinkTask.start(props);

    List<SinkRecord> records = new ArrayList<SinkRecord>();
    SinkRecord record = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel", 42);
    SinkRecord record1 = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel1", 42);
    SinkRecord record2 = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel2", 42);
    SinkRecord record3 = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel3", 42);
    SinkRecord record4 = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel4", 42);
    SinkRecord record5 = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel5", 42);
    records.add(record);
    records.add(record1);
    records.add(record2);
    records.add(record3);
    records.add(record4);
    records.add(record5);
    sinkTask.put(records);

    ConsumerTemplate consumer = sinkTask.getCms().createConsumerTemplate();
    Exchange exchange = consumer.receive(SEDA_URI, RECEIVE_TIMEOUT);
    assertEquals("camel camel1 camel2 camel3 camel4", exchange.getMessage().getBody());
    assertEquals("test", exchange.getMessage().getHeaders().get(CamelSinkTask.KAFKA_RECORD_KEY_HEADER));
    assertEquals(LoggingLevel.OFF.toString(), sinkTask.getCamelSinkConnectorConfig(props)
        .getString(CamelSinkConnectorConfig.CAMEL_SINK_CONTENT_LOG_LEVEL_CONF));

    sinkTask.stop();
}
 
Example 6
Source File: CamelSinkTaskTest.java    From camel-kafka-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testAggregationBodyAndTimeout() throws InterruptedException {
    Map<String, String> props = new HashMap<>();
    props.put(CamelSinkConnectorConfig.TOPIC_CONF, TOPIC_NAME);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_URL_CONF, SEDA_URI);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_AGGREGATE_CONF, "#class:org.apache.camel.kafkaconnector.utils.SampleAggregator");
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_AGGREGATE_SIZE_CONF, "5");
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_AGGREGATE_TIMEOUT_CONF, "100");
    CamelSinkTask sinkTask = new CamelSinkTask();
    sinkTask.start(props);

    List<SinkRecord> records = new ArrayList<SinkRecord>();
    SinkRecord record = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel", 42);
    SinkRecord record1 = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel1", 42);
    SinkRecord record2 = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel2", 42);
    SinkRecord record3 = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel3", 42);
    SinkRecord record4 = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel4", 42);
    SinkRecord record5 = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel5", 42);
    records.add(record);
    records.add(record1);
    records.add(record2);
    records.add(record3);
    records.add(record4);
    records.add(record5);
    sinkTask.put(records);

    ConsumerTemplate consumer = sinkTask.getCms().createConsumerTemplate();
    Exchange exchange = consumer.receive(SEDA_URI, RECEIVE_TIMEOUT);
    assertEquals("camel camel1 camel2 camel3 camel4", exchange.getMessage().getBody());
    assertEquals("test", exchange.getMessage().getHeaders().get(CamelSinkTask.KAFKA_RECORD_KEY_HEADER));
    assertEquals(LoggingLevel.OFF.toString(), sinkTask.getCamelSinkConnectorConfig(props)
        .getString(CamelSinkConnectorConfig.CAMEL_SINK_CONTENT_LOG_LEVEL_CONF));

    sinkTask.stop();
}
 
Example 7
Source File: CamelStubMessages.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Override
public Message receive(String destination, long timeout, TimeUnit timeUnit) {
	try {
		ConsumerTemplate consumerTemplate = this.context.createConsumerTemplate();
		Exchange exchange = consumerTemplate.receive(destination,
				timeUnit.toMillis(timeout));
		return exchange != null ? exchange.getIn() : null;
	}
	catch (Exception e) {
		log.error("Exception occurred while trying to read a message from "
				+ " a channel with name [" + destination + "]", e);
		throw new IllegalStateException(e);
	}
}
 
Example 8
Source File: CamelSinkTaskTest.java    From camel-kafka-connector with Apache License 2.0 4 votes vote down vote up
@Test
public void testBodyAndHeaders() {
    Map<String, String> props = new HashMap<>();
    props.put(CamelSinkConnectorConfig.TOPIC_CONF, TOPIC_NAME);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_URL_CONF, SEDA_URI);

    CamelSinkTask sinkTask = new CamelSinkTask();
    sinkTask.start(props);

    Byte myByte = new Byte("100");
    Float myFloat = new Float("100");
    Short myShort = new Short("100");
    Double myDouble = new Double("100");
    int myInteger = 100;
    Long myLong = new Long("100");

    List<SinkRecord> records = new ArrayList<SinkRecord>();
    SinkRecord record = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel", 42);
    record.headers().addBoolean("CamelHeaderMyBoolean", true);
    record.headers().addByte("CamelHeaderMyByte", myByte);
    record.headers().addFloat("CamelHeaderMyFloat", myFloat);
    record.headers().addShort("CamelHeaderMyShort", myShort);
    record.headers().addDouble("CamelHeaderMyDouble", myDouble);
    record.headers().addInt("CamelHeaderMyInteger", myInteger);
    record.headers().addLong("CamelHeaderMyLong", myLong);
    records.add(record);
    sinkTask.put(records);

    ConsumerTemplate consumer = sinkTask.getCms().createConsumerTemplate();
    Exchange exchange = consumer.receive(SEDA_URI, RECEIVE_TIMEOUT);
    assertEquals("camel", exchange.getMessage().getBody());
    assertEquals("test", exchange.getMessage().getHeaders().get(CamelSinkTask.KAFKA_RECORD_KEY_HEADER));
    assertTrue(exchange.getIn().getHeader("MyBoolean", Boolean.class));
    assertEquals(myByte, exchange.getIn().getHeader("MyByte", Byte.class));
    assertEquals(myFloat, exchange.getIn().getHeader("MyFloat", Float.class));
    assertEquals(myShort, exchange.getIn().getHeader("MyShort", Short.class));
    assertEquals(myDouble, exchange.getIn().getHeader("MyDouble", Double.class));
    assertEquals(myInteger, exchange.getIn().getHeader("MyInteger"));
    assertEquals(myLong, exchange.getIn().getHeader("MyLong", Long.class));

    sinkTask.stop();
}
 
Example 9
Source File: CamelSinkTaskTest.java    From camel-kafka-connector with Apache License 2.0 4 votes vote down vote up
@Test
public void testBodyAndProperties() {
    Map<String, String> props = new HashMap<>();
    props.put(CamelSinkConnectorConfig.TOPIC_CONF, TOPIC_NAME);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_URL_CONF, SEDA_URI);

    CamelSinkTask sinkTask = new CamelSinkTask();
    sinkTask.start(props);

    Byte myByte = new Byte("100");
    Float myFloat = new Float("100");
    Short myShort = new Short("100");
    Double myDouble = new Double("100");
    int myInteger = 100;
    Long myLong = new Long("100");

    List<SinkRecord> records = new ArrayList<SinkRecord>();
    SinkRecord record = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel", 42);
    record.headers().addBoolean("CamelPropertyMyBoolean", true);
    record.headers().addByte("CamelPropertyMyByte", myByte);
    record.headers().addFloat("CamelPropertyMyFloat", myFloat);
    record.headers().addShort("CamelPropertyMyShort", myShort);
    record.headers().addDouble("CamelPropertyMyDouble", myDouble);
    record.headers().addInt("CamelPropertyMyInteger", myInteger);
    record.headers().addLong("CamelPropertyMyLong", myLong);
    records.add(record);
    sinkTask.put(records);

    ConsumerTemplate consumer = sinkTask.getCms().createConsumerTemplate();
    Exchange exchange = consumer.receive(SEDA_URI, RECEIVE_TIMEOUT);
    assertEquals("camel", exchange.getMessage().getBody());
    assertEquals("test", exchange.getMessage().getHeaders().get(CamelSinkTask.KAFKA_RECORD_KEY_HEADER));
    assertTrue((boolean) exchange.getProperties().get("MyBoolean"));
    assertEquals(myByte, (Byte) exchange.getProperties().get("MyByte"));
    assertEquals(myFloat, (Float) exchange.getProperties().get("MyFloat"));
    assertEquals(myShort, (Short) exchange.getProperties().get("MyShort"));
    assertEquals(myDouble, (Double) exchange.getProperties().get("MyDouble"));
    assertEquals(myInteger, exchange.getProperties().get("MyInteger"));
    assertEquals(myLong, (Long) exchange.getProperties().get("MyLong"));

    sinkTask.stop();
}
 
Example 10
Source File: CamelSinkTaskTest.java    From camel-kafka-connector with Apache License 2.0 4 votes vote down vote up
@Test
public void testBodyAndPropertiesHeadersMixed() {
    Map<String, String> props = new HashMap<>();
    props.put(CamelSinkConnectorConfig.TOPIC_CONF, TOPIC_NAME);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_URL_CONF, SEDA_URI);

    CamelSinkTask sinkTask = new CamelSinkTask();
    sinkTask.start(props);

    Byte myByte = new Byte("100");
    Float myFloat = new Float("100");
    Short myShort = new Short("100");
    Double myDouble = new Double("100");
    int myInteger = 100;
    Long myLong = new Long("100");

    List<SinkRecord> records = new ArrayList<SinkRecord>();
    SinkRecord record = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel", 42);
    record.headers().addBoolean("CamelPropertyMyBoolean", true);
    record.headers().addByte("CamelPropertyMyByte", myByte);
    record.headers().addFloat("CamelPropertyMyFloat", myFloat);
    record.headers().addShort("CamelPropertyMyShort", myShort);
    record.headers().addDouble("CamelPropertyMyDouble", myDouble);
    record.headers().addInt("CamelPropertyMyInteger", myInteger);
    record.headers().addLong("CamelPropertyMyLong", myLong);
    record.headers().addBoolean("CamelHeaderMyBoolean", true);
    record.headers().addByte("CamelHeaderMyByte", myByte);
    record.headers().addFloat("CamelHeaderMyFloat", myFloat);
    record.headers().addShort("CamelHeaderMyShort", myShort);
    record.headers().addDouble("CamelHeaderMyDouble", myDouble);
    record.headers().addInt("CamelHeaderMyInteger", myInteger);
    record.headers().addLong("CamelHeaderMyLong", myLong);
    records.add(record);
    sinkTask.put(records);

    ConsumerTemplate consumer = sinkTask.getCms().createConsumerTemplate();
    Exchange exchange = consumer.receive(SEDA_URI, RECEIVE_TIMEOUT);
    assertEquals("camel", exchange.getMessage().getBody());
    assertEquals("test", exchange.getMessage().getHeaders().get(CamelSinkTask.KAFKA_RECORD_KEY_HEADER));
    assertTrue((boolean) exchange.getProperties().get("MyBoolean"));
    assertEquals(myByte, (Byte) exchange.getProperties().get("MyByte"));
    assertEquals(myFloat, (Float) exchange.getProperties().get("MyFloat"));
    assertEquals(myShort, (Short) exchange.getProperties().get("MyShort"));
    assertEquals(myDouble, (Double) exchange.getProperties().get("MyDouble"));
    assertEquals(myInteger, exchange.getProperties().get("MyInteger"));
    assertEquals(myLong, (Long) exchange.getProperties().get("MyLong"));
    assertTrue(exchange.getIn().getHeader("MyBoolean", Boolean.class));
    assertEquals(myByte, exchange.getIn().getHeader("MyByte", Byte.class));
    assertEquals(myFloat, exchange.getIn().getHeader("MyFloat", Float.class));
    assertEquals(myShort, exchange.getIn().getHeader("MyShort", Short.class));
    assertEquals(myDouble, exchange.getIn().getHeader("MyDouble", Double.class));
    assertEquals(myInteger, exchange.getIn().getHeader("MyInteger"));
    assertEquals(myLong, exchange.getIn().getHeader("MyLong", Long.class));

    sinkTask.stop();
}
 
Example 11
Source File: CamelSinkTaskTest.java    From camel-kafka-connector with Apache License 2.0 4 votes vote down vote up
@Test
public void testBodyAndHeadersMap() {
    Map<String, String> props = new HashMap<>();
    props.put(CamelSinkConnectorConfig.TOPIC_CONF, TOPIC_NAME);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_URL_CONF, SEDA_URI);

    CamelSinkTask sinkTask = new CamelSinkTask();
    sinkTask.start(props);

    Byte myByte = new Byte("100");
    Float myFloat = new Float("100");
    Short myShort = new Short("100");
    Double myDouble = new Double("100");
    int myInteger = 100;
    Long myLong = new Long("100");
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("a", "a");
    HashMap<Integer, String> map1 = new HashMap<Integer, String>();
    map1.put(1, "a");
    HashMap<Integer, Integer> map2 = new HashMap<Integer, Integer>();
    map2.put(1, 1);

    List<SinkRecord> records = new ArrayList<SinkRecord>();
    SinkRecord record = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel", 42);
    record.headers().addBoolean("CamelHeaderMyBoolean", true);
    record.headers().addByte("CamelHeaderMyByte", myByte);
    record.headers().addFloat("CamelHeaderMyFloat", myFloat);
    record.headers().addShort("CamelHeaderMyShort", myShort);
    record.headers().addDouble("CamelHeaderMyDouble", myDouble);
    record.headers().addInt("CamelHeaderMyInteger", myInteger);
    record.headers().addLong("CamelHeaderMyLong", myLong);
    record.headers().addMap("CamelHeaderMyMap", map, SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.STRING_SCHEMA));
    record.headers().addMap("CamelHeaderMyMap1", map1, SchemaBuilder.map(Schema.INT64_SCHEMA, Schema.STRING_SCHEMA));
    record.headers().addMap("CamelHeaderMyMap2", map2, SchemaBuilder.map(Schema.INT64_SCHEMA, Schema.INT64_SCHEMA));
    records.add(record);
    sinkTask.put(records);

    ConsumerTemplate consumer = sinkTask.getCms().createConsumerTemplate();
    Exchange exchange = consumer.receive(SEDA_URI, RECEIVE_TIMEOUT);
    assertEquals("camel", exchange.getMessage().getBody());
    assertEquals("test", exchange.getMessage().getHeaders().get(CamelSinkTask.KAFKA_RECORD_KEY_HEADER));
    assertTrue(exchange.getIn().getHeader("MyBoolean", Boolean.class));
    assertEquals(myByte, exchange.getIn().getHeader("MyByte", Byte.class));
    assertEquals(myFloat, exchange.getIn().getHeader("MyFloat", Float.class));
    assertEquals(myShort, exchange.getIn().getHeader("MyShort", Short.class));
    assertEquals(myDouble, exchange.getIn().getHeader("MyDouble", Double.class));
    assertEquals(myInteger, exchange.getIn().getHeader("MyInteger"));
    assertEquals(myLong, exchange.getIn().getHeader("MyLong", Long.class));
    assertEquals(map, exchange.getIn().getHeader("MyMap", Map.class));
    assertEquals(map1, exchange.getIn().getHeader("MyMap1", Map.class));
    assertEquals(map2, exchange.getIn().getHeader("MyMap2", Map.class));
    sinkTask.stop();
}
 
Example 12
Source File: CamelSinkTaskTest.java    From camel-kafka-connector with Apache License 2.0 4 votes vote down vote up
@Test
public void testBodyAndPropertiesHeadersMapMixed() {
    Map<String, String> props = new HashMap<>();
    props.put(CamelSinkConnectorConfig.TOPIC_CONF, TOPIC_NAME);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_URL_CONF, SEDA_URI);

    CamelSinkTask sinkTask = new CamelSinkTask();
    sinkTask.start(props);

    Byte myByte = new Byte("100");
    Float myFloat = new Float("100");
    Short myShort = new Short("100");
    Double myDouble = new Double("100");
    int myInteger = 100;
    Long myLong = new Long("100");
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("a", "a");
    HashMap<Integer, String> map1 = new HashMap<Integer, String>();
    map1.put(1, "a");
    HashMap<Integer, Integer> map2 = new HashMap<Integer, Integer>();
    map2.put(1, 1);

    List<SinkRecord> records = new ArrayList<SinkRecord>();
    SinkRecord record = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel", 42);
    record.headers().addBoolean("CamelPropertyMyBoolean", true);
    record.headers().addByte("CamelPropertyMyByte", myByte);
    record.headers().addFloat("CamelPropertyMyFloat", myFloat);
    record.headers().addShort("CamelPropertyMyShort", myShort);
    record.headers().addDouble("CamelPropertyMyDouble", myDouble);
    record.headers().addInt("CamelPropertyMyInteger", myInteger);
    record.headers().addLong("CamelPropertyMyLong", myLong);
    record.headers().addMap("CamelPropertyMyMap", map, SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.STRING_SCHEMA));
    record.headers().addMap("CamelPropertyMyMap1", map1, SchemaBuilder.map(Schema.INT64_SCHEMA, Schema.STRING_SCHEMA));
    record.headers().addMap("CamelPropertyMyMap2", map2, SchemaBuilder.map(Schema.INT64_SCHEMA, Schema.INT64_SCHEMA));
    record.headers().addBoolean("CamelHeaderMyBoolean", true);
    record.headers().addByte("CamelHeaderMyByte", myByte);
    record.headers().addFloat("CamelHeaderMyFloat", myFloat);
    record.headers().addShort("CamelHeaderMyShort", myShort);
    record.headers().addDouble("CamelHeaderMyDouble", myDouble);
    record.headers().addInt("CamelHeaderMyInteger", myInteger);
    record.headers().addLong("CamelHeaderMyLong", myLong);
    record.headers().addMap("CamelHeaderMyMap", map, SchemaBuilder.map(Schema.STRING_SCHEMA, Schema.STRING_SCHEMA));
    record.headers().addMap("CamelHeaderMyMap1", map1, SchemaBuilder.map(Schema.INT64_SCHEMA, Schema.STRING_SCHEMA));
    record.headers().addMap("CamelHeaderMyMap2", map2, SchemaBuilder.map(Schema.INT64_SCHEMA, Schema.INT64_SCHEMA));
    records.add(record);
    sinkTask.put(records);

    ConsumerTemplate consumer = sinkTask.getCms().createConsumerTemplate();
    Exchange exchange = consumer.receive(SEDA_URI, RECEIVE_TIMEOUT);
    assertEquals("camel", exchange.getMessage().getBody());
    assertEquals("test", exchange.getMessage().getHeaders().get(CamelSinkTask.KAFKA_RECORD_KEY_HEADER));
    assertTrue((boolean) exchange.getProperties().get("MyBoolean"));
    assertEquals(myByte, (Byte) exchange.getProperties().get("MyByte"));
    assertEquals(myFloat, (Float) exchange.getProperties().get("MyFloat"));
    assertEquals(myShort, (Short) exchange.getProperties().get("MyShort"));
    assertEquals(myDouble, (Double) exchange.getProperties().get("MyDouble"));
    assertEquals(myInteger, exchange.getProperties().get("MyInteger"));
    assertEquals(myLong, (Long) exchange.getProperties().get("MyLong"));
    assertEquals(map, exchange.getProperties().get("MyMap"));
    assertEquals(map1, exchange.getProperties().get("MyMap1"));
    assertEquals(map2, exchange.getProperties().get("MyMap2"));
    assertTrue(exchange.getIn().getHeader("MyBoolean", Boolean.class));
    assertEquals(myByte, exchange.getIn().getHeader("MyByte", Byte.class));
    assertEquals(myFloat, exchange.getIn().getHeader("MyFloat", Float.class));
    assertEquals(myShort, exchange.getIn().getHeader("MyShort", Short.class));
    assertEquals(myDouble, exchange.getIn().getHeader("MyDouble", Double.class));
    assertEquals(myInteger, exchange.getIn().getHeader("MyInteger"));
    assertEquals(myLong, exchange.getIn().getHeader("MyLong", Long.class));
    assertEquals(map, exchange.getIn().getHeader("MyMap", Map.class));
    assertEquals(map1, exchange.getIn().getHeader("MyMap1", Map.class));
    assertEquals(map2, exchange.getIn().getHeader("MyMap2", Map.class));

    sinkTask.stop();
}
 
Example 13
Source File: CamelSinkTaskTest.java    From camel-kafka-connector with Apache License 2.0 4 votes vote down vote up
@Test
public void testBodyAndHeadersList() {
    Map<String, String> props = new HashMap<>();
    props.put(CamelSinkConnectorConfig.TOPIC_CONF, TOPIC_NAME);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_URL_CONF, SEDA_URI);

    CamelSinkTask sinkTask = new CamelSinkTask();
    sinkTask.start(props);

    Byte myByte = new Byte("100");
    Float myFloat = new Float("100");
    Short myShort = new Short("100");
    Double myDouble = new Double("100");
    int myInteger = 100;
    Long myLong = new Long("100");
    List<String> list = new ArrayList<String>();
    list.add("a");
    List<Integer> list1 = new ArrayList<Integer>();
    list1.add(1);

    List<SinkRecord> records = new ArrayList<SinkRecord>();
    SinkRecord record = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel", 42);
    record.headers().addBoolean("CamelHeaderMyBoolean", true);
    record.headers().addByte("CamelHeaderMyByte", myByte);
    record.headers().addFloat("CamelHeaderMyFloat", myFloat);
    record.headers().addShort("CamelHeaderMyShort", myShort);
    record.headers().addDouble("CamelHeaderMyDouble", myDouble);
    record.headers().addInt("CamelHeaderMyInteger", myInteger);
    record.headers().addLong("CamelHeaderMyLong", myLong);
    record.headers().addList("CamelHeaderMyList", list, SchemaBuilder.array(Schema.STRING_SCHEMA));
    record.headers().addList("CamelHeaderMyList1", list1, SchemaBuilder.array(Schema.INT64_SCHEMA));
    records.add(record);
    sinkTask.put(records);

    ConsumerTemplate consumer = sinkTask.getCms().createConsumerTemplate();
    Exchange exchange = consumer.receive(SEDA_URI, RECEIVE_TIMEOUT);
    assertEquals("camel", exchange.getMessage().getBody());
    assertEquals("test", exchange.getMessage().getHeaders().get(CamelSinkTask.KAFKA_RECORD_KEY_HEADER));
    assertTrue(exchange.getIn().getHeader("MyBoolean", Boolean.class));
    assertEquals(myByte, exchange.getIn().getHeader("MyByte", Byte.class));
    assertEquals(myFloat, exchange.getIn().getHeader("MyFloat", Float.class));
    assertEquals(myShort, exchange.getIn().getHeader("MyShort", Short.class));
    assertEquals(myDouble, exchange.getIn().getHeader("MyDouble", Double.class));
    assertEquals(myInteger, exchange.getIn().getHeader("MyInteger"));
    assertEquals(myLong, exchange.getIn().getHeader("MyLong", Long.class));
    assertEquals(list, exchange.getIn().getHeader("MyList", List.class));
    assertEquals(list1, exchange.getIn().getHeader("MyList1", List.class));
    sinkTask.stop();
}
 
Example 14
Source File: CamelSinkTaskTest.java    From camel-kafka-connector with Apache License 2.0 4 votes vote down vote up
@Test
public void testBodyAndPropertiesHeadersListMixed() {
    Map<String, String> props = new HashMap<>();
    props.put(CamelSinkConnectorConfig.TOPIC_CONF, TOPIC_NAME);
    props.put(CamelSinkConnectorConfig.CAMEL_SINK_URL_CONF, SEDA_URI);

    CamelSinkTask sinkTask = new CamelSinkTask();
    sinkTask.start(props);

    Byte myByte = new Byte("100");
    Float myFloat = new Float("100");
    Short myShort = new Short("100");
    Double myDouble = new Double("100");
    int myInteger = 100;
    Long myLong = new Long("100");
    List<String> list = new ArrayList<String>();
    list.add("a");
    List<Integer> list1 = new ArrayList<Integer>();
    list1.add(1);

    List<SinkRecord> records = new ArrayList<SinkRecord>();
    SinkRecord record = new SinkRecord(TOPIC_NAME, 1, null, "test", null, "camel", 42);
    record.headers().addBoolean("CamelPropertyMyBoolean", true);
    record.headers().addByte("CamelPropertyMyByte", myByte);
    record.headers().addFloat("CamelPropertyMyFloat", myFloat);
    record.headers().addShort("CamelPropertyMyShort", myShort);
    record.headers().addDouble("CamelPropertyMyDouble", myDouble);
    record.headers().addInt("CamelPropertyMyInteger", myInteger);
    record.headers().addLong("CamelPropertyMyLong", myLong);
    record.headers().addBoolean("CamelHeaderMyBoolean", true);
    record.headers().addByte("CamelHeaderMyByte", myByte);
    record.headers().addFloat("CamelHeaderMyFloat", myFloat);
    record.headers().addShort("CamelHeaderMyShort", myShort);
    record.headers().addDouble("CamelHeaderMyDouble", myDouble);
    record.headers().addInt("CamelHeaderMyInteger", myInteger);
    record.headers().addLong("CamelHeaderMyLong", myLong);
    record.headers().addList("CamelHeaderMyList", list, SchemaBuilder.array(Schema.STRING_SCHEMA));
    record.headers().addList("CamelHeaderMyList1", list1, SchemaBuilder.array(Schema.INT64_SCHEMA));
    record.headers().addList("CamelPropertyMyList", list, SchemaBuilder.array(Schema.STRING_SCHEMA));
    record.headers().addList("CamelPropertyMyList1", list1, SchemaBuilder.array(Schema.INT64_SCHEMA));
    records.add(record);
    sinkTask.put(records);

    ConsumerTemplate consumer = sinkTask.getCms().createConsumerTemplate();
    Exchange exchange = consumer.receive(SEDA_URI, RECEIVE_TIMEOUT);
    assertEquals("camel", exchange.getMessage().getBody());
    assertEquals("test", exchange.getMessage().getHeaders().get(CamelSinkTask.KAFKA_RECORD_KEY_HEADER));
    assertTrue((boolean) exchange.getProperties().get("MyBoolean"));
    assertEquals(myByte, (Byte) exchange.getProperties().get("MyByte"));
    assertEquals(myFloat, (Float) exchange.getProperties().get("MyFloat"));
    assertEquals(myShort, (Short) exchange.getProperties().get("MyShort"));
    assertEquals(myDouble, (Double) exchange.getProperties().get("MyDouble"));
    assertEquals(myInteger, exchange.getProperties().get("MyInteger"));
    assertEquals(myLong, (Long) exchange.getProperties().get("MyLong"));
    assertEquals(list, exchange.getProperties().get("MyList"));
    assertEquals(list1, exchange.getProperties().get("MyList1"));
    assertTrue(exchange.getIn().getHeader("MyBoolean", Boolean.class));
    assertEquals(myByte, exchange.getIn().getHeader("MyByte", Byte.class));
    assertEquals(myFloat, exchange.getIn().getHeader("MyFloat", Float.class));
    assertEquals(myShort, exchange.getIn().getHeader("MyShort", Short.class));
    assertEquals(myDouble, exchange.getIn().getHeader("MyDouble", Double.class));
    assertEquals(myInteger, exchange.getIn().getHeader("MyInteger"));
    assertEquals(myLong, exchange.getIn().getHeader("MyLong", Long.class));
    assertEquals(list, exchange.getIn().getHeader("MyList", List.class));
    assertEquals(list1, exchange.getIn().getHeader("MyList1", List.class));

    sinkTask.stop();
}
 
Example 15
Source File: RequestSender.java    From container with Apache License 2.0 4 votes vote down vote up
/**
 * Send an operation request to a remote OpenTOSCA Container node. All information needed for the remote operation
 * that shall be executed has to be defined as header fields of the given message or passed as
 * CollaborationMessage.
 *
 * @param message     the message containing the headers to send to the remote Container
 * @param operation   the operation to perform on the remote Container
 * @param requestBody the message body of the request
 * @param timeout     the timeout to wait for a reply in ms. Zero means no timeout at all
 * @return the exchange which is received as response of the request
 */
public Exchange sendRequestToRemoteContainer(final Message message, final RemoteOperations operation,
                                             final CollaborationMessage requestBody, final int timeout) {

    Objects.requireNonNull(message);

    // create an unique correlation ID for the request
    final String correlationID = UUID.randomUUID().toString();

    final Map<String, Object> requestHeaders = new HashMap<>();

    // add header fields of the incoming message to the outgoing message
    for (final MBHeader header : MBHeader.values()) {
        if (message.getHeader(header.toString()) != null) {
            requestHeaders.put(header.toString(), message.getHeader(header.toString()));
        }
    }

    // create header fields to forward the deployment requests
    requestHeaders.put(MBHeader.MQTTBROKERHOSTNAME_STRING.toString(), Constants.LOCAL_MQTT_BROKER);
    requestHeaders.put(MBHeader.MQTTTOPIC_STRING.toString(), Constants.REQUEST_TOPIC);
    requestHeaders.put(MBHeader.CORRELATIONID_STRING.toString(), correlationID);
    requestHeaders.put(MBHeader.REPLYTOTOPIC_STRING.toString(), Constants.RESPONSE_TOPIC);
    requestHeaders.put(MBHeader.REMOTEOPERATION_STRING.toString(), operation);

    LOG.debug("Publishing request to MQTT broker at {} with topic {} and correlation ID {}",
        Constants.LOCAL_MQTT_BROKER, Constants.REQUEST_TOPIC, correlationID);

    // publish the exchange over the camel route
    scheduler.schedule(() -> collaborationContext.getProducer().sendBodyAndHeaders("direct:SendMQTT", requestBody, requestHeaders),
        300, MILLISECONDS);

    final String callbackEndpoint = "direct:Callback-" + correlationID;
    LOG.debug("Waiting for response at endpoint: {}", callbackEndpoint);

    // wait for a response at the created callback
    final ConsumerTemplate consumer = collaborationContext.getCamelContext().createConsumerTemplate();

    Exchange response = null;
    if (timeout == 0) {
        // wait without timeout
        response = consumer.receive(callbackEndpoint);
    } else {
        // assess request as failed after timeout and return null
        response = consumer.receive(callbackEndpoint, timeout);
    }

    // release resources
    try {
        consumer.stop();
    } catch (final Exception e) {
        LOG.warn("Unable to stop consumer: {}", e.getMessage());
    }

    return response;
}