Java Code Examples for org.apache.camel.ProducerTemplate#sendBodyAndHeaders()

The following examples show how to use org.apache.camel.ProducerTemplate#sendBodyAndHeaders() . 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: MailSendServlet.java    From wildfly-camel-examples with Apache License 2.0 6 votes vote down vote up
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    Map<String, Object> headers = new HashMap<>();

    Enumeration<String> parameterNames = request.getParameterNames();
    while (parameterNames.hasMoreElements()) {
        String parameterName = parameterNames.nextElement();
        String parameterValue = request.getParameter(parameterName);
        headers.put(parameterName, parameterValue);
    }

    try {
        ProducerTemplate producer = camelContext.createProducerTemplate();
        producer.sendBodyAndHeaders("direct:sendmail", request.getParameter("message"), headers);
        request.getRequestDispatcher("/success.jsp").forward(request, response);
    } catch (CamelExecutionException e) {
        request.setAttribute("error", e);
        request.getRequestDispatcher("/error.jsp").forward(request, response);
    }
}
 
Example 2
Source File: MailSendServlet.java    From wildfly-camel-examples with Apache License 2.0 6 votes vote down vote up
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
  Map<String, Object> headers = new HashMap<>();

  Enumeration<String> parameterNames = request.getParameterNames();
  while(parameterNames.hasMoreElements()) {
    String parameterName = parameterNames.nextElement();
    String parameterValue = request.getParameter(parameterName);
    headers.put(parameterName, parameterValue);
  }

  try {
    ProducerTemplate producer = camelContext.createProducerTemplate();
    producer.sendBodyAndHeaders("direct:sendmail", request.getParameter("message"), headers);
    request.getRequestDispatcher("/success.jsp").forward(request, response);
  } catch (CamelExecutionException e) {
    request.setAttribute("error", e);
    request.getRequestDispatcher("/error.jsp").forward(request, response);
  }
}
 
Example 3
Source File: KarafIT.java    From fcrepo-camel-toolbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testService() throws Exception {
    final String baseUrl = "http://localhost:" + System.getProperty("fcrepo.port") + "/fcrepo/rest";
    final CamelContext ctx = getOsgiService(CamelContext.class,
            "(camel.context.name=FcrepoService)", 10000);

    assertNotNull(ctx);

    final MockEndpoint resultEndpoint = (MockEndpoint) ctx.getEndpoint("mock:result");

    final String url1 = post(baseUrl).replace(baseUrl, "");
    final String url2 = post(baseUrl).replace(baseUrl, "");

    final ProducerTemplate template = ctx.createProducerTemplate();
    final Map<String, Object> headers = new HashMap<>();
    headers.put(FCREPO_BASE_URL, baseUrl);
    headers.put(FCREPO_IDENTIFIER, url1);
    template.sendBodyAndHeaders(ctx.getEndpoint("direct:start"), null, headers);

    template.sendBodyAndHeader(ctx.getEndpoint("direct:start"), null, FCREPO_URI, baseUrl + url2);

    resultEndpoint.expectedMinimumMessageCount(2);
    assertIsSatisfied(resultEndpoint);
}
 
Example 4
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutWithTTL() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put(HazelcastConstants.OBJECT_ID, "4711");
        headers.put(HazelcastConstants.TTL_VALUE, new Long(1));
        headers.put(HazelcastConstants.TTL_UNIT, TimeUnit.MINUTES);

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeaders("direct:put", "test", headers);
        Mockito.verify(map).put("4711", "test", 1, TimeUnit.MINUTES);
    } finally {
        camelctx.close();
    }
}
 
Example 5
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateOldValue() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put(HazelcastConstants.OBJECT_ID, "4711");
        headers.put(HazelcastConstants.OBJECT_VALUE, "my-foo");

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeaders("direct:update", "replaced", headers);
        Mockito.verify(map).lock("4711");
        Mockito.verify(map).replace("4711", "my-foo", "replaced");
        Mockito.verify(map).unlock("4711");
    } finally {
        camelctx.close();
    }
}
 
Example 6
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutIfAbsentWithTtl() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put(HazelcastConstants.OBJECT_ID, "4711");
        headers.put(HazelcastConstants.TTL_VALUE, new Long(1));
        headers.put(HazelcastConstants.TTL_UNIT, TimeUnit.MINUTES);

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeaders("direct:putIfAbsent", "replaced", headers);
        Mockito.verify(map).putIfAbsent("4711", "replaced", new Long(1), TimeUnit.MINUTES);
    } finally {
        camelctx.close();
    }
}
 
Example 7
Source File: CamelAlerter.java    From karaf-decanter with Apache License 2.0 5 votes vote down vote up
@Override
public void handleEvent(Event event) {
    HashMap<String, Object> data = new HashMap<>();
    for (String name : event.getPropertyNames()) {
        data.put(name, event.getProperty(name));
    }
    ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
    Map<String, Object> headers = new HashMap<>();
    headers.put("alertLevel", event.getProperty("alertLevel"));
    headers.put("alertPattern", event.getProperty("alertPattern"));
    headers.put("alertBackToNormal", event.getProperty("alertBackToNormal"));
    producerTemplate.sendBodyAndHeaders(alertDestinationUri, data, headers);
}
 
Example 8
Source File: EtcdIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testEtcdComponent() throws Exception {
    DefaultCamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            String dockerHost = TestUtils.getDockerHost();

            from("direct:start")
            .toF("etcd:keys?uris=http://%s:23379,http://%s:40001", dockerHost, dockerHost)
            .to("mock:result");
        }
    });

    String path = "/camel/" + UUID.randomUUID().toString();
    String value = UUID.randomUUID().toString();

    Map<String, Object> headers = new HashMap<>();
    headers.put(EtcdConstants.ETCD_ACTION, EtcdConstants.ETCD_KEYS_ACTION_SET);
    headers.put(EtcdConstants.ETCD_PATH, path);

    MockEndpoint mockEndpoint = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    mockEndpoint.expectedMinimumMessageCount(1);
    mockEndpoint.expectedHeaderReceived(EtcdConstants.ETCD_PATH, path);

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeaders("direct:start", value, headers);
        mockEndpoint.assertIsSatisfied();
    } finally {
        camelctx.close();
    }
}
 
Example 9
Source File: NagiosIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendToNagiosMultiHeaders() throws Exception {

    CamelContext camelctx = createCamelContext();

    MessagePayload expectedPayload1 = new MessagePayload("myHost", Level.CRITICAL, "myService",  "Hello Nagios");

    MockEndpoint mock = camelctx.getEndpoint("mock:result", MockEndpoint.class);
    mock.expectedMessageCount(1);
    mock.expectedBodiesReceived("Hello Nagios");

    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(NagiosConstants.LEVEL, "CRITICAL");
    headers.put(NagiosConstants.HOST_NAME, "myHost");
    headers.put(NagiosConstants.SERVICE_NAME, "myService");

    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeaders("direct:start", "Hello Nagios", headers);

        mock.assertIsSatisfied();
        Mockito.verify(nagiosPassiveCheckSender).send(expectedPayload1);
    } finally {
        camelctx.close();
    }
}
 
Example 10
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutIfAbsent() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put(HazelcastConstants.OBJECT_ID, "4711");

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeaders("direct:putIfAbsent", "replaced", headers);
        Mockito.verify(map).putIfAbsent("4711", "replaced");
    } finally {
        camelctx.close();
    }
}
 
Example 11
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvict() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put(HazelcastConstants.OBJECT_ID, "4711");

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeaders("direct:evict", "", headers);
        Mockito.verify(map).evict("4711");
    } finally {
        camelctx.close();
    }
}
 
Example 12
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvictAll() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        Map<String, Object> headers = new HashMap<String, Object>();

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeaders("direct:evictAll", "", headers);
        Mockito.verify(map).evictAll();
    } finally {
        camelctx.close();
    }
}
 
Example 13
Source File: CloudWatchIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testKeyValueOperations() throws Exception {

    AmazonCloudWatchClient cwClient = provider.getClient();
    Assume.assumeNotNull("AWS client not null", cwClient);

    List<Metric> staleMetrics = cwClient.listMetrics(new ListMetricsRequest().withNamespace(NAMESPACE)).getMetrics()
            .stream() //
            .filter(metric -> !metric.getMetricName().startsWith(CloudWatchIntegrationTest.class.getSimpleName())
                    || System.currentTimeMillis()
                            - AWSUtils.toEpochMillis(metric.getMetricName()) > AWSUtils.TWO_WEEKS) //
            .collect(Collectors.toList());
    if (staleMetrics.size() > 0) {
        Assert.fail("Found '" + CloudWatchIntegrationTest.class.getName() + "-*' metrics older than two weeks: "
                + staleMetrics);
    }

    WildFlyCamelContext camelctx = new WildFlyCamelContext();
    camelctx.getNamingContext().bind("cwClient", cwClient);

    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:metrics").to("aws-cw://" + NAMESPACE + "?amazonCwClient=#cwClient");
        }
    });

    camelctx.start();
    try {
        Map<String, Object> headers = new HashMap<>();
        headers.put(CwConstants.METRIC_NAME, METRIC_NAME);
        headers.put(CwConstants.METRIC_DIMENSION_NAME, DIM_NAME);
        headers.put(CwConstants.METRIC_DIMENSION_VALUE, DIM_VALUE);

        ListMetricsRequest request = new ListMetricsRequest().withNamespace(NAMESPACE).withMetricName(METRIC_NAME)
                .withDimensions(new DimensionFilter().withName(DIM_NAME).withValue(DIM_VALUE));

        List<Metric> metrics = Collections.emptyList();
        ProducerTemplate producer = camelctx.createProducerTemplate();
        for (int i = 100; i < 105 && metrics.size() == 0; i++) {
            producer.sendBodyAndHeaders("direct:metrics", new Double(i), headers);
            metrics = cwClient.listMetrics(request).getMetrics();
            System.out.println("metrics #" + i + ": " + metrics);
            Thread.sleep(1000);
        }

        // It may take several minutes for the metric to show up
        // Assert.assertEquals(1, metrics.size());

    } finally {
        camelctx.close();
    }
}