Java Code Examples for org.apache.camel.Exchange#getProperty()

The following examples show how to use org.apache.camel.Exchange#getProperty() . These examples are extracted from open source projects. 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
@Deployment(resources = { "process/custom.bpmn20.xml" })
public void testRunProcess() throws Exception {
    CamelContext ctx = applicationContext.getBean(CamelContext.class);
    ProducerTemplate tpl = ctx.createProducerTemplate();
    service1.expectedBodiesReceived("ala");

    Exchange exchange = ctx.getEndpoint("direct:start").createExchange();
    exchange.getIn().setBody(Collections.singletonMap("var1", "ala"));
    tpl.send("direct:start", exchange);

    String instanceId = (String) exchange.getProperty("PROCESS_ID_PROPERTY");

    tpl.sendBodyAndProperty("direct:receive", null, FlowableProducer.PROCESS_ID_PROPERTY, instanceId);

    assertProcessEnded(instanceId);

    service1.assertIsSatisfied();

    @SuppressWarnings("rawtypes")
    Map m = service2.getExchanges().get(0).getIn().getBody(Map.class);
    assertEquals("ala", m.get("var1"));
    assertEquals("var2", m.get("var2"));
}
 
Example 2
@Deployment
public void testInitiatorCamelCall() throws Exception {
  CamelContext ctx = applicationContext.getBean(CamelContext.class);
  ProducerTemplate tpl = ctx.createProducerTemplate();
  String body = "body text";

  Exchange exchange = ctx.getEndpoint("direct:startWithInitiatorHeader").createExchange();
  exchange.getIn().setBody(body);
  tpl.send("direct:startWithInitiatorHeader", exchange);

  String instanceId = (String) exchange.getProperty("PROCESS_ID_PROPERTY");

  String initiator = (String) runtimeService.getVariable(instanceId, "initiator");
  assertEquals("kermit", initiator);

  Object camelInitiatorHeader = runtimeService.getVariable(instanceId, "CamelProcessInitiatorHeader");
  assertNull(camelInitiatorHeader);
}
 
Example 3
Source Project: gumtree-spoon-ast-diff   File: file_t.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Strategy to create the unit of work to be used for the sub route
 *
 * @param routeContext the route context
 * @param processor    the processor
 * @param exchange     the exchange
 * @return the unit of work processor
 */
protected Processor createUnitOfWorkProcessor(RouteContext routeContext, Processor processor, Exchange exchange) {
    String routeId = routeContext != null ? routeContext.getRoute().idOrCreate(routeContext.getCamelContext().getNodeIdFactory()) : null;
    CamelInternalProcessor internal = new CamelInternalProcessor(processor);

    // and wrap it in a unit of work so the UoW is on the top, so the entire route will be in the same UoW
    UnitOfWork parent = exchange.getProperty(Exchange.PARENT_UNIT_OF_WORK, UnitOfWork.class);
    if (parent != null) {
        internal.addAdvice(new CamelInternalProcessor.ChildUnitOfWorkProcessorAdvice(routeId, parent));
    } else {
        internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(routeId));
    }

    // and then in route context so we can keep track which route this is at runtime
    if (routeContext != null) {
        internal.addAdvice(new CamelInternalProcessor.RouteContextAdvice(routeContext));
    }
    return internal;
}
 
Example 4
@Test
@Deployment(resources = { "process/empty.bpmn20.xml" })
public void testRunProcessWithHeader() throws Exception {
    CamelContext ctx = applicationContext.getBean(CamelContext.class);
    ProducerTemplate tpl = camelContext.createProducerTemplate();
    String body = "body text";
    Exchange exchange = ctx.getEndpoint("direct:startEmptyWithHeader").createExchange();
    exchange.getIn().setBody(body);
    tpl.send("direct:startEmptyWithHeader", exchange);

    String instanceId = (String) exchange.getProperty("PROCESS_ID_PROPERTY");
    assertProcessEnded(instanceId);
    HistoricVariableInstance var = processEngine.getHistoryService().createHistoricVariableInstanceQuery().variableName("camelBody").singleResult();
    assertThat(var).isNotNull();
    assertThat(var.getValue()).isEqualTo(body);
    var = processEngine.getHistoryService().createHistoricVariableInstanceQuery().variableName("MyVar").singleResult();
    assertThat(var).isNotNull();
    assertThat(var.getValue()).isEqualTo("Foo");
}
 
Example 5
@Deployment(resources = { "process/custom.bpmn20.xml" })
public void testRunProcess() throws Exception {
  CamelContext ctx = applicationContext.getBean(CamelContext.class);
  ProducerTemplate tpl = ctx.createProducerTemplate();
  service1.expectedBodiesReceived("ala");

  Exchange exchange = ctx.getEndpoint("direct:start").createExchange();
  exchange.getIn().setBody(Collections.singletonMap("var1", "ala"));
  tpl.send("direct:start", exchange);

  String instanceId = (String) exchange.getProperty("PROCESS_ID_PROPERTY");

  tpl.sendBodyAndProperty("direct:receive", null, ActivitiProducer.PROCESS_ID_PROPERTY, instanceId);

  assertProcessEnded(instanceId);

  service1.assertIsSatisfied();

  @SuppressWarnings("rawtypes")
  Map m = service2.getExchanges().get(0).getIn().getBody(Map.class);
  assertEquals("ala", m.get("var1"));
  assertEquals("var2", m.get("var2"));
}
 
Example 6
@Deployment
public void testInitiatorCamelCall() throws Exception {
  CamelContext ctx = applicationContext.getBean(CamelContext.class);
  ProducerTemplate tpl = ctx.createProducerTemplate();
  String body = "body text";

  Exchange exchange = ctx.getEndpoint("direct:startWithInitiatorHeader").createExchange();
  exchange.getIn().setBody(body);
  tpl.send("direct:startWithInitiatorHeader", exchange);

  String instanceId = (String) exchange.getProperty("PROCESS_ID_PROPERTY");

  String initiator = (String) runtimeService.getVariable(instanceId, "initiator");
  assertEquals("kermit", initiator);

  Object camelInitiatorHeader = runtimeService.getVariable(instanceId, "CamelProcessInitiatorHeader");
  assertNull(camelInitiatorHeader);
}
 
Example 7
Source Project: gumtree-spoon-ast-diff   File: original.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Sets the given {@link org.apache.camel.processor.aggregate.AggregationStrategy} on the {@link Exchange}.
 *
 * @param exchange            the exchange
 * @param aggregationStrategy the strategy
 */
protected void setAggregationStrategyOnExchange(Exchange exchange, AggregationStrategy aggregationStrategy) {
    Map<?, ?> property = exchange.getProperty(Exchange.AGGREGATION_STRATEGY, Map.class);
    Map<Object, AggregationStrategy> map = CastUtils.cast(property);
    if (map == null) {
        map = new ConcurrentHashMap<Object, AggregationStrategy>();
    } else {
        // it is not safe to use the map directly as the exchange doesn't have the deep copy of it's properties
        // we just create a new copy if we need to change the map
        map = new ConcurrentHashMap<Object, AggregationStrategy>(map);
    }
    // store the strategy using this processor as the key
    // (so we can store multiple strategies on the same exchange)
    map.put(this, aggregationStrategy);
    exchange.setProperty(Exchange.AGGREGATION_STRATEGY, map);
}
 
Example 8
@Test
@Deployment(resources = { "process/example.bpmn20.xml" })
public void testRunProcess() throws Exception {
    CamelContext ctx = applicationContext.getBean(CamelContext.class);
    ProducerTemplate tpl = ctx.createProducerTemplate();
    service1.expectedBodiesReceived("ala");

    Exchange exchange = ctx.getEndpoint("direct:start").createExchange();
    exchange.getIn().setBody(Collections.singletonMap("var1", "ala"));
    tpl.send("direct:start", exchange);
    String instanceId = (String) exchange.getProperty("PROCESS_ID_PROPERTY");

    tpl.sendBodyAndProperty("direct:receive", null, FlowableProducer.PROCESS_ID_PROPERTY, instanceId);

    assertProcessEnded(instanceId);

    service1.assertIsSatisfied();
    Map<?, ?> m = service2.getExchanges().get(0).getIn().getBody(Map.class);
    assertThat(m.get("var1")).isEqualTo("ala");
    assertThat(m.get("var2")).isEqualTo("var2");
}
 
Example 9
Source Project: gumtree-spoon-ast-diff   File: patched.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Strategy to create the unit of work to be used for the sub route
 *
 * @param routeContext the route context
 * @param processor    the processor
 * @param exchange     the exchange
 * @return the unit of work processor
 */
protected Processor createUnitOfWorkProcessor(RouteContext routeContext, Processor processor, Exchange exchange) {
    String routeId = routeContext != null ? routeContext.getRoute().idOrCreate(routeContext.getCamelContext().getNodeIdFactory()) : null;
    CamelInternalProcessor internal = new CamelInternalProcessor(processor);

    // and wrap it in a unit of work so the UoW is on the top, so the entire route will be in the same UoW
    UnitOfWork parent = exchange.getProperty(Exchange.PARENT_UNIT_OF_WORK, UnitOfWork.class);
    if (parent != null) {
        internal.addAdvice(new CamelInternalProcessor.ChildUnitOfWorkProcessorAdvice(routeId, parent));
    } else {
        internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(routeId));
    }

    // and then in route context so we can keep track which route this is at runtime
    if (routeContext != null) {
        internal.addAdvice(new CamelInternalProcessor.RouteContextAdvice(routeContext));
    }
    return internal;
}
 
Example 10
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
@SuppressWarnings("unchecked")
public List<PropagationStatus> delete(
        final String key,
        final Set<String> excludedResources,
        final boolean nullPriorityAsync,
        final String eraser,
        final String context) {

    PollingConsumer pollingConsumer = getConsumer("direct:deleteGroupPort");

    Map<String, Object> props = new HashMap<>();
    props.put("excludedResources", excludedResources);
    props.put("nullPriorityAsync", nullPriorityAsync);
    props.put("eraser", eraser);
    props.put("context", context);

    sendMessage("direct:deleteGroup", key, props);

    Exchange exchange = pollingConsumer.receive();

    if (exchange.getProperty(Exchange.EXCEPTION_CAUGHT) != null) {
        throw (RuntimeException) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
    }

    return exchange.getIn().getBody(List.class);
}
 
Example 11
protected String findProcessInstanceId(Exchange exchange) {
  String processInstanceId = exchange.getProperty(PROCESS_ID_PROPERTY, String.class);
  if (processInstanceId != null) {
    return processInstanceId;
  }
  String processInstanceKey = exchange.getProperty(PROCESS_KEY_PROPERTY, String.class);
  ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(processInstanceKey).singleResult();

  if (processInstance == null) {
    throw new ActivitiException("Could not find activiti with key " + processInstanceKey);
  }
  return processInstance.getId();
}
 
Example 12
Source Project: gumtree-spoon-ast-diff   File: file_s.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Removes the associated {@link org.apache.camel.processor.aggregate.AggregationStrategy} from the {@link Exchange}
 * which must be done after use.
 *
 * @param exchange the current exchange
 */
protected void removeAggregationStrategyFromExchange(Exchange exchange) {
    Map<?, ?> property = exchange.getProperty(Exchange.AGGREGATION_STRATEGY, Map.class);
    Map<Object, AggregationStrategy> map = CastUtils.cast(property);
    if (map == null) {
        return;
    }
    // remove the strategy using this processor as the key
    map.remove(this);
}
 
Example 13
private static String extractCausedByException(Exchange exchange) {
    Throwable cause = exchange.getException();
    if (cause == null) {
        cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
    }
    return (cause != null) ? cause.toString() : null;
}
 
Example 14
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
@SuppressWarnings("unchecked")
public List<PropagationStatus> delete(
        final String key,
        final Set<String> excludedResources,
        final boolean nullPriorityAsync,
        final String eraser,
        final String context) {

    PollingConsumer pollingConsumer = getConsumer("direct:deletePort");

    Map<String, Object> props = new HashMap<>();
    props.put("excludedResources", excludedResources);
    props.put("nullPriorityAsync", nullPriorityAsync);
    props.put("eraser", eraser);
    props.put("context", context);

    sendMessage("direct:deleteUser", key, props);

    Exchange exchange = pollingConsumer.receive();

    if (exchange.getProperty(Exchange.EXCEPTION_CAUGHT) != null) {
        throw (RuntimeException) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
    }

    return exchange.getIn().getBody(List.class);
}
 
Example 15
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
    // to contains the endpoint which we send to so we know which system
    // has returned data back to us
    String to = newExchange.getProperty(Exchange.TO_ENDPOINT, String.class);
    if (to.contains("erp")) {
        return aggregate("ERP", oldExchange, newExchange);
    } else if (to.contains("crm")) {
        return aggregate("CRM", oldExchange, newExchange);
    } else {
        return aggregate("SHIPPING", oldExchange, newExchange);
    }
}
 
Example 16
private void handleHttpFailure(Exchange exchange) {
    HttpOperationFailedException e = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, HttpOperationFailedException.class);
    exchange.getOut().setHeaders(exchange.getIn().getHeaders());
    exchange.getOut().setBody(String.format(RESPONSE_STRING_FORMAT,
            String.format("%d %s", e.getStatusCode(), e.getResponseBody())
    ));
}
 
Example 17
@Override
@SuppressWarnings("unchecked")
public List<PropagationStatus> provision(
        final String key,
        final Collection<String> resources,
        final boolean nullPriorityAsync,
        final String updater,
        final String context) {

    PollingConsumer pollingConsumer = getConsumer("direct:provisionGroupPort");

    Map<String, Object> props = new HashMap<>();
    props.put("resources", resources);
    props.put("nullPriorityAsync", nullPriorityAsync);
    props.put("updater", updater);
    props.put("context", context);

    sendMessage("direct:provisionGroup", key, props);

    Exchange exchange = pollingConsumer.receive();

    if (exchange.getProperty(Exchange.EXCEPTION_CAUGHT) != null) {
        throw (RuntimeException) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
    }

    return exchange.getIn().getBody(List.class);
}
 
Example 18
Source Project: gumtree-spoon-ast-diff   File: file_t.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Removes the associated {@link org.apache.camel.processor.aggregate.AggregationStrategy} from the {@link Exchange}
 * which must be done after use.
 *
 * @param exchange the current exchange
 */
protected void removeAggregationStrategyFromExchange(Exchange exchange) {
    Map<?, ?> property = exchange.getProperty(Exchange.AGGREGATION_STRATEGY, Map.class);
    Map<Object, AggregationStrategy> map = CastUtils.cast(property);
    if (map == null) {
        return;
    }
    // remove the strategy using this processor as the key
    map.remove(this);
}
 
Example 19
Source Project: gumtree-spoon-ast-diff   File: file_t.java    License: Apache License 2.0 4 votes vote down vote up
protected Integer getExchangeIndex(Exchange exchange) {
    return exchange.getProperty(Exchange.MULTICAST_INDEX, Integer.class);
}
 
Example 20
Source Project: gumtree-spoon-ast-diff   File: original.java    License: Apache License 2.0 4 votes vote down vote up
protected Integer getExchangeIndex(Exchange exchange) {
    return exchange.getProperty(Exchange.MULTICAST_INDEX, Integer.class);
}