/** * Sends a message to the given endpoint URI with the body value * * @param endpointUri the URI of the endpoint to send to * @param body the body for the message */ protected void sendBody(String endpointUri, final Object body) { template.send(endpointUri, new Processor() { public void process(Exchange exchange) { Message in = exchange.getIn(); in.setBody(body); } }); }
/** * Sends a message to the given endpoint URI with the body value and specified headers * * @param endpointUri the URI of the endpoint to send to * @param body the body for the message * @param headers any headers to set on the message */ protected void sendBody(String endpointUri, final Object body, final Map<String, Object> headers) { template.send(endpointUri, new Processor() { public void process(Exchange exchange) { Message in = exchange.getIn(); in.setBody(body); for (Map.Entry<String, Object> entry : headers.entrySet()) { in.setHeader(entry.getKey(), entry.getValue()); } } }); }
public Exchange issueRequest(Endpoint endpoint, ExchangePattern pattern, final Object body, final Map<String, Object> headers) throws CamelExecutionException { Exchange exchange = producer.send(endpoint, pattern, new Processor() { public void process(Exchange exchange) throws Exception { Message in = exchange.getIn(); in.getHeaders().putAll(headers); in.setBody(body); } }); return exchange; }
public GetSelectionResponse getCurrentSelection() { Exchange e = new DefaultExchange(getContext()); e.getIn().setBody(new GetSelection()); getContext().createProducerTemplate().send("direct:sendAnnotationAlexInOut", e); return e.getOut().getBody(GetSelectionResponse.class); }
@Override public List<String> getAnnotationIDs() { Exchange e = new DefaultExchange(getContext()); e.getIn().setBody(new GetAnnotations()); getContext().createProducerTemplate().send("direct:sendAnnotationAlexInOut", e); GetAnnotationsResponse resp = e.getOut().getBody(GetAnnotationsResponse.class); return resp.getId(); } }
@Override public Serializable doSend(final Message message, final MeasurementUnit measurementUnit) throws Exception { final Exchange exchange = template.send(endpoint, new MessageProcessor(message.getPayload(), message.getHeaders())); final Message response = new Message(); response.setPayload(exchange.getOut().getBody(String.class)); exchange.getOut().getHeaders().forEach((k, v) -> response.setHeader(k, v.toString())); return response; }
public static void sendBodyAndProperties(final Object body, final Map<String, Object> properties, ProducerTemplate producerTemplate) throws CamelExecutionException { // see DefaultProducerTemplate#sendBodyAndProperty() Exchange result = producerTemplate.send(producerTemplate.getDefaultEndpoint(), createBodyAndPropertiesProcessor( body, properties)); // must invoke extract result body in case of exception to be rethrown ExchangeHelper.extractResultBody(result, null); }
protected void sendMessage(final String uri, final Object body, final Map<String, Object> properties) { Exchange exchange = new DefaultExchange(contextFactory.getCamelContext()); for (Map.Entry<String, Object> property : properties.entrySet()) { exchange.setProperty(property.getKey(), property.getValue()); LOG.debug("Added property {}", property.getKey()); } DefaultMessage message = new DefaultMessage(contextFactory.getCamelContext()); message.setBody(body); exchange.setIn(message); ProducerTemplate template = contextFactory.getCamelContext().createProducerTemplate(); template.send(uri, exchange); }
private void resendWithDelay(Exchange message) { if (retryProducer == null) { retryProducer = camelContext.createProducerTemplate(); retryProducer.setDefaultEndpointUri(context.getEndpoints().getDelayedInputUri()); } message.getIn().setHeader(MESSAGE_RESENT_ID_HEADER, message.getExchangeId()); retryProducer.send(message); logger.debug("Successfully resent message for plugin '{}' with id '{}'", context.getId(), message.getExchangeId()); }
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 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); }
from("direct:scan") .process(new Processor() { public void process(Exchange exchange) throws Exception { ConsumerTemplate consumerTemplate = exchange.getContext().createConsumerTemplate(); ProducerTemplate producerTemplate = exchange.getContext().createProducerTemplate(); producerTemplate.setDefaultEndpointUri("file:.."); producerTemplate.send(consumerTemplate.receive("ftp:..")); } });
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 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); }
@Test public void redelivery_resumesFromPointOfFailure() { // Given doThrow(new RuntimeException("Oh no!")).when(camelFriend).throwException(); // When producerTemplate.send(new DefaultExchange(new DefaultCamelContext())); // Then verify(camelFriend, times(1)).before(); verify(camelFriend, times(3)).throwException(); verify(camelFriend, times(0)).after(); }
@Test public void testValidator6() throws InterruptedException, IOException { final String xml = IOUtils.toString(getClass().getResourceAsStream("/schematron/schematron-test-fail.xml"), Charset.defaultCharset()); error.expectedMessageCount(1); Exchange exchange = producerTemplate.send("direct:validator-schematron-test", ExchangePattern.InOut, exchange1 -> exchange1.getIn().setBody(xml)); assertEquals(SchematronValidationException.class, exchange.getException().getClass()); ValidationException e = (ValidationException) exchange.getException(); assertEquals(3, e.getCauses().length); error.assertIsSatisfied(2000); } }
@Test public void testValidator2() throws InterruptedException { error.expectedMessageCount(1); Exchange exchange = producerTemplate.send("direct:validator-test", ExchangePattern.InOut, exchange1 -> exchange1.getIn().setBody("incorrect")); assertEquals(ValidationException.class, exchange.getException().getClass()); error.assertIsSatisfied(2000); }
@Test public void testValidator4() throws InterruptedException, IOException { final String xml = IOUtils.toString(getClass().getResourceAsStream("/xsd/invalidtest.xml"), Charset.defaultCharset()); error.expectedMessageCount(1); Exchange exchange = producerTemplate.send("direct:validator-xml-test", ExchangePattern.InOut, exchange1 -> exchange1.getIn().setBody(xml)); assertEquals(ValidationException.class, exchange.getException() .getClass()); ValidationException e = (ValidationException) exchange.getException(); assertEquals(5, e.getCauses().length); error.assertIsSatisfied(2000); }
private void sendData(CamelContext context, List<String> messages) { ProducerTemplate template = context.createProducerTemplate(); for (String message : messages) { Exchange exchange = context.getEndpoint(directEndpoint.toString()).createExchange(); Message msg = exchange.getIn(); msg.setBody(message); template.send(directEndpoint.toString(), exchange); } }
protected void sendMessage(final String uri, final Object obj) { Exchange exchange = new DefaultExchange(contextFactory.getCamelContext()); DefaultMessage message = new DefaultMessage(contextFactory.getCamelContext()); message.setBody(obj); exchange.setIn(message); ProducerTemplate template = contextFactory.getCamelContext().createProducerTemplate(); template.send(uri, exchange); }