Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Apache Camel ships a Spring Boot Starter module that allows you to develop Spring Boot applications using starters. There is a sample application in the source code also.

...

Code Block
languagejava
package com.example;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("timer:foo")
          .to("log:bar");
    }
}

Then these routes will be started automatically. To keep the main thread blocked so that Camel stays up, either include the spring-boot-starter-web dependency, or add camel.springboot.main-run-controller=true to your application.properties or application.yml file. 

...

Code Block
languagejava
@Configuration
public class MyAppConfig {

  @Autowired
  CamelContext camelContext;

  @Bean
  MyService myService() {
    return new DefaultMyService(camelContext);
  }

}

Auto-Detecting Camel Routes

...

Code Block
languagejava
@Component
public class MyRouter extends RouteBuilder {

  @Override
  public void configure() throws Exception {
    from("jms:invoices").to("file:/invoices");
  }

}


...or creating a new route RouteBuilder bean in your @Configuration class:

...

Code Block
languagejava
@Configuration
public class MyAppConfig {

  ...

  @Bean
  CamelContextConfiguration contextConfiguration() {
    return new CamelContextConfiguration() {
      @Override
      void beforeApplicationStart(CamelContext context) {
        // your custom configuration goes here
      }
    };
  }

}

Method CamelContextConfiguration#beforeApplicationStart(CamelContext) will be called just before the Spring context is started, so the CamelContext instance passed to this callback is fully auto-configured. You can add many instances of CamelContextConfiguration into your Spring context - all of them will be executed.

...

Code Block
languagejava
@Component
public class InvoiceProcessor {

  @Autowired
  private ProducerTemplate producerTemplate;

  @Autowired
  private ConsumerTemplate consumerTemplate;
  public void processNextInvoice() {
    Invoice invoice = consumerTemplate.receiveBody("jms:invoices", Invoice.class);
    ...
    producerTemplate.sendBody("netty-http:http://invoicing.com/received/" + invoice.id());
  }

}

By default consumer templates and producer templates come with the endpoint cache sizes set to 1000. You can change those values via the following Spring properties:

...

By default you can put Camel Rest-DSL XML routes in the classpath under the directory camel-rest, which camel-spring-boot will auto detect and include.

 

You can configure the directory name or turn this off using the configuration optioncamel-spring-boot:

Code Block
// turn off
camel.springboot.xmlRests = false
// scan in the com/foo/routes classpath
camel.springboot.xmlRests = classpath:com/foo/rests/*.xml

...

Code Block
xml
xml
<rests xmlns="http://camel.apache.org/schema/spring">
   <rest>
      <post uri="/persons">
         <to uri="direct:postPersons"/>
      </post>
      <get uri="/persons">
         <to uri="direct:getPersons"/>
      </get>
      <get uri="/persons/{personId}">
          <to uri="direct:getPersionId"/>
      </get>
      <put uri="/persons/{personId}">
          <to uri="direct:putPersionId"/>
      </put>
      <delete uri="/persons/{personId}">
          <to uri="direct:deletePersionId"/>
      </delete>
   </rest>
 </rests>

Unit Tests

Below is a sample unit test set up for camel spring-boot. 

Code Block
languagejava
@ActiveProfiles("test")
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@DisableJmx(true)
public class MyRouteTest extends CamelTestSupport {

	@Autowired
	private CamelContext camelContext;

	@Override
	protected CamelContext createCamelContext() throws Exception {
		return camelContext;
	}

	@EndpointInject(uri = "direct:myEndpoint")
	private ProducerTemplate endpoint;

	@Override
	public void setUp() throws Exception {
		super.setUp();
		RouteDefinition definition = context().getRouteDefinitions().get(0);
		definition.adviceWith(context(), new RouteBuilder() {
			@Override
			public void configure() throws Exception {
				onException(Exception.class).maximumRedeliveries(0);
			}
		});
	}

	@Override
	public String isMockEndpointsAndSkip() {
        	return "myEndpoint:put*";
	}

	@Test
	public void shouldSucceed() throws Exception {
		assertNotNull(camelContext);
		assertNotNull(endpoint);
		
		String expectedValue = "expectedValue";
		MockEndpoint mock = getMockEndpoint("mock:myEndpoint:put");
		mock.expectedMessageCount(1);
		mock.allMessages().body().isEqualTo(expectedValue);
		mock.allMessages().header(MY_HEADER).isEqualTo("testHeader");
		endpoint.sendBodyAndHeader("test", MY_HEADER, "testHeader");
		
		mock.assertIsSatisfied();
	}
}


Include Page
Endpoint See Also
Endpoint See Also