public static Map<?, ?> getItem(CamelContext camelctx) { HashMap<String, AttributeValue> key = new HashMap<>(); key.put("Id", new AttributeValue().withN("103")); Exchange exchange = new ExchangeBuilder(camelctx) .withHeader(DdbConstants.OPERATION, DdbOperations.GetItem) .withHeader(DdbConstants.KEY, key).build(); ProducerTemplate producer = camelctx.createProducerTemplate(); producer.send("direct:start", exchange); Assert.assertNull(exchange.getException()); return exchange.getIn().getHeader(DdbConstants.ATTRIBUTES, Map.class); }
private void onInternalMessageArrived(final String deviceId, final String appTopic, final KuraPayload message, final int qos, final boolean retain, final boolean control) { this.log.debug("Received message with deviceId {}, application topic {}.", deviceId, appTopic); final Exchange exchange = anExchange(getEndpoint().getCamelContext()) // .withBody(message) // .withHeader(CAMEL_KURA_CLOUD_TOPIC, appTopic) // .withHeader(CAMEL_KURA_CLOUD_DEVICEID, deviceId) // .withHeader(CAMEL_KURA_CLOUD_QOS, qos) // .withHeader(CAMEL_KURA_CLOUD_CONTROL, control) // .withHeader(CAMEL_KURA_CLOUD_RETAIN, retain) // .build(); exchange.setFromEndpoint(getEndpoint()); try { getProcessor().process(exchange); } catch (final Exception e) { handleException("Error while processing an incoming message:", e); } }
if (line.startsWith("$GPRMC")) { ClientGpsCoordinates coordinates = ClientGpsCoordinates.parseNMEA(line); Exchange exchange = ExchangeBuilder.anExchange(getEndpoint().getCamelContext()).withBody(coordinates).build(); getProcessor().process(exchange); return 1;
@Test public void whenExchangeExceptionIsNotNull_thenExceptionHasNotBeenHandled() { // Given String actionToDoInRoute = "throwIllegalArgumentException"; // When Exchange exchange = producerTemplate.send(anExchange(camelContext) .withProperty(actionToDoInRoute, "Did IQs just drop sharply while I was away?") .build()); // Then // ...... An Exception has NOT been handled if it IS in Exchange::exception assertThat(exchange.getException()).isExactlyInstanceOf(IllegalArgumentException.class); // assertThat(exchange.getProperty(EXCEPTION_CAUGHT)) .isExactlyInstanceOf(IllegalArgumentException.class); }
ExchangeBuilder exchangeBuilder = ExchangeBuilder.anExchange(producerTemplate.getCamelContext()); Exchange exchange = exchangeBuilder.build();
@Test public void mockEndpoint_snapshotsTheExchange() throws Exception { // Given weaveRoute(GreetingRouteBuilder.ROUTE_ID, routeBuilder -> routeBuilder.weaveByToString(".*ocessor.*") .replace() .to(mockEndpoint)); // And Exchange inputExchange = new ExchangeBuilder(camelContext) .withBody("Duke") .build(); // When producerTemplate.send(inputExchange); Exchange mockEndpointExchange = mockEndpoint.getExchanges().get(0); // Then assertThat(mockEndpointExchange.getExchangeId()).isEqualTo(inputExchange.getExchangeId()); // And assertThat(mockEndpointExchange.getProperty(FIRST_PROPERTY)).isEqualTo("Hello Duke"); assertThat(mockEndpointExchange.getProperty(SECOND_PROPERTY)).isNull(); // And assertThat(inputExchange.getProperty(FIRST_PROPERTY)).isEqualTo("Hello Duke"); assertThat(inputExchange.getProperty(SECOND_PROPERTY)).isEqualTo("Goodbye Duke"); // And assertThat(mockEndpointExchange).isNotSameAs(inputExchange); }
@Test public void whenExchangeExceptionIsNull_thenExceptionHasBeenHandled() { // Given String actionToDoInRoute = "throwNullPointerException"; // When Exchange exchange = producerTemplate.send(anExchange(camelContext) .withProperty(actionToDoInRoute, "Did IQs just drop sharply while I was away?") .build()); // Then // ...... An Exception has been handled if it is NOT in Exchange::exception assertThat(exchange.getException()).isNull(); // ...... But there is still evidence of it assertThat(exchange.getProperty(EXCEPTION_CAUGHT)) .isExactlyInstanceOf(NullPointerException.class); }
public static void putItem(CamelContext camelctx, String title) { HashMap<String, AttributeValue> putItem = new HashMap<>(); putItem.put("Id", new AttributeValue().withN("103")); putItem.put("Title", new AttributeValue().withS(title)); Exchange exchange = new ExchangeBuilder(camelctx) .withHeader(DdbConstants.OPERATION, DdbOperations.PutItem) .withHeader(DdbConstants.ITEM, putItem).build(); ProducerTemplate producer = camelctx.createProducerTemplate(); producer.send("direct:start", exchange); }
/** * Process the TPVObject, all params required. * * @param tpv The time-position-velocity object to process * @param processor Processor that handles the exchange. * @param endpoint GpsdEndpoint receiving the exchange. */ public static void consumeTPVObject(TPVObject tpv, Processor processor, GpsdEndpoint endpoint, ExceptionHandler exceptionHandler) { // Simplify logging when https://github.com/taimos/GPSd4Java/pull/19 is merged into upstream. LOG.debug("About to consume TPV object {},{} from {}", tpv.getLatitude(), tpv.getLongitude(), endpoint); Validate.notNull(tpv); Validate.notNull(processor); Validate.notNull(endpoint); GpsCoordinates coordinates = gpsCoordinates(new Date(new Double(tpv.getTimestamp()).longValue()), tpv.getLatitude(), tpv.getLongitude()); Exchange exchange = anExchange(endpoint.getCamelContext()).withPattern(OutOnly). withHeader(TPV_HEADER, tpv).withBody(coordinates).build(); try { processor.process(exchange); } catch (Exception e) { exceptionHandler.handleException(e); } } }
public static Map<?, ?> getItem(CamelContext camelctx) { HashMap<String, AttributeValue> key = new HashMap<>(); key.put("Id", new AttributeValue().withN("103")); Exchange exchange = new ExchangeBuilder(camelctx) .withHeader(DdbConstants.OPERATION, DdbOperations.GetItem) .withHeader(DdbConstants.KEY, key).build(); ProducerTemplate producer = camelctx.createProducerTemplate(); producer.send("direct:start", exchange); Assert.assertNull(exchange.getException()); return exchange.getIn().getHeader(DdbConstants.ATTRIBUTES, Map.class); }
public static void putItem(CamelContext camelctx, String title) { HashMap<String, AttributeValue> putItem = new HashMap<>(); putItem.put("Id", new AttributeValue().withN("103")); putItem.put("Title", new AttributeValue().withS(title)); Exchange exchange = new ExchangeBuilder(camelctx) .withHeader(DdbConstants.OPERATION, DdbOperations.PutItem) .withHeader(DdbConstants.ITEM, putItem).build(); ProducerTemplate producer = camelctx.createProducerTemplate(); producer.send("direct:start", exchange); }
public static void updItem(CamelContext camelctx, String title) { HashMap<String, AttributeValue> key = new HashMap<>(); key.put("Id", new AttributeValue().withN("103")); HashMap<String, AttributeValueUpdate> updItem = new HashMap<>(); AttributeValueUpdate updValue = new AttributeValueUpdate(); updValue.setValue(new AttributeValue().withS(title)); updItem.put("Title", updValue); Exchange exchange = new ExchangeBuilder(camelctx) .withHeader(DdbConstants.OPERATION, DdbOperations.UpdateItem) .withHeader(DdbConstants.KEY, key) .withHeader(DdbConstants.UPDATE_VALUES, updItem).build(); ProducerTemplate producer = camelctx.createProducerTemplate(); producer.send("direct:start", exchange); Assert.assertNull(exchange.getException()); }
public static void updItem(CamelContext camelctx, String title) { HashMap<String, AttributeValue> key = new HashMap<>(); key.put("Id", new AttributeValue().withN("103")); HashMap<String, AttributeValueUpdate> updItem = new HashMap<>(); AttributeValueUpdate updValue = new AttributeValueUpdate(); updValue.setValue(new AttributeValue().withS(title)); updItem.put("Title", updValue); Exchange exchange = new ExchangeBuilder(camelctx) .withHeader(DdbConstants.OPERATION, DdbOperations.UpdateItem) .withHeader(DdbConstants.KEY, key) .withHeader(DdbConstants.UPDATE_VALUES, updItem).build(); ProducerTemplate producer = camelctx.createProducerTemplate(); producer.send("direct:start", exchange); Assert.assertNull(exchange.getException()); }